Skip to content

Add http functionality #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add http parsing test
  • Loading branch information
lpcvoid committed Dec 26, 2022
commit 3df66db7987a1d8106821e93f869c530b6ae3c64
4 changes: 4 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ set(TEST_SOURCES
test_large_data_transfer.cpp
test_raii.cpp)

if (WITH_HTTP)
set(TEST_SOURCES ${TEST_SOURCES} test_http_parser.cpp)
endif()

enable_testing()
add_executable(test_libnetcpp main.cpp ${TEST_SOURCES})
target_link_libraries(test_libnetcpp ${CMAKE_THREAD_LIBS_INIT})
Expand Down
33 changes: 30 additions & 3 deletions tests/test_http_parser.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
//
// Created by lpcvoid on 26.12.22.
//
#include "../doctest/doctest/doctest.h"
#include "../src/http/http.hpp"
#include "../src/netlib.hpp"
#include <atomic>

using namespace std::chrono_literals;
extern uint16_t test_port;

TEST_CASE("Test HTTP response parser")
{
netlib::http::http_response resp;

std::string raw_response = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Content-Length: 1256\r\n"
"\r\n\r\n"
"TEST";

CHECK_FALSE(resp.from_raw_response(raw_response));
CHECK_EQ(resp.version.first, 1);
CHECK_EQ(resp.version.second, 1);
CHECK_EQ(resp.response_code, 200);
CHECK_EQ(resp.headers.size(), 2);
CHECK_EQ(resp.headers[0].first, "Content-Type");
CHECK_EQ(resp.headers[1].first, "Content-Length");
CHECK_EQ(resp.headers[0].second, " text/html; charset=UTF-8");
CHECK_EQ(resp.headers[1].second, " 1256");
CHECK_EQ(resp.body, "TEST");

}