增加HTTP解析层单元测试用例。

This commit is contained in:
Lu Qiuwen
2018-09-18 20:45:21 +08:00
parent 9ccc3d329e
commit 3d8498b648
4 changed files with 96 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ set(CMAKE_C_STANDARD 11)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall)
enable_testing()
add_subdirectory(vendor)
add_subdirectory(common)
add_subdirectory(plugin)

View File

@@ -1,3 +1,4 @@
### PLUGIN
add_library(http src/http_entry.cpp src/http_half.cpp)
target_include_directories(http PRIVATE include/internal)
@@ -6,3 +7,12 @@ target_include_directories(http PUBLIC incluce/external)
target_link_libraries(http common)
target_link_libraries(http http-parser-static)
target_link_libraries(http libevent-static)
### UNITTEST CASE
add_executable(test-http-half test/test_http_half.cpp)
target_include_directories(test-http-half PRIVATE include/internal)
target_link_libraries(test-http-half gtest common http)
include(GoogleTest)
gtest_discover_tests(test-http-half)

View File

@@ -0,0 +1,83 @@
#include <gtest/gtest.h>
#include <http_half.h>
static const char * __identify_http_request =
"POST /gen_204 HTTP/1.1\r\n"
"Host: www.google.com\r\n"
"Connection: close\r\n"
"Content-Length: 0\r\n"
"Origin: https://www.google.com\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36\r\n"
"Content-Type: text/plain;charset=UTF-8\r\n"
"Accept: */*\r\n"
"X-Client-Data: CJG2yQEIorbJAQjEtskBCKmdygEI2J3KAQjZncoBCKijygEY+aXKAQ==\r\n"
"Referer: https://www.google.com/\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7\r\n";
static const char * __identify_not_http_request = "MZMZ";
TEST(HttpHalfConstruct, ProtoIdentifyIsHTTP)
{
auto * hf_request = hf_private_create(TFE_HTTP_REQUEST, 1, 1);
ASSERT_TRUE(hf_request != NULL);
int ret = hf_private_parse(hf_request, (const unsigned char *) __identify_http_request,
strlen(__identify_http_request));
EXPECT_EQ(ret, 0);
}
TEST(HttpHalfConstruct, ProtoIdentifyNotHTTP)
{
auto * hf_request = hf_private_create(TFE_HTTP_REQUEST, 1, 1);
ASSERT_TRUE(hf_request != NULL);
int ret = hf_private_parse(hf_request, (const unsigned char *) __identify_not_http_request,
strlen(__identify_not_http_request));
EXPECT_EQ(ret, -1);
}
static const char * __get_http_request_01 =
"GET /gfwlist/gfwlist/master/gfwlist.txt HTTP/1.1\r\n"
"Host: raw.githubusercontent.com\r\n"
"Connection: close\r\n"
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36\r\n"
"Accept: */*\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"Accept-Language: zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7\r\n"
"If-None-Match: \"023aeae5eafc12082067c36031888adb3bafa797\"\r\n"
"\r\n";
void __get_http_request_01_verify_helper(struct http_half_private * hf_private)
{
auto * hf_public = &hf_private->hf_public;
EXPECT_EQ(hf_public->major_version, 1);
EXPECT_EQ(hf_public->minor_version, 1);
auto * hf_public_request = &hf_public->req_spec;
EXPECT_STREQ(hf_public_request->uri, "/gfwlist/gfwlist/master/gfwlist.txt");
EXPECT_STREQ(hf_public_request->url, "/gfwlist/gfwlist/master/gfwlist.txt");
EXPECT_STREQ(hf_public_request->host, "raw.githubusercontent.com");
EXPECT_EQ(hf_public_request->method, TFE_HTTP_GET);
}
TEST(HttpHalfConstruct, GetWithNoBody)
{
auto * hf_request = hf_private_create(TFE_HTTP_REQUEST, 0, 0);
ASSERT_TRUE(hf_request != NULL);
int ret =
hf_private_parse(hf_request, (const unsigned char *) __get_http_request_01, strlen(__get_http_request_01));
ASSERT_EQ(ret, 0);
__get_http_request_01_verify_helper(hf_request);
}
int main(int argc, char ** argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -112,9 +112,10 @@ file(MAKE_DIRECTORY ${INSTALL_DIR}/include)
add_library(gtest STATIC IMPORTED GLOBAL)
set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libgtest.a)
set_property(TARGET gtest PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)
set_property(TARGET gtest PROPERTY INTERFACE_LINK_LIBRARIES pthread)
add_library(gmock STATIC IMPORTED GLOBAL)
set_property(TARGET gmock PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libgtest.a)
set_property(TARGET gmock PROPERTY IMPORTED_LOCATION ${INSTALL_DIR}/lib/libgmock.a)
set_property(TARGET gmock PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${INSTALL_DIR}/include)