-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver_config_parser_test.cc
115 lines (84 loc) · 3.49 KB
/
server_config_parser_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
#include "gtest/gtest.h"
#include "server_config_parser.h"
class PortParseTest : public ::testing::Test {
protected:
// Test to check if server parser can read in port correctly
// Returns true if the port is not -1
bool GetPortNumber(std::string config_contents) {
int fd;
char name[] = "/tmp/fileXXXXXX";
// Creates a temporary file using name, replacing the last 6 X's
fd = mkstemp(name);
// Write the config_contents into the temp file
write(fd, config_contents.c_str(), config_contents.size());
// Initialize server parser
NginxServerConfigParser server_parser(name);
// Get the port number
port = server_parser.ParseServerSettings(server_settings);
// Delete the temporary file
remove(name);
return (port != -1);
}
int port;
ServerSettings server_settings;
};
TEST_F(PortParseTest, ValidPortConfig) {
ASSERT_TRUE(GetPortNumber("port 8080;"));
EXPECT_EQ(8080, port);
}
TEST_F(PortParseTest, InvalidPortConfig) {
EXPECT_FALSE(GetPortNumber("pOrT 8080;"));
EXPECT_FALSE(GetPortNumber("port 80800;"));
}
class HandlerParseTest : public ::testing::Test {
protected:
// Test to check the number of handlers parsed from config file
// Returns true iff at least one handler is parsed
int GetNumberOfHandlersParsed(std::string config_contents) {
int fd;
char name[] = "/tmp/fileXXXXXX";
// Creates a temporary file using name, replacing the last 6 X's
fd = mkstemp(name);
// Write the config_contents into the temp file
write(fd, config_contents.c_str(), config_contents.size());
// Initialize server parser
NginxServerConfigParser server_parser(name);
// Parse the echo and static file request handlers from the config file
return server_parser.ParseRequestHandlers(handlers, handler_info);
}
std::map<std::string, std::unique_ptr<RequestHandler> > handlers;
std::map<std::string, std::string> handler_info;
};
TEST_F(HandlerParseTest, NoHandlersConfig) {
EXPECT_EQ(GetNumberOfHandlersParsed("no /handlers;"), 1);
EXPECT_EQ(handlers.size(), 1);
EXPECT_EQ(handler_info.size(), 1);
EXPECT_TRUE(handlers.find("") != handlers.end());
EXPECT_EQ(handler_info["(default)"], "NotFoundHandler");
}
TEST_F(HandlerParseTest, DefaultHandlerConfig) {
EXPECT_EQ(GetNumberOfHandlersParsed("default EchoHandler {};"), 1);
EXPECT_EQ(handlers.size(), 1);
EXPECT_TRUE(handlers.find("") != handlers.end());
EXPECT_EQ(handler_info.size(), 1);
EXPECT_EQ(handler_info["(default)"], "EchoHandler");
}
TEST_F(HandlerParseTest, SomeHandlersConfig) {
EXPECT_EQ(GetNumberOfHandlersParsed("path /echo EchoHandler {}\n"
"path / StaticFileHandler {\n"
" root ./;\n"
"}"), 3);
EXPECT_EQ(handlers.size(), 3);
EXPECT_EQ(handler_info.size(), 3);
EXPECT_TRUE(handlers.find("/echo") != handlers.end());
EXPECT_EQ(handler_info["/echo"], "EchoHandler");
EXPECT_TRUE(handlers.find("/") != handlers.end());
EXPECT_EQ(handler_info["/"], "StaticFileHandler");
EXPECT_TRUE(handlers.find("") != handlers.end());
EXPECT_EQ(handler_info["(default)"], "NotFoundHandler");
}
TEST_F(HandlerParseTest, ImproperHandlerConfig) {
EXPECT_EQ(GetNumberOfHandlersParsed("path /echo StaticFileHandler"), 1);
EXPECT_TRUE(handlers.find("") != handlers.end());
EXPECT_EQ(handler_info["(default)"], "NotFoundHandler");
}