Skip to content

Commit f01a02b

Browse files
committed
Added method to get amount of headers in message
In addition, added tests for the atomizes message parser
1 parent d3be38c commit f01a02b

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

include/atomizes.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,14 @@ namespace atomizes
481481
return m_body.size();
482482
}
483483

484+
/**
485+
* Return the amount of headers in the message.
486+
*/
487+
inline size_t HeaderCount()
488+
{
489+
return m_headers.size();
490+
}
491+
484492
private:
485493
/**
486494
* The HTTP method for this message.

tests/parser_tests.cpp

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,56 @@
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\nHost: example.com\r\nUser-Agent: Test Agent\r\nConnection: 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\nConnection: close\r\nContent-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\nConnection: close\r\nContent-Type: text/plain\r\nContent-Length: 12\r\n\r\nHello 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

Comments
 (0)