-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
239 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
Oops, something went wrong.