11#include < atomizes.hpp>
2- #include " catch.hpp"
2+ #include " catch.hpp"
3+
4+ TEST_CASE (" http message parsing correct" , " [http_message_parse]" )
5+ {
6+ atomizes::HTTPMessageParser parser;
7+
8+ SECTION (" parse http request" )
9+ {
10+ const std::string requestStr = " GET / HTTP/1.1\r\n Host: example.com\r\n User-Agent: Test Agent\r\n Connection: keep-alive\r\n\r\n " ;
11+
12+ atomizes::HTTPMessage request;
13+
14+ parser.Parse (&request, requestStr);
15+
16+ REQUIRE (request.HeaderCount () == 3 );
17+ REQUIRE (request.GetHeader (" Host" ) == " example.com" );
18+ REQUIRE (request.GetHeader (" User-Agent" ) == " Test Agent" );
19+ REQUIRE (request.GetHeader (" Connection" ) == " keep-alive" );
20+ REQUIRE (request.GetMethod () == atomizes::MessageMethod::GET);
21+ REQUIRE (request.GetPath () == " /" );
22+ }
23+
24+ SECTION (" parse http response without body" )
25+ {
26+ const std::string responseStr = " HTTP/1.1 200 OK\r\n Connection: close\r\n Content-Type: text/plain\r\n\r\n " ;
27+
28+ atomizes::HTTPMessage response;
29+
30+ parser.Parse (&response, responseStr);
31+
32+ REQUIRE (response.HeaderCount () == 2 );
33+ REQUIRE (response.GetHeader (" Connection" ) == " close" );
34+ REQUIRE (response.GetHeader (" Content-Type" ) == " text/plain" );
35+ REQUIRE (response.GetMessageBody ().empty ());
36+ }
37+
38+ SECTION (" parse http response with body" )
39+ {
40+ const std::string responseStr = " HTTP/1.1 200 OK\r\n Connection: close\r\n Content-Type: text/plain\r\n Content-Length: 12\r\n\r\n Hello world!" ;
41+
42+ atomizes::HTTPMessage response;
43+
44+ parser.Parse (&response, responseStr);
45+
46+ REQUIRE (response.HeaderCount () == 3 );
47+ REQUIRE (response.GetHeader (" Connection" ) == " close" );
48+ REQUIRE (response.GetHeader (" Content-Type" ) == " text/plain" );
49+ REQUIRE (!response.GetMessageBody ().empty ());
50+
51+ // convert the body to a string for easy compare
52+ std::string body (response.GetMessageBody ().begin (), response.GetMessageBody ().end ());
53+
54+ REQUIRE (body == " Hello world!" );
55+ }
56+ }
0 commit comments