From ec566f87efadf88591f4ddf0b74026d65d8f459d Mon Sep 17 00:00:00 2001 From: The-EDev Date: Sat, 3 Apr 2021 06:00:23 +0300 Subject: [PATCH] added tests and fixed small issues moved body handling to Response.end() and fixed a bug where a 404 was not returned on a route that doesn't exist --- include/crow/http_connection.h | 5 ---- include/crow/http_response.h | 5 +++- include/crow/routing.h | 2 +- tests/unittest.cpp | 48 ++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/include/crow/http_connection.h b/include/crow/http_connection.h index ac7c9046f..f30f17ea9 100644 --- a/include/crow/http_connection.h +++ b/include/crow/http_connection.h @@ -357,11 +357,6 @@ namespace crow (*middlewares_, ctx_, req_, res); } - if (res.no_body) - { - res.body = ""; - } - std::string accept_encoding = req_.get_header_value("Accept-Encoding"); if (!accept_encoding.empty() && res.compressed) { diff --git a/include/crow/http_response.h b/include/crow/http_response.h index f6c5bc29b..d65fcd03e 100644 --- a/include/crow/http_response.h +++ b/include/crow/http_response.h @@ -148,7 +148,10 @@ namespace crow if (!completed_) { completed_ = true; - + if (no_body) + { + body = ""; + } if (complete_request_handler_) { complete_request_handler_(); diff --git a/include/crow/routing.h b/include/crow/routing.h index 51cad7004..41496f3de 100644 --- a/include/crow/routing.h +++ b/include/crow/routing.h @@ -1119,7 +1119,7 @@ namespace crow allow += method_name((HTTPMethod)i) + ", "; } } - if (allow.size() > 0) + if (allow != "OPTIONS, HEAD, ") { allow = allow.substr(0, allow.size()-2); res = response(204); diff --git a/tests/unittest.cpp b/tests/unittest.cpp index 5bc1910ea..b91b22be9 100644 --- a/tests/unittest.cpp +++ b/tests/unittest.cpp @@ -378,6 +378,54 @@ TEST_CASE("http_method") CHECK(405 == res.code); } + + { + request req; + response res; + + req.url = "/get_only"; + req.method = "HEAD"_method; + app.handle(req, res); + + CHECK(200 == res.code); + CHECK("" == res.body); + } + + { + request req; + response res; + + req.url = "/"; + req.method = "OPTIONS"_method; + app.handle(req, res); + + CHECK(204 == res.code); + CHECK("OPTIONS, HEAD, GET, POST" == res.get_header_value("Allow")); + } + + { + request req; + response res; + + req.url = "/does_not_exist"; + req.method = "OPTIONS"_method; + app.handle(req, res); + + CHECK(404 == res.code); + } + + { + request req; + response res; + + req.url = "/*"; + req.method = "OPTIONS"_method; + app.handle(req, res); + + CHECK(204 == res.code); + CHECK("OPTIONS, HEAD, GET, POST, PATCH, PURGE" == res.get_header_value("Allow")); + + } } TEST_CASE("server_handling_error_request")