Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
ggjulio committed Aug 25, 2021
1 parent 693a8db commit b922ed0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 35 deletions.
5 changes: 3 additions & 2 deletions src/http/HttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,10 @@ void HttpRequest::read(const char *buffer)
this->getUri().setAuthority(this->getHeader("Host"));

char c;
size_t contentLength = this->getContentLength();
while (contentLength-- && (c = _scanner.getChar()) != -1)
size_t contentLength = this->getContentLength(); // - _content.size();
while (contentLength-- && (c = _scanner.getChar()))
_content += c;
if (_content.size() != this->getContentLength()) return ;
_isComplete = true;
}

Expand Down
6 changes: 0 additions & 6 deletions src/parser/ScannerBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,12 @@ void parser::ScannerBuffer::putback(char c)
_buffer.push_front(c);
}

#include <iostream>

void parser::ScannerBuffer::pushNewBuffer(const char *buffer)
{
std::size_t i = 0;

while (buffer[i])
_buffer.push_back(buffer[i++]);

std::cout << ">>>>>|";
std::cout << toString();
std::cout << "|<<<<<<" << std::endl;
}

std::string parser::ScannerBuffer::toString()
Expand Down
81 changes: 54 additions & 27 deletions tests/src/TestsHttpRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ TEST_CASE( "HttpRequest::read simple get, but cut in two read", "[class][HttpReq
std::string data((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());

size_t idx = data.size() - 7;
size_t idx = 75;
std::string one = data.substr(0,idx);
std::string two = data.substr(idx);
REQUIRE( one + two == data);
Expand Down Expand Up @@ -208,38 +208,65 @@ TEST_CASE( "HttpRequest::read simple get, but cut in two read", "[class][HttpReq

CHECK( req.getContent() == "Test");

req.clear();
CHECK( req.getMethod().empty() );
CHECK( req.getContentLength() == 0 );
CHECK( req.getUri().empty());

CHECK( req.getHeaders().size() == 0);
CHECK( req.getContent().empty());

}

// simple get cut in half loop
TEST_CASE( "HttpRequest::read simple get, cut in two read - loop", "[class][HttpRequest][read]" )
{
std::ifstream file;
file.open("./http_requests/simple_get", std::ifstream::in);

// for (size_t idx = 0; idx < 140; idx++) {
// DYNAMIC_SECTION( "Looped section " << idx)
// {
// std::string one = data.substr(0,idx);
// std::string two = data.substr(idx);
// REQUIRE( one + two == data);
HttpRequest req;
std::string data((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());

for (size_t idx = 0; idx < data.size(); idx++) {
DYNAMIC_SECTION( "Looped section nb: " << idx)
{
std::string one = data.substr(0,idx);
std::string two = data.substr(idx);
CHECK( one + two == data);


// req.read(one.c_str());
// CHECK( req.isComplete() == false);
// CHECK( req.getHttpErrorCode() == 0);
// req.read(two.c_str());
// CHECK( req.isComplete() == true);
req.read(one.c_str());
CHECK( req.isComplete() == false);
CHECK( req.getHttpErrorCode() == 0);
req.read(two.c_str());
CHECK( req.isComplete() == true);


// CHECK( req.getMethod() == "GET" );
// CHECK( req.getUri().getPathEtc() == "/getip");
// CHECK( req.getUri().toString() == "http://dynamicdns.park-your-domain.com:8080/getip");
CHECK( req.getMethod() == "GET" );
CHECK( req.getUri().getPathEtc() == "/getip");
CHECK( req.getUri().toString() == "http://dynamicdns.park-your-domain.com:8080/getip");

// CHECK( req.getHeaders().size() == 8);
CHECK( req.getHeaders().size() == 8);

// CHECK( req.getHeader("User-Agent") == "PostmanRuntime/7.26.10");
// CHECK( req.getHeader("Accept") == "*/*");
// CHECK( req.getHeader("Postman-Token") == "ec250329-5eb0-4d4b-8150-39f294b6aea2");
// CHECK( req.getHeader("Host") == "dynamicdns.park-your-domain.com:8080");
// CHECK( req.getHeader("Accept-Encoding") == "gzip, deflate, br");
// CHECK( req.getHeader("Connection") == "keep-alive");
// CHECK( req.getHeader("Cookie") == "ASPSESSIONIDQADTQAQR=JNJLAIGBPIMBDAJPJNIFKIEK");

// CHECK( req.getContent() == "Test");
// }
// }
CHECK( req.getHeader("User-Agent") == "PostmanRuntime/7.26.10");
CHECK( req.getHeader("Accept") == "*/*");
CHECK( req.getHeader("Postman-Token") == "ec250329-5eb0-4d4b-8150-39f294b6aea2");
CHECK( req.getHeader("Host") == "dynamicdns.park-your-domain.com:8080");
CHECK( req.getHeader("Accept-Encoding") == "gzip, deflate, br");
CHECK( req.getHeader("Connection") == "keep-alive");
CHECK( req.getHeader("Cookie") == "ASPSESSIONIDQADTQAQR=JNJLAIGBPIMBDAJPJNIFKIEK");

CHECK( req.getContent() == "Test");

req.clear();
CHECK( req.getMethod().empty() );
CHECK( req.getContentLength() == 0 );
CHECK( req.getUri().empty());

CHECK( req.getHeaders().size() == 0);
CHECK( req.getContent().empty());
}
}

}

0 comments on commit b922ed0

Please sign in to comment.