Skip to content

Commit

Permalink
Update tests for new errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacquwes committed Sep 16, 2024
1 parent 09de62d commit 7a6b313
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 397 deletions.
7 changes: 3 additions & 4 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ add_executable(unit_tests)

target_sources(unit_tests
PRIVATE
"http_request_test.cpp"
"http_response_test.cpp"
"http_utils_test.cpp"
"thread_pool_test.cpp"
"http_request_tests.cpp"
"http_response_tests.cpp"
"http_tests.cpp"
"unit_tests.cpp"
)

Expand Down
96 changes: 0 additions & 96 deletions tests/unit/http_request_test.cpp

This file was deleted.

80 changes: 80 additions & 0 deletions tests/unit/http_request_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include <gtest/gtest.h>
#include "error.h"
#include "http_request.h"

TEST(HttpRequestTests, DefaultConstructor)
{
pine::http_request request;
EXPECT_EQ(pine::http_method::get, request.get_method());
EXPECT_EQ("", request.get_uri());
EXPECT_EQ(pine::http_version::http_1_1, request.get_version());
EXPECT_TRUE(request.get_headers().empty());
EXPECT_EQ("", request.get_body());
}

TEST(HttpRequestTests, ParameterizedConstructor)
{
pine::http_request request(pine::http_method::post, "/api/users", pine::http_version::http_1_1,
{{"Content-Type", "application/json"}, {"Authorization", "Bearer token"}},
R"({"name": "John Doe", "age": 30})");

EXPECT_EQ(pine::http_method::post, request.get_method());
EXPECT_EQ("/api/users", request.get_uri());
EXPECT_EQ(pine::http_version::http_1_1, request.get_version());
EXPECT_EQ(2, request.get_headers().size());
EXPECT_EQ("application/json", request.get_header("Content-Type"));
EXPECT_EQ("Bearer token", request.get_header("Authorization"));
EXPECT_EQ(R"({"name": "John Doe", "age": 30})", request.get_body());
}

TEST(HttpRequestTests, SettersAndGetters)
{
pine::http_request request;

request.set_method(pine::http_method::post);
request.set_uri("/api/products");
request.set_version(pine::http_version::http_1_1);
request.set_header("Content-Type", "application/xml");
request.set_body("<product><name>Widget</name><price>9.99</price></product>");

EXPECT_EQ(pine::http_method::post, request.get_method());
EXPECT_EQ("/api/products", request.get_uri());
EXPECT_EQ(pine::http_version::http_1_1, request.get_version());
EXPECT_EQ(1, request.get_headers().size());
EXPECT_EQ("application/xml", request.get_header("Content-Type"));
EXPECT_EQ("<product><name>Widget</name><price>9.99</price></product>", request.get_body());
}

TEST(HttpRequestTests, ParseValidRequest)
{
std::string requestStr = "GET /api/users HTTP/1.1\r\n"
"Host: example.com\r\n"
"User-Agent: Mozilla/5.0\r\n"
"Accept: application/json\r\n"
"\r\n";

auto result = pine::http_request::parse(requestStr);
ASSERT_TRUE(result.has_value());

pine::http_request request = result.value();

EXPECT_EQ(pine::http_method::get, request.get_method());
EXPECT_EQ("/api/users", request.get_uri());
EXPECT_EQ(pine::http_version::http_1_1, request.get_version());
EXPECT_EQ(3, request.get_headers().size());
EXPECT_EQ("example.com", request.get_header("Host"));
EXPECT_EQ("Mozilla/5.0", request.get_header("User-Agent"));
EXPECT_EQ("application/json", request.get_header("Accept"));
EXPECT_EQ("", request.get_body());
}

TEST(HttpRequestTests, ParseInvalidRequest)
{
std::string requestStr = "INVALID REQUEST";

auto result = pine::http_request::parse(requestStr);
ASSERT_FALSE(result.has_value());

std::error_code error = result.error();
EXPECT_EQ(pine::make_error_code(pine::error::parse_error_method), error);
}
89 changes: 0 additions & 89 deletions tests/unit/http_response_test.cpp

This file was deleted.

65 changes: 65 additions & 0 deletions tests/unit/http_response_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <gtest/gtest.h>
#include "http_response.h"

using namespace pine;

TEST(HttpResponseTests, GetBody_ReturnsCorrectValue)
{
http_response response;
response.set_body("Hello, World!");

EXPECT_EQ("Hello, World!", response.get_body());
}

TEST(HttpResponseTests, GetHeader_ReturnsCorrectValue)
{
http_response response;
response.set_header("Content-Type", "application/json");

EXPECT_EQ("application/json", response.get_header("Content-Type"));
}

TEST(HttpResponseTests, GetHeaders_ReturnsCorrectValue)
{
http_response response;
response.set_header("Content-Type", "application/json");
response.set_header("Content-Length", "100");

const auto& headers = response.get_headers();

EXPECT_EQ("application/json", headers.at("Content-Type"));
EXPECT_EQ("100", headers.at("Content-Length"));
}

TEST(HttpResponseTests, GetStatus_ReturnsCorrectValue)
{
http_response response;
response.set_status(http_status::ok);

EXPECT_EQ(http_status::ok, response.get_status());
}

TEST(HttpResponseTests, GetVersion_ReturnsCorrectValue)
{
http_response response;
response.set_version(http_version::http_1_1);

EXPECT_EQ(http_version::http_1_1, response.get_version());
}

TEST(HttpResponseTests, ToString_ReturnsCorrectValue)
{
http_response response;
response.set_status(http_status::ok);
response.set_version(http_version::http_1_1);
response.set_header("Content-Type", "application/json");
response.set_body("Hello, World!");

std::string expected = "HTTP/1.1 200 OK\r\n";
expected += "Content-Length: 13\r\n";
expected += "Content-Type: application/json\r\n";
expected += "\r\n";
expected += "Hello, World!";

EXPECT_EQ(expected, response.to_string());
}
Loading

0 comments on commit 7a6b313

Please sign in to comment.