#ifndef REQUEST_TEST #define REQUEST_TEST #include "munit/munit.h" #include "../src/readline.c" #include "../src/request.c" #ifndef REQUESTRESPONSE_C #define REQUESTRESPONSE_C #include "../src/requestresponse.c" #endif /* ifndef REQUESTRESPONSE_C */ typedef struct { char *fullLine; char *method; char *host; char *protocol; double version; char *path; char *queryString; } firstLine; static firstLine line1Examples[] = { //Full Line, Method Host Protocol Version Path { "GET /search?q=test HTTP/2", "GET", "", "", 2, "/search", "?q=test"}, { "POST http://example.com/test HTTP/1.1", "POST", "example.com", "http", 1.1, "/test", "" }, { "POST https://example.com/test/test2/test3 HTTP/1.1", "POST", "example.com", "https", 1.1, "/test/test2/test3", ""}, { NULL, NULL, NULL, NULL, 0, NULL, NULL } }; MunitResult testFirstLineProtocols(const MunitParameter params[], void* user_data_or_fixture){ Request *req; for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){ req = newRequest(); requestFirstLine( req, line->fullLine ); munit_assert_string_equal( req->protocol, line->protocol ); free( req ); } return MUNIT_OK; } MunitResult testFirstLineMethod(const MunitParameter params[], void* user_data_or_fixture){ Request *req; for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){ req = newRequest(); requestFirstLine( req, line->fullLine ); munit_assert_string_equal( req->method, line->method ); free( req ); } return MUNIT_OK; } MunitResult testFirstLineHosts(const MunitParameter params[], void* user_data_or_fixture){ Request *req; for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){ req = newRequest(); requestFirstLine( req, line->fullLine ); munit_assert_string_equal( req->host, line->host ); free( req ); } return MUNIT_OK; } MunitResult testFirstLinePaths(const MunitParameter params[], void* user_data_or_fixture){ Request *req; for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){ req = newRequest(); requestFirstLine( req, line->fullLine ); munit_assert_string_equal( req->path, line->path ); free( req ); } return MUNIT_OK; } MunitResult testFirstLineVersions(const MunitParameter params[], void* user_data_or_fixture){ Request *req; for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){ req = newRequest(); requestFirstLine( req, line->fullLine ); munit_assert_float( req->version, ==, line->version ); free( req ); } return MUNIT_OK; } MunitResult testFirstLineQueryString(const MunitParameter params[], void* user_data_or_fixture){ Request *req; for ( firstLine *line = line1Examples; line->fullLine != NULL; line++ ){ req = newRequest(); requestFirstLine( req, line->fullLine ); munit_assert_string_equal( req->queryString, line->queryString ); free( req ); } return MUNIT_OK; } MunitResult testRequestAddHeader(const MunitParameter params[], void* user_data_or_fixture){ Request *req = newRequest(); munit_assert_null(req->headers); requestAddHeader(req, "Content-Length: 0"); munit_assert_int( 1, ==, countHeaders(req->headers) ); requestAddHeader(req, "Test-header: 5"); munit_assert_int( 2, ==, countHeaders(req->headers) ); return MUNIT_OK; } MunitResult testRequestToString(const MunitParameter params[], void* user_data_or_fixture){ Request *req = newRequest(); requestFirstLine( req, "GET /search?q=test HTTP/1.1" ); munit_assert_string_equal( requestToString( req, 0 ), "GET /search?q=test HTTP/1.1\r\n" ); requestAddHeader( req, "Host: example.com" ); munit_assert_string_equal( requestToString( req, 0 ), "GET /search?q=test HTTP/1.1\r\nHost: example.com\r\n" ); return MUNIT_OK; } static MunitTest request_tests[] = { { "/line1/versions", /* name */ testFirstLineVersions, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ }, { "/line1/methods", /* name */ testFirstLineMethod, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ },{ "/line1/protocols", /* name */ testFirstLineProtocols, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ },{ "/line1/hosts", /* name */ testFirstLineHosts, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ },{ "/line1/paths", /* name */ testFirstLinePaths, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ },{ "/line1/queryStrings", /* name */ testFirstLineQueryString, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ },{ "/headers/add", /* name */ testRequestAddHeader, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ NULL /* parameters */ },{ "/tostring", /* name */ testRequestToString, /* 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 request_test_suite = { "/request", /* name */ request_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(&request_test_suite, NULL, argc, argv); } #endif /* ifndef MAINTEST */ #endif /* ifndef REQUEST_TEST */