Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 1 | #include "crow.h" |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 2 | #include <iostream> |
| 3 | #include <sstream> |
| 4 | #include <vector> |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 5 | #include "gtest/gtest.h" |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 6 | #undef BMCWEB_LOG_LEVEL |
| 7 | #define BMCWEB_LOG_LEVEL 0 |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 8 | |
| 9 | using namespace std; |
| 10 | using namespace crow; |
| 11 | |
| 12 | bool failed__ = false; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 13 | void error_print() { cerr << endl; } |
| 14 | |
| 15 | template <typename A, typename... Args> |
| 16 | void error_print(const A& a, Args... args) { |
| 17 | cerr << a; |
| 18 | error_print(args...); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 19 | } |
| 20 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 21 | template <typename... Args> |
| 22 | void fail(Args... args) { |
| 23 | error_print(args...); |
| 24 | failed__ = true; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 25 | } |
| 26 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 27 | #define ASSERT_EQUAL(a, b) \ |
| 28 | if ((a) != (b)) \ |
| 29 | fail(__FILE__ ":", __LINE__, ": Assert fail: expected ", (a), " actual ", \ |
| 30 | (b), ", " #a " == " #b ", at " __FILE__ ":", __LINE__) |
| 31 | #define ASSERT_NOTEQUAL(a, b) \ |
| 32 | if ((a) == (b)) \ |
| 33 | fail(__FILE__ ":", __LINE__, ": Assert fail: not expected ", (a), \ |
| 34 | ", " #a " != " #b ", at " __FILE__ ":", __LINE__) |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 35 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 36 | #define DISABLE_TEST(x) \ |
| 37 | struct test##x { \ |
| 38 | void test(); \ |
| 39 | } x##_; \ |
| 40 | void test##x::test() |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 41 | |
| 42 | #define LOCALHOST_ADDRESS "127.0.0.1" |
| 43 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 44 | TEST(Crow, Rule) { |
| 45 | TaggedRule<> r("/http/"); |
| 46 | r.name("abc"); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 47 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 48 | // empty handler - fail to validate |
| 49 | try { |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 50 | r.validate(); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 51 | fail("empty handler should fail to validate"); |
| 52 | } catch (runtime_error& e) { |
| 53 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 54 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 55 | int x = 0; |
| 56 | |
| 57 | // registering handler |
| 58 | r([&x] { |
| 59 | x = 1; |
| 60 | return ""; |
| 61 | }); |
| 62 | |
| 63 | r.validate(); |
| 64 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 65 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 66 | |
| 67 | // executing handler |
| 68 | ASSERT_EQUAL(0, x); |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 69 | boost::beast::http::request<boost::beast::http::string_body> req{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 70 | r.handle(Request(req), res, RoutingParams()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 71 | ASSERT_EQUAL(1, x); |
| 72 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 73 | // registering handler with Request argument |
| 74 | r([&x](const crow::Request&) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 75 | x = 2; |
| 76 | return ""; |
| 77 | }); |
| 78 | |
| 79 | r.validate(); |
| 80 | |
| 81 | // executing handler |
| 82 | ASSERT_EQUAL(1, x); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 83 | r.handle(Request(req), res, RoutingParams()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 84 | ASSERT_EQUAL(2, x); |
| 85 | } |
| 86 | |
| 87 | TEST(Crow, ParameterTagging) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 88 | static_assert(black_magic::isValid("<int><int><int>"), "valid url"); |
| 89 | static_assert(!black_magic::isValid("<int><int<<int>"), "invalid url"); |
| 90 | static_assert(!black_magic::isValid("nt>"), "invalid url"); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 91 | ASSERT_EQUAL(1, black_magic::get_parameter_tag("<int>")); |
| 92 | ASSERT_EQUAL(2, black_magic::get_parameter_tag("<uint>")); |
| 93 | ASSERT_EQUAL(3, black_magic::get_parameter_tag("<float>")); |
| 94 | ASSERT_EQUAL(3, black_magic::get_parameter_tag("<double>")); |
| 95 | ASSERT_EQUAL(4, black_magic::get_parameter_tag("<str>")); |
| 96 | ASSERT_EQUAL(4, black_magic::get_parameter_tag("<string>")); |
| 97 | ASSERT_EQUAL(5, black_magic::get_parameter_tag("<path>")); |
| 98 | ASSERT_EQUAL(6 * 6 + 6 + 1, |
| 99 | black_magic::get_parameter_tag("<int><int><int>")); |
| 100 | ASSERT_EQUAL(6 * 6 + 6 + 2, |
| 101 | black_magic::get_parameter_tag("<uint><int><int>")); |
| 102 | ASSERT_EQUAL(6 * 6 + 6 * 3 + 2, |
| 103 | black_magic::get_parameter_tag("<uint><double><int>")); |
| 104 | |
| 105 | // url definition parsed in compile time, build into *one number*, and given |
| 106 | // to template argument |
| 107 | static_assert( |
| 108 | std::is_same<black_magic::S<uint64_t, double, int64_t>, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 109 | black_magic::Arguments<6 * 6 + 6 * 3 + 2>::type>::value, |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 110 | "tag to type container"); |
| 111 | } |
| 112 | |
| 113 | TEST(Crow, PathRouting) { |
| 114 | SimpleApp app; |
| 115 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 116 | BMCWEB_ROUTE(app, "/file") |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 117 | ([] { return "file"; }); |
| 118 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 119 | BMCWEB_ROUTE(app, "/path/") |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 120 | ([] { return "path"; }); |
| 121 | |
| 122 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 123 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 124 | Request req{r}; |
| 125 | Response res; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 126 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 127 | req.url = "/file"; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 128 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 129 | app.handle(req, res); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 130 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 131 | ASSERT_EQUAL(200, res.resultInt()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 132 | } |
| 133 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 134 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 135 | Request req{r}; |
| 136 | Response res; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 137 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 138 | req.url = "/file/"; |
| 139 | |
| 140 | app.handle(req, res); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 141 | ASSERT_EQUAL(404, res.resultInt()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 142 | } |
| 143 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 144 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 145 | Request req{r}; |
| 146 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 147 | |
| 148 | req.url = "/path"; |
| 149 | |
| 150 | app.handle(req, res); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 151 | ASSERT_NOTEQUAL(404, res.resultInt()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 152 | } |
| 153 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 154 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 155 | Request req{r}; |
| 156 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 157 | |
| 158 | req.url = "/path/"; |
| 159 | |
| 160 | app.handle(req, res); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 161 | ASSERT_EQUAL(200, res.resultInt()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 162 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 165 | TEST(Crow, RoutingTest) { |
| 166 | SimpleApp app; |
| 167 | int A{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 168 | uint32_t b{}; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 169 | double C{}; |
| 170 | string D{}; |
| 171 | string E{}; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 172 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 173 | BMCWEB_ROUTE(app, "/0/<uint>") |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 174 | ([&](uint32_t b) { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 175 | b = b; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 176 | return "OK"; |
| 177 | }); |
| 178 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 179 | BMCWEB_ROUTE(app, "/1/<int>/<uint>") |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 180 | ([&](int a, uint32_t b) { |
| 181 | A = a; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 182 | b = b; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 183 | return "OK"; |
| 184 | }); |
| 185 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 186 | BMCWEB_ROUTE(app, "/4/<int>/<uint>/<double>/<string>") |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 187 | ([&](int a, uint32_t b, double c, string d) { |
| 188 | A = a; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 189 | b = b; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 190 | C = c; |
| 191 | D = d; |
| 192 | return "OK"; |
| 193 | }); |
| 194 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 195 | BMCWEB_ROUTE(app, "/5/<int>/<uint>/<double>/<string>/<path>") |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 196 | ([&](int a, uint32_t b, double c, string d, string e) { |
| 197 | A = a; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 198 | b = b; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 199 | C = c; |
| 200 | D = d; |
| 201 | E = e; |
| 202 | return "OK"; |
| 203 | }); |
| 204 | |
| 205 | app.validate(); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 206 | // app.debugPrint(); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 207 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 208 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 209 | Request req{r}; |
| 210 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 211 | |
| 212 | req.url = "/-1"; |
| 213 | |
| 214 | app.handle(req, res); |
| 215 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 216 | ASSERT_EQUAL(404, res.resultInt()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 220 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 221 | Request req{r}; |
| 222 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 223 | |
| 224 | req.url = "/0/1001999"; |
| 225 | |
| 226 | app.handle(req, res); |
| 227 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 228 | ASSERT_EQUAL(200, res.resultInt()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 229 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 230 | ASSERT_EQUAL(1001999, b); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 234 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 235 | Request req{r}; |
| 236 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 237 | |
| 238 | req.url = "/1/-100/1999"; |
| 239 | |
| 240 | app.handle(req, res); |
| 241 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 242 | ASSERT_EQUAL(200, res.resultInt()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 243 | |
| 244 | ASSERT_EQUAL(-100, A); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 245 | ASSERT_EQUAL(1999, b); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 246 | } |
| 247 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 248 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 249 | Request req{r}; |
| 250 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 251 | |
| 252 | req.url = "/4/5000/3/-2.71828/hellhere"; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 253 | |
| 254 | app.handle(req, res); |
| 255 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 256 | ASSERT_EQUAL(200, res.resultInt()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 257 | |
| 258 | ASSERT_EQUAL(5000, A); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 259 | ASSERT_EQUAL(3, b); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 260 | ASSERT_EQUAL(-2.71828, C); |
| 261 | ASSERT_EQUAL("hellhere", D); |
| 262 | } |
| 263 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 264 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 265 | Request req{r}; |
| 266 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 267 | |
| 268 | req.url = "/5/-5/999/3.141592/hello_there/a/b/c/d"; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 269 | |
| 270 | app.handle(req, res); |
| 271 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 272 | ASSERT_EQUAL(200, res.resultInt()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 273 | |
| 274 | ASSERT_EQUAL(-5, A); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 275 | ASSERT_EQUAL(999, b); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 276 | ASSERT_EQUAL(3.141592, C); |
| 277 | ASSERT_EQUAL("hello_there", D); |
| 278 | ASSERT_EQUAL("a/b/c/d", E); |
| 279 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 282 | TEST(Crow, simple_response_RoutingParams) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 283 | ASSERT_EQUAL(100, |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 284 | Response(boost::beast::http::status::continue_).resultInt()); |
| 285 | ASSERT_EQUAL(200, Response("Hello there").resultInt()); |
| 286 | ASSERT_EQUAL(500, Response(boost::beast::http::status::internal_server_error, |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 287 | "Internal Error?") |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 288 | .resultInt()); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 289 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 290 | RoutingParams rp; |
| 291 | rp.intParams.push_back(1); |
| 292 | rp.intParams.push_back(5); |
| 293 | rp.uintParams.push_back(2); |
| 294 | rp.doubleParams.push_back(3); |
| 295 | rp.stringParams.push_back("hello"); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 296 | ASSERT_EQUAL(1, rp.get<int64_t>(0)); |
| 297 | ASSERT_EQUAL(5, rp.get<int64_t>(1)); |
| 298 | ASSERT_EQUAL(2, rp.get<uint64_t>(0)); |
| 299 | ASSERT_EQUAL(3, rp.get<double>(0)); |
| 300 | ASSERT_EQUAL("hello", rp.get<string>(0)); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 303 | TEST(Crow, handler_with_response) { |
| 304 | SimpleApp app; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 305 | BMCWEB_ROUTE(app, "/")([](const crow::Request&, crow::Response&) {}); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 308 | TEST(Crow, http_method) { |
| 309 | SimpleApp app; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 310 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 311 | BMCWEB_ROUTE(app, "/").methods("POST"_method, |
| 312 | "GET"_method)([](const Request& req) { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 313 | if (req.method() == "GET"_method) |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 314 | return "2"; |
| 315 | else |
| 316 | return "1"; |
| 317 | }); |
| 318 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 319 | BMCWEB_ROUTE(app, "/get_only") |
| 320 | .methods("GET"_method)([](const Request& /*req*/) { return "get"; }); |
| 321 | BMCWEB_ROUTE(app, "/post_only") |
| 322 | .methods("POST"_method)([](const Request& /*req*/) { return "post"; }); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 323 | |
| 324 | // cannot have multiple handlers for the same url |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 325 | // BMCWEB_ROUTE(app, "/") |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 326 | //.methods("GET"_method) |
| 327 | //([]{ return "2"; }); |
| 328 | |
| 329 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 330 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 331 | Request req{r}; |
| 332 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 333 | |
| 334 | req.url = "/"; |
| 335 | app.handle(req, res); |
| 336 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 337 | ASSERT_EQUAL("2", res.body()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 338 | } |
| 339 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 340 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 341 | Request req{r}; |
| 342 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 343 | |
| 344 | req.url = "/"; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 345 | r.method("POST"_method); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 346 | app.handle(req, res); |
| 347 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 348 | ASSERT_EQUAL("1", res.body()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 352 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 353 | Request req{r}; |
| 354 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 355 | |
| 356 | req.url = "/get_only"; |
| 357 | app.handle(req, res); |
| 358 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 359 | ASSERT_EQUAL("get", res.body()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 363 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 364 | Request req{r}; |
| 365 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 366 | |
| 367 | req.url = "/get_only"; |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 368 | r.method("POST"_method); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 369 | app.handle(req, res); |
| 370 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 371 | ASSERT_NOTEQUAL("get", res.body()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 372 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 375 | TEST(Crow, server_handling_error_request) { |
| 376 | static char buf[2048]; |
| 377 | SimpleApp app; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 378 | BMCWEB_ROUTE(app, "/")([] { return "A"; }); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 379 | Server<SimpleApp> server(&app, LOCALHOST_ADDRESS, 45451); |
| 380 | auto _ = async(launch::async, [&] { server.run(); }); |
| 381 | std::string sendmsg = "POX"; |
| 382 | asio::io_service is; |
| 383 | { |
| 384 | asio::ip::tcp::socket c(is); |
| 385 | c.connect(asio::ip::tcp::endpoint( |
| 386 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 387 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 388 | c.send(asio::buffer(sendmsg)); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 389 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 390 | try { |
| 391 | c.receive(asio::buffer(buf, 2048)); |
| 392 | fail(); |
| 393 | } catch (std::exception& e) { |
| 394 | // std::cerr << e.what() << std::endl; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 395 | } |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 396 | } |
| 397 | server.stop(); |
| 398 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 399 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 400 | TEST(Crow, multi_server) { |
| 401 | static char buf[2048]; |
| 402 | SimpleApp app1, app2; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 403 | BMCWEB_ROUTE(app1, "/").methods("GET"_method, |
| 404 | "POST"_method)([] { return "A"; }); |
| 405 | BMCWEB_ROUTE(app2, "/").methods("GET"_method, |
| 406 | "POST"_method)([] { return "B"; }); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 407 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 408 | Server<SimpleApp> server1(&app1, LOCALHOST_ADDRESS, 45451); |
| 409 | Server<SimpleApp> server2(&app2, LOCALHOST_ADDRESS, 45452); |
| 410 | |
| 411 | auto _ = async(launch::async, [&] { server1.run(); }); |
| 412 | auto _2 = async(launch::async, [&] { server2.run(); }); |
| 413 | |
| 414 | std::string sendmsg = |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 415 | "POST /\r\nContent-Length:3\r\nX-HeaderTest: 123\r\n\r\nA=b\r\n"; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 416 | asio::io_service is; |
| 417 | { |
| 418 | asio::ip::tcp::socket c(is); |
| 419 | c.connect(asio::ip::tcp::endpoint( |
| 420 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
| 421 | |
| 422 | c.send(asio::buffer(sendmsg)); |
| 423 | |
| 424 | size_t recved = c.receive(asio::buffer(buf, 2048)); |
| 425 | ASSERT_EQUAL('A', buf[recved - 1]); |
| 426 | } |
| 427 | |
| 428 | { |
| 429 | asio::ip::tcp::socket c(is); |
| 430 | c.connect(asio::ip::tcp::endpoint( |
| 431 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45452)); |
| 432 | |
| 433 | for (auto ch : sendmsg) { |
| 434 | char buf[1] = {ch}; |
| 435 | c.send(asio::buffer(buf)); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 438 | size_t recved = c.receive(asio::buffer(buf, 2048)); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 439 | ASSERT_EQUAL('b', buf[recved - 1]); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 440 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 441 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 442 | server1.stop(); |
| 443 | server2.stop(); |
| 444 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 445 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 446 | TEST(Crow, black_magic) { |
| 447 | using namespace black_magic; |
| 448 | static_assert( |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 449 | std::is_same<void, LastElementType<int, char, void>::type>::value, |
| 450 | "LastElementType"); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 451 | static_assert( |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 452 | std::is_same< |
| 453 | char, PopBack<int, char, void>::rebind<LastElementType>::type>::value, |
| 454 | "pop_back"); |
| 455 | static_assert( |
| 456 | std::is_same<int, PopBack<int, char, void>::rebind<PopBack>::rebind< |
| 457 | LastElementType>::type>::value, |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 458 | "pop_back"); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 461 | struct NullMiddleware { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 462 | struct Context {}; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 463 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 464 | template <typename AllContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 465 | void beforeHandle(Request&, Response&, Context&, AllContext&) {} |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 466 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 467 | template <typename AllContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 468 | void afterHandle(Request&, Response&, Context&, AllContext&) {} |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 469 | }; |
| 470 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 471 | struct NullSimpleMiddleware { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 472 | struct Context {}; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 473 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 474 | void beforeHandle(Request& /*req*/, Response& /*res*/, Context& /*ctx*/) {} |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 475 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 476 | void afterHandle(Request& /*req*/, Response& /*res*/, Context& /*ctx*/) {} |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 477 | }; |
| 478 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 479 | TEST(Crow, middleware_simple) { |
| 480 | App<NullMiddleware, NullSimpleMiddleware> app; |
| 481 | decltype(app)::server_t server(&app, LOCALHOST_ADDRESS, 45451); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 482 | BMCWEB_ROUTE(app, "/") |
| 483 | ([&](const crow::Request& req) { |
| 484 | app.getContext<NullMiddleware>(req); |
| 485 | app.getContext<NullSimpleMiddleware>(req); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 486 | return ""; |
| 487 | }); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 490 | struct IntSettingMiddleware { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 491 | struct Context { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 492 | int val; |
| 493 | }; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 494 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 495 | template <typename AllContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 496 | void beforeHandle(Request&, Response&, Context& ctx, AllContext&) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 497 | ctx.val = 1; |
| 498 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 499 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 500 | template <typename AllContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 501 | void afterHandle(Request&, Response&, Context& ctx, AllContext&) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 502 | ctx.val = 2; |
| 503 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 504 | }; |
| 505 | |
| 506 | std::vector<std::string> test_middleware_context_vector; |
| 507 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 508 | struct FirstMW { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 509 | struct Context { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 510 | std::vector<string> v; |
| 511 | }; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 512 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 513 | void beforeHandle(Request& /*req*/, Response& /*res*/, Context& ctx) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 514 | ctx.v.push_back("1 before"); |
| 515 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 516 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 517 | void afterHandle(Request& /*req*/, Response& /*res*/, Context& ctx) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 518 | ctx.v.push_back("1 after"); |
| 519 | test_middleware_context_vector = ctx.v; |
| 520 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 521 | }; |
| 522 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 523 | struct SecondMW { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 524 | struct Context {}; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 525 | template <typename AllContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 526 | void beforeHandle(Request& req, Response& res, Context&, |
| 527 | AllContext& all_ctx) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 528 | all_ctx.template get<FirstMW>().v.push_back("2 before"); |
| 529 | if (req.url == "/break") res.end(); |
| 530 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 531 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 532 | template <typename AllContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 533 | void afterHandle(Request&, Response&, Context&, AllContext& all_ctx) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 534 | all_ctx.template get<FirstMW>().v.push_back("2 after"); |
| 535 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 536 | }; |
| 537 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 538 | struct ThirdMW { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 539 | struct Context {}; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 540 | template <typename AllContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 541 | void beforeHandle(Request&, Response&, Context&, AllContext& all_ctx) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 542 | all_ctx.template get<FirstMW>().v.push_back("3 before"); |
| 543 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 544 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 545 | template <typename AllContext> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 546 | void afterHandle(Request&, Response&, Context&, AllContext& all_ctx) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 547 | all_ctx.template get<FirstMW>().v.push_back("3 after"); |
| 548 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 549 | }; |
| 550 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 551 | TEST(Crow, middlewareContext) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 552 | static char buf[2048]; |
| 553 | // SecondMW depends on FirstMW (it uses all_ctx.get<FirstMW>) |
| 554 | // so it leads to compile error if we remove FirstMW from definition |
| 555 | // App<IntSettingMiddleware, SecondMW> app; |
| 556 | // or change the order of FirstMW and SecondMW |
| 557 | // App<IntSettingMiddleware, SecondMW, FirstMW> app; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 558 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 559 | App<IntSettingMiddleware, FirstMW, SecondMW, ThirdMW> app; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 560 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 561 | int x{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 562 | BMCWEB_ROUTE(app, "/") |
| 563 | ([&](const Request& req) { |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 564 | { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 565 | auto& ctx = app.getContext<IntSettingMiddleware>(req); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 566 | x = ctx.val; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 567 | } |
| 568 | { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 569 | auto& ctx = app.getContext<FirstMW>(req); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 570 | ctx.v.push_back("handle"); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 571 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 572 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 573 | return ""; |
| 574 | }); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 575 | BMCWEB_ROUTE(app, "/break") |
| 576 | ([&](const Request& req) { |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 577 | { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 578 | auto& ctx = app.getContext<FirstMW>(req); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 579 | ctx.v.push_back("handle"); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 580 | } |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 581 | |
| 582 | return ""; |
| 583 | }); |
| 584 | |
| 585 | decltype(app)::server_t server(&app, LOCALHOST_ADDRESS, 45451); |
| 586 | auto _ = async(launch::async, [&] { server.run(); }); |
| 587 | std::string sendmsg = "GET /\r\n\r\n"; |
| 588 | asio::io_service is; |
| 589 | { |
| 590 | asio::ip::tcp::socket c(is); |
| 591 | c.connect(asio::ip::tcp::endpoint( |
| 592 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
| 593 | |
| 594 | c.send(asio::buffer(sendmsg)); |
| 595 | |
| 596 | c.receive(asio::buffer(buf, 2048)); |
| 597 | c.close(); |
| 598 | } |
| 599 | { |
| 600 | auto& out = test_middleware_context_vector; |
| 601 | ASSERT_EQUAL(1, x); |
| 602 | ASSERT_EQUAL(7, out.size()); |
| 603 | ASSERT_EQUAL("1 before", out[0]); |
| 604 | ASSERT_EQUAL("2 before", out[1]); |
| 605 | ASSERT_EQUAL("3 before", out[2]); |
| 606 | ASSERT_EQUAL("handle", out[3]); |
| 607 | ASSERT_EQUAL("3 after", out[4]); |
| 608 | ASSERT_EQUAL("2 after", out[5]); |
| 609 | ASSERT_EQUAL("1 after", out[6]); |
| 610 | } |
| 611 | std::string sendmsg2 = "GET /break\r\n\r\n"; |
| 612 | { |
| 613 | asio::ip::tcp::socket c(is); |
| 614 | c.connect(asio::ip::tcp::endpoint( |
| 615 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
| 616 | |
| 617 | c.send(asio::buffer(sendmsg2)); |
| 618 | |
| 619 | c.receive(asio::buffer(buf, 2048)); |
| 620 | c.close(); |
| 621 | } |
| 622 | { |
| 623 | auto& out = test_middleware_context_vector; |
| 624 | ASSERT_EQUAL(4, out.size()); |
| 625 | ASSERT_EQUAL("1 before", out[0]); |
| 626 | ASSERT_EQUAL("2 before", out[1]); |
| 627 | ASSERT_EQUAL("2 after", out[2]); |
| 628 | ASSERT_EQUAL("1 after", out[3]); |
| 629 | } |
| 630 | server.stop(); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 633 | TEST(Crow, bug_quick_repeated_request) { |
| 634 | static char buf[2048]; |
| 635 | |
| 636 | SimpleApp app; |
| 637 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 638 | BMCWEB_ROUTE(app, "/")([&] { return "hello"; }); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 639 | |
| 640 | decltype(app)::server_t server(&app, LOCALHOST_ADDRESS, 45451); |
| 641 | auto _ = async(launch::async, [&] { server.run(); }); |
| 642 | std::string sendmsg = "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"; |
| 643 | asio::io_service is; |
| 644 | { |
| 645 | std::vector<std::future<void>> v; |
| 646 | for (int i = 0; i < 5; i++) { |
| 647 | v.push_back(async(launch::async, [&] { |
| 648 | asio::ip::tcp::socket c(is); |
| 649 | c.connect(asio::ip::tcp::endpoint( |
| 650 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
| 651 | |
| 652 | for (int j = 0; j < 5; j++) { |
| 653 | c.send(asio::buffer(sendmsg)); |
| 654 | |
| 655 | size_t received = c.receive(asio::buffer(buf, 2048)); |
| 656 | ASSERT_EQUAL("hello", |
| 657 | std::string(buf + received - 5, buf + received)); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 658 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 659 | c.close(); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 660 | })); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 661 | } |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 662 | } |
| 663 | server.stop(); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 666 | TEST(Crow, simple_url_params) { |
| 667 | static char buf[2048]; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 668 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 669 | SimpleApp app; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 670 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 671 | QueryString lastUrlParams; |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 672 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 673 | BMCWEB_ROUTE(app, "/params") |
| 674 | ([&lastUrlParams](const crow::Request& req) { |
| 675 | lastUrlParams = std::move(req.urlParams); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 676 | return "OK"; |
| 677 | }); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 678 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 679 | /// params?h=1&foo=bar&lol&count[]=1&count[]=4&pew=5.2 |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 680 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 681 | decltype(app)::server_t server(&app, LOCALHOST_ADDRESS, 45451); |
| 682 | auto _ = async(launch::async, [&] { server.run(); }); |
| 683 | asio::io_service is; |
| 684 | std::string sendmsg; |
| 685 | |
| 686 | // check empty params |
| 687 | sendmsg = "GET /params\r\n\r\n"; |
| 688 | { |
| 689 | asio::ip::tcp::socket c(is); |
| 690 | c.connect(asio::ip::tcp::endpoint( |
| 691 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
| 692 | c.send(asio::buffer(sendmsg)); |
| 693 | c.receive(asio::buffer(buf, 2048)); |
| 694 | c.close(); |
| 695 | |
| 696 | stringstream ss; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 697 | ss << lastUrlParams; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 698 | |
| 699 | ASSERT_EQUAL("[ ]", ss.str()); |
| 700 | } |
| 701 | // check single presence |
| 702 | sendmsg = "GET /params?foobar\r\n\r\n"; |
| 703 | { |
| 704 | asio::ip::tcp::socket c(is); |
| 705 | c.connect(asio::ip::tcp::endpoint( |
| 706 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
| 707 | c.send(asio::buffer(sendmsg)); |
| 708 | c.receive(asio::buffer(buf, 2048)); |
| 709 | c.close(); |
| 710 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 711 | ASSERT_TRUE(lastUrlParams.get("missing") == nullptr); |
| 712 | ASSERT_TRUE(lastUrlParams.get("foobar") != nullptr); |
| 713 | ASSERT_TRUE(lastUrlParams.getList("missing").empty()); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 714 | } |
| 715 | // check multiple presence |
| 716 | sendmsg = "GET /params?foo&bar&baz\r\n\r\n"; |
| 717 | { |
| 718 | asio::ip::tcp::socket c(is); |
| 719 | c.connect(asio::ip::tcp::endpoint( |
| 720 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
| 721 | c.send(asio::buffer(sendmsg)); |
| 722 | c.receive(asio::buffer(buf, 2048)); |
| 723 | c.close(); |
| 724 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 725 | ASSERT_TRUE(lastUrlParams.get("missing") == nullptr); |
| 726 | ASSERT_TRUE(lastUrlParams.get("foo") != nullptr); |
| 727 | ASSERT_TRUE(lastUrlParams.get("bar") != nullptr); |
| 728 | ASSERT_TRUE(lastUrlParams.get("baz") != nullptr); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 729 | } |
| 730 | // check single value |
| 731 | sendmsg = "GET /params?hello=world\r\n\r\n"; |
| 732 | { |
| 733 | asio::ip::tcp::socket c(is); |
| 734 | c.connect(asio::ip::tcp::endpoint( |
| 735 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
| 736 | c.send(asio::buffer(sendmsg)); |
| 737 | c.receive(asio::buffer(buf, 2048)); |
| 738 | c.close(); |
| 739 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 740 | ASSERT_EQUAL(string(lastUrlParams.get("hello")), "world"); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 741 | } |
| 742 | // check multiple value |
| 743 | sendmsg = "GET /params?hello=world&left=right&up=down\r\n\r\n"; |
| 744 | { |
| 745 | asio::ip::tcp::socket c(is); |
| 746 | c.connect(asio::ip::tcp::endpoint( |
| 747 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
| 748 | c.send(asio::buffer(sendmsg)); |
| 749 | c.receive(asio::buffer(buf, 2048)); |
| 750 | c.close(); |
| 751 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 752 | ASSERT_EQUAL(string(lastUrlParams.get("hello")), "world"); |
| 753 | ASSERT_EQUAL(string(lastUrlParams.get("left")), "right"); |
| 754 | ASSERT_EQUAL(string(lastUrlParams.get("up")), "down"); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 755 | } |
| 756 | // check multiple value, multiple types |
| 757 | sendmsg = "GET /params?int=100&double=123.45&boolean=1\r\n\r\n"; |
| 758 | { |
| 759 | asio::ip::tcp::socket c(is); |
| 760 | c.connect(asio::ip::tcp::endpoint( |
| 761 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
| 762 | c.send(asio::buffer(sendmsg)); |
| 763 | c.receive(asio::buffer(buf, 2048)); |
| 764 | c.close(); |
| 765 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 766 | ASSERT_EQUAL(boost::lexical_cast<int>(lastUrlParams.get("int")), 100); |
| 767 | ASSERT_EQUAL(boost::lexical_cast<double>(lastUrlParams.get("double")), |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 768 | 123.45); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 769 | ASSERT_EQUAL(boost::lexical_cast<bool>(lastUrlParams.get("boolean")), true); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 770 | } |
| 771 | // check single array value |
| 772 | sendmsg = "GET /params?tmnt[]=leonardo\r\n\r\n"; |
| 773 | { |
| 774 | asio::ip::tcp::socket c(is); |
| 775 | |
| 776 | c.connect(asio::ip::tcp::endpoint( |
| 777 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
| 778 | c.send(asio::buffer(sendmsg)); |
| 779 | c.receive(asio::buffer(buf, 2048)); |
| 780 | c.close(); |
| 781 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 782 | ASSERT_TRUE(lastUrlParams.get("tmnt") == nullptr); |
| 783 | ASSERT_EQUAL(lastUrlParams.getList("tmnt").size(), 1); |
| 784 | ASSERT_EQUAL(string(lastUrlParams.getList("tmnt")[0]), "leonardo"); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 785 | } |
| 786 | // check multiple array value |
| 787 | sendmsg = |
| 788 | "GET /params?tmnt[]=leonardo&tmnt[]=donatello&tmnt[]=raphael\r\n\r\n"; |
| 789 | { |
| 790 | asio::ip::tcp::socket c(is); |
| 791 | |
| 792 | c.connect(asio::ip::tcp::endpoint( |
| 793 | asio::ip::address::from_string(LOCALHOST_ADDRESS), 45451)); |
| 794 | c.send(asio::buffer(sendmsg)); |
| 795 | c.receive(asio::buffer(buf, 2048)); |
| 796 | c.close(); |
| 797 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 798 | ASSERT_EQUAL(lastUrlParams.getList("tmnt").size(), 3); |
| 799 | ASSERT_EQUAL(string(lastUrlParams.getList("tmnt")[0]), "leonardo"); |
| 800 | ASSERT_EQUAL(string(lastUrlParams.getList("tmnt")[1]), "donatello"); |
| 801 | ASSERT_EQUAL(string(lastUrlParams.getList("tmnt")[2]), "raphael"); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 802 | } |
| 803 | server.stop(); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 806 | TEST(Crow, routeDynamic) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 807 | SimpleApp app; |
| 808 | int x = 1; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 809 | app.routeDynamic("/")([&] { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 810 | x = 2; |
| 811 | return ""; |
| 812 | }); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 813 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 814 | app.routeDynamic("/set4")([&](const Request&) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 815 | x = 4; |
| 816 | return ""; |
| 817 | }); |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 818 | app.routeDynamic("/set5")([&](const Request&, Response& res) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 819 | x = 5; |
| 820 | res.end(); |
| 821 | }); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 822 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 823 | app.routeDynamic("/set_int/<int>")([&](int y) { |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 824 | x = y; |
| 825 | return ""; |
| 826 | }); |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 827 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 828 | try { |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 829 | app.routeDynamic("/invalid_test/<double>/<path>")([]() { return ""; }); |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 830 | fail(); |
| 831 | } catch (std::exception&) { |
| 832 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 833 | |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 834 | // app is in an invalid state when routeDynamic throws an exception. |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 835 | try { |
| 836 | app.validate(); |
| 837 | fail(); |
| 838 | } catch (std::exception&) { |
| 839 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 840 | |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 841 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 842 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 843 | Request req{r}; |
| 844 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 845 | req.url = "/"; |
| 846 | app.handle(req, res); |
| 847 | ASSERT_EQUAL(x, 2); |
| 848 | } |
| 849 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 850 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 851 | Request req{r}; |
| 852 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 853 | req.url = "/set_int/42"; |
| 854 | app.handle(req, res); |
| 855 | ASSERT_EQUAL(x, 42); |
| 856 | } |
| 857 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 858 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 859 | Request req{r}; |
| 860 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 861 | req.url = "/set5"; |
| 862 | app.handle(req, res); |
| 863 | ASSERT_EQUAL(x, 5); |
| 864 | } |
| 865 | { |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 866 | boost::beast::http::request<boost::beast::http::string_body> r{}; |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 867 | Request req{r}; |
| 868 | Response res; |
Ed Tanous | 1ff4878 | 2017-04-18 12:45:08 -0700 | [diff] [blame] | 869 | req.url = "/set4"; |
| 870 | app.handle(req, res); |
| 871 | ASSERT_EQUAL(x, 4); |
| 872 | } |
Ed Tanous | 8041f31 | 2017-04-03 09:47:01 -0700 | [diff] [blame] | 873 | } |