增加HTTP解析层单元测试用例。
This commit is contained in:
83
plugin/protocol/http/test/test_http_half.cpp
Normal file
83
plugin/protocol/http/test/test_http_half.cpp
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user