diff --git a/src/tiny_http/tiny_http_server_lib.h b/src/tiny_http/tiny_http_server_lib.h index fa2a7ce..9146a07 100644 --- a/src/tiny_http/tiny_http_server_lib.h +++ b/src/tiny_http/tiny_http_server_lib.h @@ -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; diff --git a/test/assert_tiny_http_server_lib.c b/test/assert_tiny_http_server_lib.c index a701b9a..cd394c7 100644 --- a/test/assert_tiny_http_server_lib.c +++ b/test/assert_tiny_http_server_lib.c @@ -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; @@ -148,6 +148,57 @@ 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(); @@ -155,6 +206,8 @@ int main() { 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; }