diff --git a/src/http/HttpRequest.cpp b/src/http/HttpRequest.cpp index 41b37ede..1f640fd4 100644 --- a/src/http/HttpRequest.cpp +++ b/src/http/HttpRequest.cpp @@ -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; } diff --git a/src/parser/ScannerBuffer.cpp b/src/parser/ScannerBuffer.cpp index 72f08a78..1912ad21 100644 --- a/src/parser/ScannerBuffer.cpp +++ b/src/parser/ScannerBuffer.cpp @@ -43,18 +43,12 @@ void parser::ScannerBuffer::putback(char c) _buffer.push_front(c); } -#include - 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() diff --git a/tests/src/TestsHttpRequest.cpp b/tests/src/TestsHttpRequest.cpp index 9fce3ef1..9226f03c 100644 --- a/tests/src/TestsHttpRequest.cpp +++ b/tests/src/TestsHttpRequest.cpp @@ -179,7 +179,7 @@ TEST_CASE( "HttpRequest::read simple get, but cut in two read", "[class][HttpReq std::string data((std::istreambuf_iterator(file)), std::istreambuf_iterator()); - 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); @@ -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(file)), + std::istreambuf_iterator()); + + 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()); + } + } + +}