#ifndef REQUESTRESPONSE_TEST #define REQUESTRESPONSE_TEST #include "munit/munit.h" #include "../src/requestresponse.c" typedef struct { char *fullLine; char *name; char *value; } HeaderTestCase; //Not that some of these have trailing \r and \n in the full lines //These should be removed when turning it into a header struct static HeaderTestCase testCases[] = { { "Content-Encoding: gzip", "Content-Encoding", "gzip" }, { "Accept-Ranges: bytes", "Accept-Ranges", "bytes" }, { "Age: 186432\n", "Age", "186432" }, { "Cache-Control: max-age=604800", "Cache-Control", "max-age=604800" }, { "Content-Type: text/html; charset=UTF-8", "Content-Type", "text/html; charset=UTF-8" }, { "Date: Thu, 06 Jan 2022 18:52:13 GMT\r", "Date", "Thu, 06 Jan 2022 18:52:13 GMT" }, { "Etag: \"3147526947+ident\"", "Etag", "\"3147526947+ident\"" }, { "Expires: Thu, 13 Jan 2022 18:52:13 GMT", "Expires", "Thu, 13 Jan 2022 18:52:13 GMT" }, { "Last-Modified: Thu, 17 Oct 2019 07:18:26 GMT\r\n\r\n", "Last-Modified", "Thu, 17 Oct 2019 07:18:26 GMT" }, { "Server: ECS (nyb/1D13)", "Server", "ECS (nyb/1D13)" }, { "X-Cache: HIT", "X-Cache", "HIT" }, { "Content-Length: 648", "Content-Length", "648" }, //These are request heaedrs but should work the same way { "Host: example.com\r\n", "Host", "example.com" }, { "User-Agent: curl/7.80.0", "User-Agent", "curl/7.80.0" }, { "Accept: */*", "Accept", "*/*" }, { "Accept: */*", "Accept", "*/*" }, }; MunitResult testHeadersName(const MunitParameter params[], void* user_data_or_fixture){ for ( int i = 0; i < sizeof(testCases) / sizeof(HeaderTestCase); i++ ){ Header *test = newHeader(testCases[i].fullLine); munit_assert_string_equal( testCases[i].name, test->name ); } return MUNIT_OK; } MunitResult testHeadersValue(const MunitParameter params[], void* user_data_or_fixture){ for ( int i = 0; i < sizeof(testCases) / sizeof(HeaderTestCase); i++ ){ Header *test = newHeader(testCases[i].fullLine); munit_assert_string_equal( testCases[i].value, test->value ); } return MUNIT_OK; } MunitResult testHeadersListCount(const MunitParameter params[], void* user_data_or_fixture){ HeaderList *list = malloc( sizeof( HeaderList ) ); list->header = newHeader("Content-Length: 0"); list->next = malloc( sizeof( HeaderList ) ); list->next->header = newHeader("Test-header: 5"); list->next->next = NULL; munit_assert_int( 2, ==, countHeaders(list) ); list->next->next = malloc( sizeof( HeaderList ) ); list->next->next->header = newHeader("Test-header: 5"); list->next->next->next = NULL; munit_assert_int( 3, ==, countHeaders(list) ); return MUNIT_OK; } MunitResult testHeadersListAdd(const MunitParameter params[], void* user_data_or_fixture){ HeaderList *list = malloc( sizeof( HeaderList ) ); list->header = newHeader("Content-Length: 0"); addHeader( list, "Test-header: 5" ); munit_assert_int( 2, ==, countHeaders(list) ); addHeader( list, "Another-Test-header: 50" ); munit_assert_int( 3, ==, countHeaders(list) ); return MUNIT_OK; } MunitResult testHeadersListCharLength(const MunitParameter params[], void* user_data_or_fixture){ HeaderList *list = malloc( sizeof( HeaderList ) ); list->header = newHeader("Content-Length: 0"); munit_assert_int( headerListCharLength( list ) , ==, 19 ); addHeader( list, "Test-header: 5" ); munit_assert_int( headerListCharLength( list ) , ==, 35 ); addHeader( list, "Another-Test-header: 50" ); munit_assert_int( headerListCharLength( list ) , ==, 60 ); return MUNIT_OK; } MunitResult testHeadersToString(const MunitParameter params[], void* user_data_or_fixture){ HeaderList *list = malloc( sizeof( HeaderList ) ); list->header = newHeader("Content-Length: 0"); addHeader( list, "Test-header: 5" ); munit_assert_string_equal( headersToString(list), "Content-Length: 0\r\nTest-header: 5\r\n" ); return MUNIT_OK; } MunitResult testGetHeader(const MunitParameter params[], void* user_data_or_fixture){ HeaderList *list = malloc( sizeof( HeaderList ) ); list->header = newHeader("Content-Length: 0"); addHeader( list, "Test-header: 5" ); addHeader( list, "Another-Test-header: 50" ); Header *chosen = getHeader( list, "content-length" ); munit_assert_string_equal( chosen->value, "0" ); chosen = getHeader( list, "ConTENt-LenGth" ); munit_assert_string_equal( chosen->value, "0" ); chosen = getHeader( list, "test-header" ); munit_assert_string_equal( chosen->value, "5" ); chosen = getHeader( list, "another-test-header" ); munit_assert_string_equal( chosen->value, "50" ); chosen = getHeader( list, "Does-not-exist" ); munit_assert_null( chosen ); return MUNIT_OK; } static MunitTest requestresponse_tests[] = { { "/headers/name", /* name */ testHeadersName, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/headers/value", /* name */ testHeadersValue, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/headerslist/count", /* name */ testHeadersListCount, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/headerslist/add", /* name */ testHeadersListAdd, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/headerslist/charLength", /* name */ testHeadersListCharLength, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/headerslist/toString", /* name */ testHeadersToString, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/headerslist/getHeader", /* name */ testGetHeader, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, /* Mark the end of the array with an entry where the test * function is NULL */ { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } }; MunitSuite requestresponse_test_suite = { "/requestresponse", /* name */ requestresponse_tests, /* tests */ NULL, /* suites */ 1, /* iterations */ MUNIT_SUITE_OPTION_NONE /* options */ }; #ifndef MAINTEST #define MAINTEST int main (int argc, char* argv[]) { return munit_suite_main(&requestresponse_test_suite, NULL, argc, argv); } #endif /* ifndef MAINTEST */ #endif /* ifndef REQUESTRESPONSE_TEST */