-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrequest_handler_static_test.cpp
134 lines (115 loc) · 4.53 KB
/
request_handler_static_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include <boost/asio.hpp>
#include "request_handler_static.h"
namespace http {
namespace server {
TEST(StaticHandlerTest, InitTest)
{
RequestHandler* staticHandler = RequestHandler::CreateByName("StaticHandler");
NginxConfigParser config_parser;
NginxConfig config;
config_parser.Parse("config", &config);
NginxConfig child;
for (const auto& statement : config.statements_) {
for (unsigned int i = 0; i < statement->tokens_.size(); ++i) {
if (statement->tokens_[i] == "port")
;
else if (statement->tokens_[i]=="path" && statement->tokens_[i+1]=="/echo")
;
else if (statement->tokens_[i]=="path" && statement->tokens_[i+1]=="/status")
;
else if (statement->tokens_[i]=="path")
child = *(statement->child_block_);
}
}
RequestHandler::Status status = staticHandler->Init("/", child);
EXPECT_EQ(status, RequestHandler::Status::OK);
}
TEST(StaticHandlerTest, HandleBadRequestTest)
{
RequestHandler* staticHandler = RequestHandler::CreateByName("StaticHandler");
NginxConfigParser config_parser;
NginxConfig config;
config_parser.Parse("config", &config);
NginxConfig child;
for (const auto& statement : config.statements_) {
for (unsigned int i = 0; i < statement->tokens_.size(); ++i) {
if (statement->tokens_[i] == "port")
;
else if (statement->tokens_[i]=="path" && statement->tokens_[i+1]=="/echo")
;
else if (statement->tokens_[i]=="path" && statement->tokens_[i+1]=="/status")
;
else if (statement->tokens_[i]=="path")
child = *(statement->child_block_);
}
}
staticHandler->Init("/", child);
Request request;
Response response;
RequestHandler::Status status = staticHandler->HandleRequest(request, &response);
EXPECT_EQ(status, RequestHandler::Status::BAD_REQUEST);
}
TEST(StaticHandlerTest, HandleOkRequestTest)
{
RequestHandler* staticHandler = RequestHandler::CreateByName("StaticHandler");
NginxConfigParser config_parser;
NginxConfig config;
config_parser.Parse("config", &config);
NginxConfig child;
for (const auto& statement : config.statements_) {
if (statement->tokens_.size() == 3 && statement->tokens_[0] == "path"
&& statement->tokens_[2] == "StaticHandler") {
child = *(statement->child_block_);
}
}
staticHandler->Init("/", child);
std::string raw_request = "GET / HTTP/1.1\r\n"
"Host: localhost:2020\r\n"
"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0\r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
"Accept-Language: en-US,en;q=0.5\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"Connection: keep-alive\r\n\r\n";
std::unique_ptr<Request> request = Request::Parse(raw_request);
Request req = *request;
Response response;
RequestHandler::Status status = staticHandler->HandleRequest(req, &response);
EXPECT_EQ(status, RequestHandler::Status::OK);
}
TEST(StaticHandlerTest, HandleNotFoundRequestTest)
{
RequestHandler* staticHandler = RequestHandler::CreateByName("StaticHandler");
NginxConfigParser config_parser;
NginxConfig config;
config_parser.Parse("config", &config);
NginxConfig child;
for (const auto& statement : config.statements_) {
for (unsigned int i = 0; i < statement->tokens_.size(); ++i) {
if (statement->tokens_[i] == "port")
;
else if (statement->tokens_[i]=="path" && statement->tokens_[i+1]=="/echo")
;
else if (statement->tokens_[i]=="path" && statement->tokens_[i+1]=="/status")
;
else if (statement->tokens_[i]=="path")
child = *(statement->child_block_);
}
}
staticHandler->Init("/", child);
std::string raw_request = "GET /sddfsdf HTTP/1.1\r\n"
"Host: localhost:2020\r\n"
"User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0\r\n"
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"
"Accept-Language: en-US,en;q=0.5\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"Connection: keep-alive\r\n\r\n";
std::unique_ptr<Request> request = Request::Parse(raw_request);
Request req = *request;
Response response;
RequestHandler::Status status = staticHandler->HandleRequest(req, &response);
EXPECT_EQ(status, RequestHandler::Status::NOT_FOUND);
}
}
}