Moves some logic out of proxy and into main

Also fixes some segfaults caused by trying to free memory that wasn't
allocated with strdup or malloc etc.

Fixes some tests
This commit is contained in:
Jonathan Hodgson 2022-01-19 12:56:11 +00:00
parent 8a5bfe9b36
commit 6eaad263be
10 changed files with 162 additions and 121 deletions

View file

@ -2,7 +2,7 @@
#define CONFIG_TEST
#include "munit/munit.h"
#include "../src/config.c"
#include "../src/config.h"
#include <unistd.h> //This has getcwd
#include <stdlib.h> //This has getenv

View file

@ -4,15 +4,9 @@
#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 {
@ -38,19 +32,25 @@ static requestTestFirstLine requestLine1Examples[] = {
{ NULL, NULL, NULL, 80, NULL, 0, NULL, NULL }
};
MunitResult testFirstLineProtocols(const MunitParameter params[],
void* user_data_or_fixture){
Request *req;
requestTestFirstLine* getLineObj( const MunitParameter params[] ){
const char *firstLine = munit_parameters_get(params, "L1" );
requestTestFirstLine *line = requestLine1Examples;
while ( line->fullLine != NULL && strcmp( line->fullLine, firstLine ) != 0 )
line++;
return line;
}
MunitResult testFirstLineProtocols(const MunitParameter params[],
void* user_data_or_fixture){
Request *req;
requestTestFirstLine *line = getLineObj(params);
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_not_null( req->protocol );
@ -62,12 +62,7 @@ MunitResult testFirstLineProtocols(const MunitParameter params[],
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++;
requestTestFirstLine *line = getLineObj(params);
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
@ -80,12 +75,7 @@ MunitResult testFirstLineMethod(const MunitParameter params[],
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++;
requestTestFirstLine *line = getLineObj(params);
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
@ -98,12 +88,7 @@ MunitResult testFirstLineHosts(const MunitParameter params[],
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++;
requestTestFirstLine *line = getLineObj(params);
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
@ -115,15 +100,8 @@ MunitResult testFirstLinePorts(const MunitParameter params[],
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++;
requestTestFirstLine *line = getLineObj(params);
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_not_null( req->path );
@ -136,15 +114,8 @@ 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++;
requestTestFirstLine *line = getLineObj(params);
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_float( req->version, ==, line->version );
@ -156,16 +127,8 @@ MunitResult testFirstLineVersions(const MunitParameter params[],
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++;
requestTestFirstLine *line = getLineObj(params);
if ( line->fullLine == NULL ) return MUNIT_ERROR;
req = newRequest();
requestFirstLine( req, line->fullLine );
munit_assert_not_null( req->queryString );

View file

@ -2,7 +2,7 @@
#define REQUESTRESPONSE_TEST
#include "munit/munit.h"
#include "../src/requestresponse.c"
#include "../src/requestresponse.h"
typedef struct {
char *fullLine;

View file

@ -12,15 +12,9 @@
#include <stdbool.h>
#include "munit/munit.h"
#ifndef READLINE_C
#define READLINE_C
#include "../src/readline.c"
#endif
#ifndef REQUESTRESPONSE_C
#define REQUESTRESPONSE_C
#include "../src/requestresponse.c"
#endif /* ifndef REQUESTRESPONSE_C */
#include "../src/response.c"
#include "../src/readline.h"
#include "../src/requestresponse.h"
#include "../src/response.h"
typedef struct {
char *fullLine;
@ -191,6 +185,20 @@ MunitResult testResponseFromSocketBody(const MunitParameter params[],
return MUNIT_OK;
}
MunitResult testConectionEstablished(const MunitParameter params[],
void* user_data_or_fixture){
Response *rsp = newResponse();
connectionEstablished(rsp);
munit_assert_string_equal(
responseToString( rsp ),
"HTTP/1.1 200 Connection Established\r\n\r\n"
);
return MUNIT_OK;
}
static MunitTest response_tests[] = {
{
"/new/status", /* name */
@ -227,6 +235,13 @@ static MunitTest response_tests[] = {
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
}, {
"/to/string/connectionEstablished", /* name */
testConectionEstablished, /* test */
NULL, /* setup */
NULL, /* tear_down */
MUNIT_TEST_OPTION_NONE, /* options */
NULL /* parameters */
}, {
"/line1/statusCode", /* name */
testResponseFirstLineStatusCode, /* test */