@ -1,31 +1,41 @@
# ifndef REQUEST_TEST
# define REQUEST_TEST
# include "munit/munit.h"
# include "../src/readline.c"
# include "../src/request.c"
# ifndef READLINE_C
# define READLINE_C
# include "../src/readline.h"
# endif
# include "../src/request.h"
# ifndef REQUESTRESPONSE_C
# define REQUESTRESPONSE_C
# include "../src/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 ;
} f irstLine;
} requestTestF irstLine;
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 }
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 }
} ;
@ -34,72 +44,133 @@ static firstLine line1Examples[] = {
MunitResult testFirstLineProtocols ( const MunitParameter params [ ] ,
void * user_data_or_fixture ) {
Request * req ;
for ( firstLine * line = line1Examples ; line - > fullLine ! = NULL ; line + + ) {
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 ;
for ( firstLine * line = line1Examples ; line - > fullLine ! = NULL ; line + + ) {
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 ;
for ( firstLine * line = line1Examples ; line - > fullLine ! = NULL ; line + + ) {
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 ;
for ( firstLine * line = line1Examples ; line - > fullLine ! = NULL ; line + + ) {
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 ;
for ( firstLine * line = line1Examples ; line - > fullLine ! = NULL ; line + + ) {
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 ;
for ( firstLine * line = line1Examples ; line - > fullLine ! = NULL ; line + + ) {
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 ;
}
@ -118,13 +189,15 @@ 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 " ) ;
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 , 0 ) , " GET /search?q=test HTTP/1.1 \r \n Host: example.com \r \n " ) ;
munit_assert_string_equal ( requestToString ( req ) , " GET /search?q=test HTTP/1.1 \r \n Host: example.com \r \n \r \n " ) ;
return MUNIT_OK ;
}
MunitParameterEnum test_first_line_params [ 2 ] = { NULL , NULL } ;
static MunitTest request_tests [ ] = {
{
" /line1/versions " , /* name */
@ -132,42 +205,49 @@ static MunitTest request_tests[] = {
NULL , /* setup */
NULL , /* tear_down */
MUNIT_TEST_OPTION_NONE , /* options */
NULL /* parameters */
test_first_line_params /* parameters */
} , {
" /line1/methods " , /* name */
testFirstLineMethod , /* test */
NULL , /* setup */
NULL , /* tear_down */
MUNIT_TEST_OPTION_NONE , /* options */
NULL /* parameters */
test_first_line_params /* parameters */
} , {
" /line1/protocols " , /* name */
testFirstLineProtocols , /* test */
NULL , /* setup */
NULL , /* tear_down */
MUNIT_TEST_OPTION_NONE , /* options */
NULL /* parameters */
test_first_line_params /* parameters */
} , {
" /line1/hosts " , /* name */
testFirstLineHosts , /* test */
NULL , /* setup */
NULL , /* tear_down */
MUNIT_TEST_OPTION_NONE , /* options */
NULL /* parameters */
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 */
NULL /* parameters */
test_first_line_params /* parameters */
} , {
" /line1/queryStrings " , /* name */
testFirstLineQueryString , /* test */
NULL , /* setup */
NULL , /* tear_down */
MUNIT_TEST_OPTION_NONE , /* options */
NULL /* parameters */
test_first_line_params /* parameters */
} , {
" /headers/add " , /* name */
testRequestAddHeader , /* test */
@ -200,6 +280,24 @@ MunitSuite request_test_suite = {
# 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 ) ;
}