-
Notifications
You must be signed in to change notification settings - Fork 1
/
static_file_handler_test.cc
146 lines (105 loc) · 4.69 KB
/
static_file_handler_test.cc
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
135
136
137
138
139
140
141
142
143
144
145
#include "gtest/gtest.h"
#include "static_file_handler.h"
#include "request.h"
#include "response.h"
#include "cpp-markdown/markdown.h"
TEST(StaticFileHandlerTest, FileExists) {
int fd; int fd_config;
NginxConfig config;
NginxConfigParser parser;
char name[] = "/tmp/fileXXXXXX";
char name_config[] = "/tmp/configXXXXXX";
std::string text_contents = "foo bar\n bar foo";
std::string config_contents = "root /;";
// Creates a temporary file using name, replacing the last 6 X's
fd = mkstemp(name);
// Write the text_contents into the temp file
write(fd, text_contents.c_str(), text_contents.size());
fd_config = mkstemp(name_config);
write(fd_config, config_contents.c_str(), config_contents.size());
std::string test_string = "GET /static/" + std::string(name) +
" HTTP/1.0\r\n" +
"Host: localhost:1234\r\n" +
"\r\n";
std::unique_ptr<Request> request = Request::Parse(test_string);
EXPECT_TRUE(request != NULL);
bool parse_status = parser.Parse(name_config, &config);
// Delete the file
remove(name_config);
EXPECT_TRUE(parse_status);
StaticFileHandler h;
EXPECT_EQ(RequestHandler::Status::OK, h.Init("/static", config));
Response response;
EXPECT_EQ(RequestHandler::Status::OK, h.HandleRequest(*request, &response));
// Delete the file
remove(name);
using Headers = std::vector<std::pair<std::string, std::string>>;
Headers headers = response.GetHeaders();
ASSERT_EQ(2, headers.size());
EXPECT_EQ("Content-Length", response.GetHeaders()[0].first);
EXPECT_EQ(std::to_string(text_contents.size()), response.GetHeaders()[0].second);
EXPECT_EQ("Content-Type", response.GetHeaders()[1].first);
EXPECT_EQ("text/plain", response.GetHeaders()[1].second);
EXPECT_EQ(text_contents, response.GetBody());
EXPECT_EQ(Response::ResponseCode::ok, response.GetStatus());
}
TEST(StaticFileHandlerTest, NoFileExists) {
int fd; int fd_config;
NginxConfig config;
NginxConfigParser parser;
char name_config[] = "/tmp/configXXXXXX";
std::string config_contents = "root /;";
fd_config = mkstemp(name_config);
write(fd_config, config_contents.c_str(), config_contents.size());
std::string test_string = "GET /static/NOTEXIST"
" HTTP/1.0\r\n"
"Host: localhost:1234\r\n"
"\r\n";
std::unique_ptr<Request> request = Request::Parse(test_string);
EXPECT_TRUE(request != NULL);
bool parse_status = parser.Parse(name_config, &config);
// Delete the file
remove(name_config);
EXPECT_TRUE(parse_status);
StaticFileHandler h;
EXPECT_EQ(RequestHandler::Status::OK, h.Init("/static", config));
Response response;
EXPECT_EQ(RequestHandler::Status::OK, h.HandleRequest(*request, &response));
EXPECT_EQ(Response::ResponseCode::not_found, response.GetStatus());
}
TEST(StaticFileHandlerTest, LuaScriptedHTML) {
int fd_config;
NginxConfig config;
NginxConfigParser parser;
char name_config[] = "/tmp/configXXXXXX";
std::string config_contents = "root .;";
fd_config = mkstemp(name_config);
write(fd_config, config_contents.c_str(), config_contents.size());
std::string test_string = std::string() +
"GET /static/test.htm?a=b&b=c HTTP/1.0\r\n" +
"Content-Length: 17\r\n" +
"Host: localhost:1234\r\n" +
"\r\n" +
"foo=bar&test=test";
std::unique_ptr<Request> request = Request::Parse(test_string);
EXPECT_TRUE(request != NULL);
bool parse_status = parser.Parse(name_config, &config);
// Delete the file
remove(name_config);
EXPECT_TRUE(parse_status);
StaticFileHandler h;
EXPECT_EQ(RequestHandler::Status::OK, h.Init("/static", config));
Response response;
EXPECT_EQ(RequestHandler::Status::OK, h.HandleRequest(*request, &response));
using Headers = std::vector<std::pair<std::string, std::string> >;
Headers headers = response.GetHeaders();
ASSERT_EQ(3, headers.size());
EXPECT_EQ("Foo", response.GetHeaders()[0].first);
EXPECT_EQ("bar", response.GetHeaders()[0].second);
EXPECT_EQ("Content-Length", response.GetHeaders()[1].first);
EXPECT_EQ("26", response.GetHeaders()[1].second);
EXPECT_EQ("Content-Type", response.GetHeaders()[2].first);
EXPECT_EQ("text/html", response.GetHeaders()[2].second);
EXPECT_EQ(Response::ResponseCode::accepted, response.GetStatus());
EXPECT_EQ("hello, world\nhello, world\n", response.GetBody());
}