Added some HTTP parser unit tests, fixed HTTP support for zero content length, added gcovr file for coverage reports

This commit is contained in:
Thulinma 2023-07-03 16:27:36 +02:00
parent 9a599a06de
commit 8b8a28c4ec
5 changed files with 89 additions and 3 deletions

64
test/http_parser.cpp Normal file
View file

@ -0,0 +1,64 @@
#include <iostream>
#include <mist/http_parser.h>
#include <mist/timing.h>
int main(int argc, char ** argv){
bool preMade = false;
Socket::Connection C(1, 0); // Open stdio by default
// If there is a T_HTTP environment variable, use that as input instead
if (getenv("T_HTTP")){
preMade = true;
// Keep stdio open, only drop the reference to it
C.drop();
// Create a pipe and reconnect the socket to it
int p[2];
if (pipe(p)){
FAIL_MSG("Could not open pipe!");
return 1;
}
C.open(p[1], p[0]);
// Write the T_HTTP env contents into the pipe
C.SendNow(getenv("T_HTTP"));
// Close the write end if we're not lingering
if (!getenv("T_LINGER")){close(p[1]);}
}
HTTP::Parser p;
int counter = 0;
C.setBlocking(false);
uint64_t lastData = Util::bootMS();
do {
if (C.spool()){
lastData = Util::bootMS();
while (p.Read(C)){
INFO_MSG("Read a HTTP message: %s %s %s (%zu bytes)", p.method.c_str(), p.url.c_str(), p.protocol.c_str(), p.body.size());
++counter;
p.Clean();
}
}else{
// premade requests will instantly time out, others after 10 seconds
if (preMade){break;}
if (Util::bootMS() > lastData + 10000){
WARN_MSG("Read timeout, aborting");
break;
}
Util::sleep(5);
}
}while(C);
while (p.Read(C)){
INFO_MSG("Read a HTTP message: %s %s %s (%zu bytes)", p.method.c_str(), p.url.c_str(), p.protocol.c_str(), p.body.size());
++counter;
p.Clean();
}
INFO_MSG("Total messages: %d", counter);
if (getenv("T_COUNT")){
if (counter != atoi(getenv("T_COUNT"))){
return 1;
}
}
return 0;
}

View file

@ -148,6 +148,21 @@ test('DTSC Sizing Test', dtsc_sizing_test)
bitwritertest = executable('bitwritertest', 'bitwriter.cpp', dependencies: libmist_dep)
test('bitWriter Test', bitwritertest)
httpparsertest = executable('httpparsertest', 'http_parser.cpp', dependencies: libmist_dep)
test('GET request for /', httpparsertest, suite: 'HTTP parser', env: {'T_HTTP':'GET / HTTP/1.1\n\n', 'T_COUNT':'1'})
test('GET request for / with carriage returns', httpparsertest, suite: 'HTTP parser', env: {'T_HTTP':'GET / HTTP/1.1\r\n\r\n', 'T_COUNT':'1'})
test('POST request to /, raw body', httpparsertest, suite: 'HTTP parser', env: {'T_HTTP':'POST / HTTP/1.1\nContent-Length: 4\nContent-Type: text/plain\n\ntest', 'T_COUNT':'1'})
test('POST request to /, urlencoded body', httpparsertest, suite: 'HTTP parser', env: {'T_HTTP':'POST / HTTP/1.1\nContent-Length: 28\nContent-Type: application/x-www-form-urlencoded\n\nfoo=bar&banana=sauce&cookies', 'T_COUNT':'1'})
test('Blank HTTP response, closed connection', httpparsertest, suite: 'HTTP parser', env: {'T_HTTP':'HTTP/1.1 200 OK\nDate: Thu, 15 Jun 2023 21:34:06 GMT\nContent-Length: 0\n\n', 'T_COUNT':'1'})
test('Blank HTTP response, lingering connection', httpparsertest, suite: 'HTTP parser', env: {'T_HTTP':'HTTP/1.1 200 OK\nDate: Thu, 15 Jun 2023 21:34:06 GMT\nContent-Length: 0\n\n', 'T_LINGER':'1', 'T_COUNT':'1'})
test('Simple HTTP response, closed connection', httpparsertest, suite: 'HTTP parser', env: {'T_HTTP':'HTTP/1.1 200 OK\nDate: Thu, 15 Jun 2023 21:34:06 GMT\nContent-Length: 4\n\ntest', 'T_COUNT':'1'})
test('Simple HTTP response, lingering connection', httpparsertest, suite: 'HTTP parser', env: {'T_HTTP':'HTTP/1.1 200 OK\nDate: Thu, 15 Jun 2023 21:34:06 GMT\nContent-Length: 4\n\ntest', 'T_LINGER':'1', 'T_COUNT':'1'})
test('Simple HTTP response, no length, closed connection', httpparsertest, suite: 'HTTP parser', env: {'T_HTTP':'HTTP/1.1 200 OK\nDate: Thu, 15 Jun 2023 21:34:06 GMT\n\ntest', 'T_COUNT':'1'})
test('Simple HTTP response, no length, lingering connection', httpparsertest, suite: 'HTTP parser', env: {'T_HTTP':'HTTP/1.1 200 OK\nDate: Thu, 15 Jun 2023 21:34:06 GMT\n\ntest', 'T_LINGER':'1', 'T_COUNT':'0'})
test('Chunked HTTP response, closed connection', httpparsertest, suite: 'HTTP parser', env: {'T_HTTP':'HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n1\nt\n3\nest\n0\n\n', 'T_COUNT':'1'})
test('Chunked HTTP response, lingering connection', httpparsertest, suite: 'HTTP parser', env: {'T_HTTP':'HTTP/1.1 200 OK\nTransfer-Encoding: chunked\n\n1\nt\n3\nest\n0\n\n', 'T_LINGER':'1', 'T_COUNT':'1'})
#abst_test = executable('abst_test', 'abst_test.cpp', dependencies: libmist_dep)
#test('MP4::ABST Test', abst_test)