Add UT for http parser.
parent
ddfcecccc6
commit
b72820c9c5
@ -1,14 +1,32 @@
|
||||
# Unit test
|
||||
set(UT_SRCS
|
||||
base64_test.cc
|
||||
rest_service_manager_test.cc
|
||||
base64_test.cc
|
||||
http_parser_test.cc
|
||||
rest_service_manager_test.cc
|
||||
url_test.cc
|
||||
)
|
||||
|
||||
set(UT_TARGET_NAME webcc_unittest)
|
||||
|
||||
# Common libraries to link.
|
||||
set(TEST_LIBS webcc ${Boost_LIBRARIES} "${CMAKE_THREAD_LIBS_INIT}")
|
||||
|
||||
if(WEBCC_ENABLE_SSL)
|
||||
set(TEST_LIBS ${TEST_LIBS} ${OPENSSL_LIBRARIES})
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
set(TEST_LIBS ${TEST_LIBS} zlibstatic crypt32)
|
||||
else()
|
||||
set(TEST_LIBS ${TEST_LIBS} ${ZLIB_LIBRARIES})
|
||||
endif()
|
||||
|
||||
if(UNIX)
|
||||
# Add `-ldl` for Linux to avoid "undefined reference to `dlopen'".
|
||||
set(TEST_LIBS ${TEST_LIBS} ${CMAKE_DL_LIBS})
|
||||
endif()
|
||||
|
||||
add_executable(${UT_TARGET_NAME} ${UT_SRCS})
|
||||
target_link_libraries(${UT_TARGET_NAME} webcc gtest ${Boost_LIBRARIES})
|
||||
target_link_libraries(${UT_TARGET_NAME} "${CMAKE_THREAD_LIBS_INIT}")
|
||||
target_link_libraries(${UT_TARGET_NAME} webcc gtest ${TEST_LIBS})
|
||||
|
||||
add_test(${UT_TARGET_NAME} ${UT_TARGET_NAME})
|
||||
|
@ -0,0 +1,147 @@
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "webcc/http_request.h"
|
||||
#include "webcc/http_request_parser.h"
|
||||
#include "webcc/http_response.h"
|
||||
#include "webcc/http_response_parser.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// HTTP GET request parser test fixture.
|
||||
class GetRequestParserTest : public testing::Test {
|
||||
protected:
|
||||
GetRequestParserTest() : parser_(&request_) {
|
||||
}
|
||||
|
||||
void SetUp() override {
|
||||
payload_ =
|
||||
"GET /get HTTP/1.1\r\n"
|
||||
"Accept: application/json\r\n"
|
||||
"Connection: Close\r\n"
|
||||
"Host: httpbin.org\r\n\r\n";
|
||||
}
|
||||
|
||||
void CheckResult() {
|
||||
EXPECT_EQ("GET", request_.method());
|
||||
EXPECT_EQ("httpbin.org", request_.GetHeader("Host"));
|
||||
EXPECT_EQ("application/json", request_.GetHeader("Accept"));
|
||||
EXPECT_EQ("Close", request_.GetHeader("Connection"));
|
||||
|
||||
EXPECT_EQ("", request_.content());
|
||||
EXPECT_EQ(0, request_.content_length());
|
||||
}
|
||||
|
||||
std::string payload_;
|
||||
|
||||
webcc::HttpRequest request_;
|
||||
webcc::HttpRequestParser parser_;
|
||||
};
|
||||
|
||||
TEST_F(GetRequestParserTest, ParseFullDataOnce) {
|
||||
bool ok = parser_.Parse(payload_.data(), payload_.size());
|
||||
|
||||
EXPECT_TRUE(ok);
|
||||
EXPECT_TRUE(parser_.finished());
|
||||
|
||||
CheckResult();
|
||||
}
|
||||
|
||||
// Parse byte by byte.
|
||||
TEST_F(GetRequestParserTest, ParseByteWise) {
|
||||
for (std::size_t i = 0; i < payload_.size(); ++i) {
|
||||
bool ok = parser_.Parse(payload_.data() + i, 1);
|
||||
EXPECT_TRUE(ok);
|
||||
}
|
||||
|
||||
EXPECT_TRUE(parser_.finished());
|
||||
|
||||
CheckResult();
|
||||
}
|
||||
|
||||
// Parse line by line.
|
||||
TEST_F(GetRequestParserTest, ParseLineWise) {
|
||||
for (std::size_t i = 0; i < payload_.size();) {
|
||||
std::size_t j = payload_.find('\n', i);
|
||||
|
||||
if (j != std::string::npos) {
|
||||
bool ok = parser_.Parse(payload_.data() + i, j - i + 1);
|
||||
EXPECT_TRUE(ok);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
i = j + 1;
|
||||
}
|
||||
|
||||
EXPECT_TRUE(parser_.finished());
|
||||
|
||||
CheckResult();
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// HTTP POST request parser test fixture.
|
||||
class PostRequestParserTest : public testing::Test {
|
||||
protected:
|
||||
PostRequestParserTest() : parser_(&request_) {
|
||||
}
|
||||
|
||||
void SetUp() override {
|
||||
data_ =
|
||||
"{\n"
|
||||
" 'note': 'Webcc test',\n"
|
||||
" 'scopes': ['public_repo', 'repo', 'repo:status', 'user']\n"
|
||||
"}";
|
||||
|
||||
payload_ =
|
||||
"POST /authorizations HTTP/1.1\r\n"
|
||||
"Content-Type: application/json; charset=utf-8\r\n"
|
||||
"Content-Length: " + std::to_string(data_.size()) + "\r\n"
|
||||
"Accept: application/json\r\n"
|
||||
"Connection: Close\r\n"
|
||||
"Host: api.github.com\r\n\r\n";
|
||||
|
||||
payload_ += data_;
|
||||
}
|
||||
|
||||
void CheckResult() {
|
||||
EXPECT_EQ("POST", request_.method());
|
||||
EXPECT_EQ("api.github.com", request_.GetHeader("Host"));
|
||||
EXPECT_EQ("application/json", request_.GetHeader("Accept"));
|
||||
EXPECT_EQ("Close", request_.GetHeader("Connection"));
|
||||
EXPECT_EQ("application/json; charset=utf-8", request_.GetHeader("Content-Type"));
|
||||
EXPECT_EQ(std::to_string(data_.size()), request_.GetHeader("Content-Length"));
|
||||
|
||||
EXPECT_EQ(data_, request_.content());
|
||||
EXPECT_EQ(data_.size(), request_.content_length());
|
||||
}
|
||||
|
||||
std::string payload_;
|
||||
std::string data_;
|
||||
|
||||
webcc::HttpRequest request_;
|
||||
webcc::HttpRequestParser parser_;
|
||||
};
|
||||
|
||||
TEST_F(PostRequestParserTest, ParseFullDataOnce) {
|
||||
bool ok = parser_.Parse(payload_.data(), payload_.size());
|
||||
|
||||
EXPECT_TRUE(ok);
|
||||
EXPECT_TRUE(parser_.finished());
|
||||
|
||||
CheckResult();
|
||||
}
|
||||
|
||||
// Parse byte by byte.
|
||||
TEST_F(PostRequestParserTest, ParseByteWise) {
|
||||
for (std::size_t i = 0; i < payload_.size(); ++i) {
|
||||
bool ok = parser_.Parse(payload_.data() + i, 1);
|
||||
EXPECT_TRUE(ok);
|
||||
}
|
||||
|
||||
EXPECT_TRUE(parser_.finished());
|
||||
|
||||
CheckResult();
|
||||
}
|
Loading…
Reference in New Issue