1+ #include " config/server_config.hpp"
12#include " handler/delete_handler.hpp"
23#include " handler/error_handler.hpp"
34#include " handler/static_file_handler.hpp"
1112#include < string>
1213
1314// This is very brittle and horrible
14- static ServerConfig make_unittest_config ()
15+ static HttpConfig make_unittest_config ()
1516{
16- SharedConfig dummy (" " );
17- std::vector<std::string> methods;
18-
19- RouteConfig loc1 (" /" , dummy);
20- methods.push_back (" GET" );
21- loc1.shared ().set_allowed_methods (methods);
22-
23- RouteConfig loc2 (" /files" , dummy);
24- methods.clear ();
25- methods.push_back (" DELETE" );
26- loc2.shared ().set_allowed_methods (methods);
27-
28- RouteConfig loc3 (" /files/private" , dummy);
29- methods.clear ();
30- methods.push_back (" POST" );
31- loc3.shared ().set_allowed_methods (methods);
32- loc3.shared ().set_uploads_off ();
33-
34- RouteConfig loc4 (" /upload" , dummy);
35- methods.clear ();
36- methods.push_back (" POST" );
37- loc4.shared ().set_allowed_methods (methods);
38- loc4.shared ().set_uploads_on ();
39-
40- ServerConfig srv (" " );
41-
42- srv.add_location (loc1);
43- srv.add_location (loc2);
44- srv.add_location (loc3);
45- srv.add_location (loc4);
46-
47- return srv;
17+ return load_http_config (" tests/config/router_unittest.conf" );
4818}
4919
5020UTEST (RouterTest, MatchesDefaultRoute)
5121{
52- Router router (make_unittest_config ());
22+ HttpConfig conf = make_unittest_config ();
23+ Router router (conf.servers [0 ].locations );
5324
5425 std::string request_path = " /" ;
55- std::string result = router.find_best_route (request_path).path () ;
26+ std::string result = router.find_best_route (request_path).path ;
5627
5728 ASSERT_STREQ (" /" , result.c_str ());
5829}
5930
6031UTEST (RouterTest, MatchesFilePrefix)
6132{
62- Router router (make_unittest_config ());
33+ HttpConfig conf = make_unittest_config ();
34+ Router router (conf.servers [0 ].locations );
6335
6436 std::string request_path = " /files/42.txt" ;
65- std::string result = router.find_best_route (request_path).path () ;
37+ std::string result = router.find_best_route (request_path).path ;
6638
6739 ASSERT_STREQ (" /files" , result.c_str ());
6840}
6941
7042UTEST (RouterTest, NoMatchFallsBackToDefaultRoute)
7143{
72- Router router (make_unittest_config ());
44+ HttpConfig conf = make_unittest_config ();
45+ Router router (conf.servers [0 ].locations );
7346
7447 std::string request_path = " /unknown/42.txt" ;
75- std::string result = router.find_best_route (request_path).path () ;
48+ std::string result = router.find_best_route (request_path).path ;
7649
7750 ASSERT_STREQ (" /" , result.c_str ());
7851}
7952
8053UTEST (RouterTest, MatchesDirectory)
8154{
82- Router router (make_unittest_config ());
55+ HttpConfig conf = make_unittest_config ();
56+ Router router (conf.servers [0 ].locations );
8357
8458 std::string request_path = " /files" ;
85- std::string result = router.find_best_route (request_path).path () ;
59+ std::string result = router.find_best_route (request_path).path ;
8660
8761 ASSERT_STREQ (" /files" , result.c_str ());
8862}
8963
9064UTEST (RouterTest, MatchesDirectoryWithTrailingSlash)
9165{
92- Router router ( make_unittest_config () );
93-
66+ HttpConfig conf = make_unittest_config ();
67+ Router router (conf. servers [ 0 ]. locations );
9468 std::string request_path = " /files/" ;
95- std::string result = router.find_best_route (request_path).path () ;
69+ std::string result = router.find_best_route (request_path).path ;
9670
9771 ASSERT_STREQ (" /files" , result.c_str ());
9872}
@@ -101,17 +75,19 @@ UTEST(RouterTest, MatchesDirectoryWithMutlipleLeadingSlashes)
10175{
10276 UTEST_SKIP (" TODO: IMPLEMENT THIS FEATURE" );
10377
104- Router router (make_unittest_config ());
78+ HttpConfig conf = make_unittest_config ();
79+ Router router (conf.servers [0 ].locations );
10580
10681 std::string request_path = " ///files" ;
107- std::string result = router.find_best_route (request_path).path () ;
82+ std::string result = router.find_best_route (request_path).path ;
10883
10984 ASSERT_STREQ (" /files" , result.c_str ());
11085}
11186
11287UTEST (RouterTest, ReturnsStaticFileHandler)
11388{
114- Router router (make_unittest_config ());
89+ HttpConfig conf = make_unittest_config ();
90+ Router router (conf.servers [0 ].locations );
11591
11692 HttpRequest req;
11793 req.method = " GET" ;
@@ -124,7 +100,8 @@ UTEST(RouterTest, ReturnsStaticFileHandler)
124100
125101UTEST (RouterTest, ReturnsUploadHandler)
126102{
127- Router router (make_unittest_config ());
103+ HttpConfig conf = make_unittest_config ();
104+ Router router (conf.servers [0 ].locations );
128105
129106 HttpRequest req;
130107 req.method = " POST" ;
@@ -139,7 +116,8 @@ UTEST(RouterTest, ReturnsUploadHandler)
139116
140117UTEST (RouterTest, ReturnsDeleteHandler)
141118{
142- Router router (make_unittest_config ());
119+ HttpConfig conf = make_unittest_config ();
120+ Router router (conf.servers [0 ].locations );
143121
144122 HttpRequest req;
145123 req.method = " DELETE" ;
@@ -152,7 +130,8 @@ UTEST(RouterTest, ReturnsDeleteHandler)
152130
153131UTEST (RouterTest, UploadsNotAllowed)
154132{
155- Router router (make_unittest_config ());
133+ HttpConfig conf = make_unittest_config ();
134+ Router router (conf.servers [0 ].locations );
156135
157136 HttpRequest req;
158137 req.method = " POST" ;
@@ -167,7 +146,8 @@ UTEST(RouterTest, UploadsNotAllowed)
167146
168147UTEST (RouterTest, EmptyPostBypassesUploadRestriction)
169148{
170- Router router (make_unittest_config ());
149+ HttpConfig conf = make_unittest_config ();
150+ Router router (conf.servers [0 ].locations );
171151
172152 HttpRequest req;
173153 req.method = " POST" ;
@@ -180,7 +160,8 @@ UTEST(RouterTest, EmptyPostBypassesUploadRestriction)
180160}
181161UTEST (RouterTest, UnsupportedMethodReturnsErrorHandler)
182162{
183- Router router (make_unittest_config ());
163+ HttpConfig conf = make_unittest_config ();
164+ Router router (conf.servers [0 ].locations );
184165
185166 HttpRequest req;
186167 req.method = " PATCH" ;
@@ -191,6 +172,7 @@ UTEST(RouterTest, UnsupportedMethodReturnsErrorHandler)
191172 delete h;
192173}
193174
175+ /*
194176UTEST(RouterTest, BuildRequestPath)
195177{
196178 UTEST_SKIP("TODO: MOVE THIS TO A BETTER LOCATION");
@@ -200,7 +182,8 @@ UTEST(RouterTest, BuildRequestPath)
200182 req.method = "GET";
201183 req.path = "/index.html";
202184
203- Router router (config);
185+ HttpConfig conf = make_unittest_config();
186+ Router router(conf.servers[0].locations);
204187
205188 Handler* handler = router.handle_request(req);
206189
@@ -219,7 +202,8 @@ UTEST(RouterTest, BuildRequestPath_Long)
219202 req.method = "GET";
220203 req.path = "/example/test_subfolder/test_subfolder/someFile.txt";
221204
222- Router router (config);
205+ HttpConfig conf = make_unittest_config();
206+ Router router(conf.servers[0].locations);
223207
224208 Handler* handler = router.handle_request(req);
225209 ASSERT_TRUE(handler != NULL);
@@ -231,3 +215,4 @@ UTEST(RouterTest, BuildRequestPath_Long)
231215
232216 delete handler;
233217}
218+ */
0 commit comments