Skip to content

Commit

Permalink
Add test for POST request with wide characters (UTF-8, etc.).
Browse files Browse the repository at this point in the history
This commit introduces a new test function to handle POST requests containing wide characters in the URL and headers. It ensures that the parsing logic correctly processes such requests and validates the content and headers appropriately. This enhancement improves the robustness of the HTTP request parser.
  • Loading branch information
SeriousSamV committed Oct 31, 2024
1 parent 0aa64a9 commit 2895636
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions test/assert_tiny_http_server_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,48 @@ void test_request_post_root_curl(void) {
destroy_http_request(http_req);
}

void test_request_post_root_curl_with_wide_chars(void) {
// ReSharper disable once CppVariableCanBeMadeConstexpr
const uint8_t request[] = "POST /one/🐌/three HTTP/1.0\r\n"
"Content-Type: application/json\r\n"
"User-Agent: PostmanRuntime/7.42.0\r\n"
"Accept: */*\r\n"
"Test-Header: 🐍\r\n"
"Host: localhost:8085\r\n"
"Accept-Encoding: gzip, deflate, br\r\n"
"Content-Length: 68\r\n"
"\r\n"
"{\n \"key1\": \"🐌\",\n \"key2\": \"value2\",\n \"key3\": \"value3\"\n}";
http_request *http_req = parse_http_request(request, strlen((char *) request));
assert(http_req != nullptr);
assert(http_req->method == POST);
assert(http_req->version == HTTP_1_0);
assert(strncmp(http_req->url, "/one/🐌/three", 255) == 0);
assert(strncmp(http_req->headers[0]->name, "Content-Type", 255) == 0);
assert(strncmp(http_req->headers[0]->value, "application/json", 255) == 0);
assert(strncmp(http_req->headers[1]->name, "User-Agent", 255) == 0);
assert(strncmp(http_req->headers[1]->value, "PostmanRuntime/7.42.0", 255) == 0);
assert(strncmp(http_req->headers[2]->name, "Accept", 255) == 0);
assert(strncmp(http_req->headers[2]->value, "*/*", 255) == 0);
assert(strncmp(http_req->headers[3]->name, "Test-Header", 255) == 0);
assert(strncmp(http_req->headers[3]->value, "🐍", 255) == 0);
assert(strncmp(http_req->headers[4]->name, "Host", 255) == 0);
assert(strncmp(http_req->headers[4]->value, "localhost:8085", 255) == 0);
assert(strncmp(http_req->headers[5]->name, "Accept-Encoding", 255) == 0);
assert(strncmp(http_req->headers[5]->value, "gzip, deflate, br", 255) == 0);
assert(strncmp(http_req->headers[6]->name, "Content-Length", 255) == 0);
assert(strncmp(http_req->headers[6]->value, "68", 255) == 0);
assert(http_req->body_len == 68);
assert(
strncmp((char *) http_req->body,
"{\n \"key1\": \"🐌\",\n \"key2\": \"value2\",\n \"key3\": \"value3\"\n}", 68) == 0);
destroy_http_request(http_req);
}

int main() {
test_request_parse_get_root_curl();
test_request_post_root_curl();
test_request_post_root_curl_with_wide_chars();

return EXIT_SUCCESS;
}

0 comments on commit 2895636

Please sign in to comment.