You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
4.5 KiB
167 lines
4.5 KiB
#ifndef REQUEST_TEST |
|
#define REQUEST_TEST |
|
|
|
#include "munit/munit.h" |
|
#include "../src/readline.c" |
|
#include "../src/request.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; |
|
} |
|
|
|
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 */ |
|
}, |
|
/* 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 */
|
|
|