MXS-1220: Add HEAD method support

The HEAD method was not in the list of supported methods.
This commit is contained in:
Markus Mäkelä
2017-04-17 01:18:56 +03:00
committed by Markus Mäkelä
parent c937457738
commit 55b52b8ab1
2 changed files with 37 additions and 14 deletions

View File

@ -28,7 +28,8 @@ enum http_verb
HTTP_PUT, HTTP_PUT,
HTTP_POST, HTTP_POST,
HTTP_OPTIONS, HTTP_OPTIONS,
HTTP_PATCH HTTP_PATCH,
HTTP_HEAD
}; };
/** Possible HTTP return codes */ /** Possible HTTP return codes */
@ -101,6 +102,10 @@ static inline enum http_verb string_to_http_verb(string& verb)
{ {
return HTTP_OPTIONS; return HTTP_OPTIONS;
} }
else if (verb == "HEAD")
{
return HTTP_HEAD;
}
return HTTP_UNKNOWN; return HTTP_UNKNOWN;
} }
@ -126,6 +131,8 @@ static inline const char* http_verb_to_string(enum http_verb verb)
return "PATCH"; return "PATCH";
case HTTP_OPTIONS: case HTTP_OPTIONS:
return "OPTIONS"; return "OPTIONS";
case HTTP_HEAD:
return "HEAD";
default: default:
return "UNKNOWN"; return "UNKNOWN";
} }

View File

@ -27,6 +27,7 @@ const char* verbs_pass[] =
"POST", "POST",
"OPTIONS", "OPTIONS",
"PATCH", "PATCH",
"HEAD",
NULL NULL
}; };
@ -264,27 +265,42 @@ const char* body_fail[] =
NULL NULL
}; };
const char* body_verbs_pass[] =
{
"PUT",
"POST",
"PATCH",
NULL
};
int test_message_body() int test_message_body()
{ {
for (int i = 0; body_pass[i]; i++) for (int i = 0; body_pass[i]; i++)
{ {
stringstream ss; for (int j = 0; body_verbs_pass[j]; j++)
ss << "GET / HTTP/1.1\r\n\r\n" << body_pass[i]; {
SHttpRequest parser(HttpRequest::parse(ss.str())); /** Only PUT/POST/PATCH methods should have request bodies */
TEST(parser.get() != NULL, "Valid request body should be parsed: %s", stringstream ss;
ss.str().c_str()); ss << body_verbs_pass[j] << " / HTTP/1.1\r\n\r\n" << body_pass[i];
TEST(parser->get_json(), "Body should be found"); SHttpRequest parser(HttpRequest::parse(ss.str()));
TEST(parser->get_json_str() == body_pass[i], "Body value should be correct: %s", TEST(parser.get() != NULL, "Valid request body should be parsed: %s",
parser->get_json_str().c_str()); ss.str().c_str());
TEST(parser->get_json(), "Body should be found");
TEST(parser->get_json_str() == body_pass[i], "Body value should be correct: %s",
parser->get_json_str().c_str());
}
} }
for (int i = 0; body_pass[i]; i++) for (int i = 0; body_pass[i]; i++)
{ {
stringstream ss; for (int j = 0; verbs_pass[j]; j++)
ss << "GET / HTTP/1.1\r\n\r\n" << body_fail[i]; {
SHttpRequest parser(HttpRequest::parse(ss.str())); stringstream ss;
TEST(parser.get() == NULL, "Invalid request body should not be parsed: %s", ss << verbs_pass[j] << " / HTTP/1.1\r\n\r\n" << body_fail[i];
ss.str().c_str()); SHttpRequest parser(HttpRequest::parse(ss.str()));
TEST(parser.get() == NULL, "Invalid request body should not be parsed: %s",
ss.str().c_str());
}
} }
return 0; return 0;