Skip to content

Commit 3df66db

Browse files
committed
Add http parsing test
1 parent dfd93c5 commit 3df66db

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ set(TEST_SOURCES
55
test_large_data_transfer.cpp
66
test_raii.cpp)
77

8+
if (WITH_HTTP)
9+
set(TEST_SOURCES ${TEST_SOURCES} test_http_parser.cpp)
10+
endif()
11+
812
enable_testing()
913
add_executable(test_libnetcpp main.cpp ${TEST_SOURCES})
1014
target_link_libraries(test_libnetcpp ${CMAKE_THREAD_LIBS_INIT})

tests/test_http_parser.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1-
//
2-
// Created by lpcvoid on 26.12.22.
3-
//
1+
#include "../doctest/doctest/doctest.h"
2+
#include "../src/http/http.hpp"
3+
#include "../src/netlib.hpp"
4+
#include <atomic>
5+
6+
using namespace std::chrono_literals;
7+
extern uint16_t test_port;
8+
9+
TEST_CASE("Test HTTP response parser")
10+
{
11+
netlib::http::http_response resp;
12+
13+
std::string raw_response = "HTTP/1.1 200 OK\r\n"
14+
"Content-Type: text/html; charset=UTF-8\r\n"
15+
"Content-Length: 1256\r\n"
16+
"\r\n\r\n"
17+
"TEST";
18+
19+
CHECK_FALSE(resp.from_raw_response(raw_response));
20+
CHECK_EQ(resp.version.first, 1);
21+
CHECK_EQ(resp.version.second, 1);
22+
CHECK_EQ(resp.response_code, 200);
23+
CHECK_EQ(resp.headers.size(), 2);
24+
CHECK_EQ(resp.headers[0].first, "Content-Type");
25+
CHECK_EQ(resp.headers[1].first, "Content-Length");
26+
CHECK_EQ(resp.headers[0].second, " text/html; charset=UTF-8");
27+
CHECK_EQ(resp.headers[1].second, " 1256");
28+
CHECK_EQ(resp.body, "TEST");
29+
30+
}

0 commit comments

Comments
 (0)