Skip to content

Commit

Permalink
Update status_code to uint16_t and add HTTP response tests
Browse files Browse the repository at this point in the history
Change status_code type from uint8_t to uint16_t for better range handling. Add tests for rendering HTTP 404 response with no body and 200 response with a JSON body to ensure proper functionality.
  • Loading branch information
SeriousSamV committed Nov 1, 2024
1 parent 581ab2b commit b831336
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/tiny_http/tiny_http_server_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ typedef struct http_request {

typedef struct http_response {
http_version version;
uint8_t status_code;
uint16_t status_code;
uint8_t *reason_phrase;
http_header *headers;
size_t headers_cnt;
Expand Down
55 changes: 54 additions & 1 deletion test/assert_tiny_http_server_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void test_response_render_200_no_body(void) {
const http_response response = {
.version = HTTP_1_0,
.status_code = 200,
.reason_phrase = "OK",
.reason_phrase = (uint8_t *) "OK",
};
uint8_t *response_octets = nullptr;
size_t response_octets_len = 0;
Expand All @@ -148,13 +148,66 @@ void test_response_render_200_no_body(void) {
assert(strncmp((char *) response_octets, "HTTP/1.0 200 OK\r\n", 32) == 0);
}

void test_response_render_404_no_body(void) {
const http_response response = {
.version = HTTP_1_0,
.status_code = 404,
.reason_phrase = (uint8_t *) "Not Found",
};
uint8_t *response_octets = nullptr;
size_t response_octets_len = 0;
const enum render_http_response_status response_code = render_http_response(
&response, &response_octets, &response_octets_len);
assert(response_code == RENDER_OK);
assert(response_octets_len > 0);
assert(strncmp((char *) response_octets, "HTTP/1.0 404 Not Found\r\n", 32) == 0);
}

void test_response_render_200_with_body(void) {
{
http_response response = {
.version = HTTP_1_0,
.status_code = 200,
.reason_phrase = (uint8_t *) "OK",
.headers_cnt = 2,
.body = (uint8_t *) "{\n \"key1\": \"value1\",\n \"key2\": \"value2\"\n}",
.body_len = 48
};
response.headers = malloc(sizeof(struct http_header) * response.headers_cnt);
response.headers[0].name = "Content-Type";
response.headers[0].value = "application/json";
response.headers[1].name = "Content-Length";
response.headers[1].value = "48";

uint8_t *response_octets = nullptr;
size_t response_octets_len = 0;

const enum render_http_response_status response_code = render_http_response(
&response, &response_octets, &response_octets_len);

assert(response_code == RENDER_OK);
assert(response_octets_len > 0);

const char *expected_response = "HTTP/1.0 200 OK\r\n"
"Content-Type: application/json\r\n"
"Content-Length: 48\r\n"
"\r\n"
"{\n \"key1\": \"value1\",\n \"key2\": \"value2\"\n}";
assert(strncmp((char *)response_octets, expected_response, response_octets_len) == 0);
assert(strnlen((char *) response_octets, response_octets_len + 8) == response_octets_len);
free(response_octets);
}
}

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

test_response_render_200_no_body();
test_response_render_404_no_body();
test_response_render_200_with_body();

return EXIT_SUCCESS;
}

0 comments on commit b831336

Please sign in to comment.