#ifndef REQUEST_TEST #define REQUEST_TEST #include "munit/munit.h" #ifndef READLINE_C #define READLINE_C #include "../src/readline.h" #endif #include "../src/request.h" #ifndef REQUESTRESPONSE_C #define REQUESTRESPONSE_C #include "../src/requestresponse.h" #endif /* ifndef REQUESTRESPONSE_C */ typedef struct { char *fullLine; char *method; char *host; int port; char *protocol; double version; char *path; char *queryString; } requestTestFirstLine; static requestTestFirstLine requestLine1Examples[] = { //Full Line, Method Host Port Protocol Version Path Query string { "GET /search?q=test HTTP/2", "GET", "", 80, "http", 2, "/search", "?q=test"}, { "GET / HTTP/2", "GET", "", 80, "http", 2, "/", ""}, { "POST http://example.com/test HTTP/1.1", "POST", "example.com", 80, "http", 1.1, "/test", "" }, { "CONNECT example.com:443 HTTP/1.1", "CONNECT", "example.com", 443, "https", 1.1, "", "" }, { "POST https://example.com/test/test2/test3 HTTP/1.1", "POST", "example.com", 443, "https", 1.1, "/test/test2/test3", ""}, { "POST https://example.com:4444/test/ HTTP/1.1", "POST", "example.com", 4444, "https", 1.1, "/test/", ""}, { NULL, NULL, NULL, 80, NULL, 0, NULL, NULL } }; MunitResult testFirstLineProtocols(const MunitParameter params[], void* user_data_or_fixture){ Request *req; const char *firstLine = munit_parameters_get(params, "L1" ); requestTestFirstLine *line = requestLine1Examples; while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 ) line++; if ( line->fullLine == NULL ) return MUNIT_ERROR; req = newRequest(); requestFirstLine( req, line->fullLine ); munit_assert_not_null( req->protocol ); 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; const char *firstLine = munit_parameters_get(params, "L1" ); requestTestFirstLine *line = requestLine1Examples; while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 ) line++; if ( line->fullLine == NULL ) return MUNIT_ERROR; req = newRequest(); requestFirstLine( req, line->fullLine ); munit_assert_not_null( req->method ); 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; const char *firstLine = munit_parameters_get(params, "L1" ); requestTestFirstLine *line = requestLine1Examples; while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 ) line++; if ( line->fullLine == NULL ) return MUNIT_ERROR; req = newRequest(); requestFirstLine( req, line->fullLine ); munit_assert_not_null( req->host ); munit_assert_string_equal( req->host, line->host ); free( req ); return MUNIT_OK; } MunitResult testFirstLinePorts(const MunitParameter params[], void* user_data_or_fixture){ Request *req; const char *firstLine = munit_parameters_get(params, "L1" ); requestTestFirstLine *line = requestLine1Examples; while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 ) line++; if ( line->fullLine == NULL ) return MUNIT_ERROR; req = newRequest(); requestFirstLine( req, line->fullLine ); munit_assert_int( req->port, ==, line->port ); free( req ); return MUNIT_OK; } MunitResult testFirstLinePaths(const MunitParameter params[], void* user_data_or_fixture){ Request *req; const char *firstLine = munit_parameters_get(params, "L1" ); requestTestFirstLine *line = requestLine1Examples; while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 ) line++; if ( line->fullLine == NULL ) return MUNIT_ERROR; req = newRequest(); requestFirstLine( req, line->fullLine ); munit_assert_not_null( req->path ); 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; const char *firstLine = munit_parameters_get(params, "L1" ); requestTestFirstLine *line = requestLine1Examples; while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 ) line++; if ( line->fullLine == NULL ) return MUNIT_ERROR; 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; const char *firstLine = munit_parameters_get(params, "L1" ); requestTestFirstLine *line = requestLine1Examples; while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 ) line++; if ( line->fullLine == NULL ) return MUNIT_ERROR; req = newRequest(); requestFirstLine( req, line->fullLine ); munit_assert_not_null( req->queryString ); 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 ), "GET /search?q=test HTTP/1.1\r\n\r\n" ); requestAddHeader( req, "Host: example.com" ); munit_assert_string_equal( requestToString( req ), "GET /search?q=test HTTP/1.1\r\nHost: example.com\r\n\r\n" ); return MUNIT_OK; } MunitParameterEnum test_first_line_params[2] = {NULL, NULL}; static MunitTest request_tests[] = { { "/line1/versions", /* name */ testFirstLineVersions, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ test_first_line_params /* parameters */ }, { "/line1/methods", /* name */ testFirstLineMethod, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ test_first_line_params /* parameters */ },{ "/line1/protocols", /* name */ testFirstLineProtocols, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ test_first_line_params /* parameters */ },{ "/line1/hosts", /* name */ testFirstLineHosts, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ test_first_line_params /* parameters */ },{ "/line1/ports", /* name */ testFirstLinePorts, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ test_first_line_params /* parameters */ },{ "/line1/paths", /* name */ testFirstLinePaths, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ test_first_line_params /* parameters */ },{ "/line1/queryStrings", /* name */ testFirstLineQueryString, /* test */ NULL, /* setup */ NULL, /* tear_down */ MUNIT_TEST_OPTION_NONE, /* options */ test_first_line_params /* 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[]) { static char** firstLines; unsigned int count = 1; requestTestFirstLine *current = requestLine1Examples; while ( current->fullLine != NULL ){ count++; current++; } printf("There are %i request lines to test", count); firstLines = malloc( sizeof( char * ) * ( count ) ); for( unsigned int i = 0; i < count; i++ ){ firstLines[i] = requestLine1Examples[i].fullLine; } test_first_line_params[0].name = "L1"; test_first_line_params[0].values = firstLines; return munit_suite_main(&request_test_suite, NULL, argc, argv); } #endif /* ifndef MAINTEST */ #endif /* ifndef REQUEST_TEST */