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
This commit is contained in:
The-EDev 2021-04-03 06:00:23 +03:00
parent 06226b86a7
commit ec566f87ef
4 changed files with 53 additions and 7 deletions

View File

@ -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)
{

View File

@ -148,7 +148,10 @@ namespace crow
if (!completed_)
{
completed_ = true;
if (no_body)
{
body = "";
}
if (complete_request_handler_)
{
complete_request_handler_();

View File

@ -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);

View File

@ -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")