Split out some stuff that is shared between request and response
I now have files with the infinitely imaginative names requestrespons.{c,h,test.c}.
This commit is contained in:
parent
0e2b9dae2b
commit
48e3092317
9 changed files with 533 additions and 70 deletions
|
@ -1,4 +1,8 @@
|
|||
#ifndef REQUEST_TEST
|
||||
#define REQUEST_TEST
|
||||
|
||||
#include "munit/munit.h"
|
||||
#include "../src/readline.c"
|
||||
#include "../src/request.c"
|
||||
|
||||
typedef struct {
|
||||
|
@ -160,3 +164,4 @@ int main (int argc, char* argv[]) {
|
|||
}
|
||||
|
||||
#endif /* ifndef MAINTEST */
|
||||
#endif /* ifndef REQUEST_TEST */
|
||||
|
|
186
tests/requestresponse.test.c
Normal file
186
tests/requestresponse.test.c
Normal file
|
@ -0,0 +1,186 @@
|
|||
#ifndef REQUESTRESPONSE_TEST
|
||||
#define REQUESTRESPONSE_TEST
|
||||
|
||||
#include "munit/munit.h"
|
||||
#include "../src/requestresponse.c"
|
||||
|
||||
typedef struct {
|
||||
char *fullLine;
|
||||
char *name;
|
||||
char *value;
|
||||
} HeaderTestCase;
|
||||
|
||||
static HeaderTestCase testCases[] = {
|
||||
{ "Content-Encoding: gzip", "Content-Encoding", "gzip" },
|
||||
{ "Accept-Ranges: bytes", "Accept-Ranges", "bytes" },
|
||||
{ "Age: 186432", "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", "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", "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" }
|
||||
};
|
||||
|
||||
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 */
|
109
tests/response.test.c
Normal file
109
tests/response.test.c
Normal file
|
@ -0,0 +1,109 @@
|
|||
#ifndef RESPONSE_TEST
|
||||
#define RESPONSE_TEST value
|
||||
|
||||
#include "munit/munit.h"
|
||||
#ifndef REQUESTRESPONSE_C
|
||||
#define REQUESTRESPONSE_C value
|
||||
#include "requestresponse.test.c"
|
||||
#endif /* ifndef REQUESTRESPONSE_C */
|
||||
#include "../src/response.c"
|
||||
|
||||
|
||||
MunitResult testResponseNewStatus(const MunitParameter params[],
|
||||
void* user_data_or_fixture){
|
||||
Response *rsp = newResponse();
|
||||
munit_assert_int( rsp->statusCode, ==, 200 );
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
MunitResult testResponseNewStatusMessage(const MunitParameter params[],
|
||||
void* user_data_or_fixture){
|
||||
Response *rsp = newResponse();
|
||||
munit_assert_string_equal( rsp->statusMessage, "OK" );
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
MunitResult testResponseNewVersion(const MunitParameter params[],
|
||||
void* user_data_or_fixture){
|
||||
Response *rsp = newResponse();
|
||||
munit_assert_float( rsp->version, ==, 1.1 );
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
MunitResult testResponseSetBody(const MunitParameter params[],
|
||||
void* user_data_or_fixture){
|
||||
Response *rsp = newResponse();
|
||||
responseSetBody( rsp, "Testing", 1 );
|
||||
munit_assert_string_equal( rsp->body, "Testing" );
|
||||
munit_assert_string_equal( getHeader( rsp->headers, "content-length" )->value, "7" );
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
MunitResult testResponseToString(const MunitParameter params[],
|
||||
void* user_data_or_fixture){
|
||||
Response *rsp = newResponse();
|
||||
responseSetBody( rsp, "Testing", 1 );
|
||||
munit_assert_string_equal( responseToString( rsp ), "HTTP/1.1 200 OK\r\nContent-Length: 7\r\nContent-Type: text/plain\r\n\r\nTesting" );
|
||||
return MUNIT_OK;
|
||||
}
|
||||
|
||||
static MunitTest response_tests[] = {
|
||||
{
|
||||
"/new/status", /* name */
|
||||
testResponseNewStatus, /* test */
|
||||
NULL, /* setup */
|
||||
NULL, /* tear_down */
|
||||
MUNIT_TEST_OPTION_NONE, /* options */
|
||||
NULL /* parameters */
|
||||
}, {
|
||||
"/new/statusMessage", /* name */
|
||||
testResponseNewStatusMessage, /* test */
|
||||
NULL, /* setup */
|
||||
NULL, /* tear_down */
|
||||
MUNIT_TEST_OPTION_NONE, /* options */
|
||||
NULL /* parameters */
|
||||
}, {
|
||||
"/new/version", /* name */
|
||||
testResponseNewVersion, /* test */
|
||||
NULL, /* setup */
|
||||
NULL, /* tear_down */
|
||||
MUNIT_TEST_OPTION_NONE, /* options */
|
||||
NULL /* parameters */
|
||||
}, {
|
||||
"/set/body", /* name */
|
||||
testResponseSetBody, /* test */
|
||||
NULL, /* setup */
|
||||
NULL, /* tear_down */
|
||||
MUNIT_TEST_OPTION_NONE, /* options */
|
||||
NULL /* parameters */
|
||||
}, {
|
||||
"/to/string", /* name */
|
||||
testResponseToString, /* 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 response_test_suite = {
|
||||
"/response", /* name */
|
||||
response_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(&response_test_suite, NULL, argc, argv);
|
||||
}
|
||||
|
||||
#endif /* ifndef MAINTEST */
|
||||
|
||||
#endif /* ifndef RESPONSE_TEST */
|
Loading…
Add table
Add a link
Reference in a new issue