184 {
186
188
189 app.get(
190 "/next/basic",
192 req->setAttribute<std::string>("middleware-seen", "yes");
193 next();
194 },
196 std::string marker = "no";
197 req->getAttribute<std::string>(
198 [&](const std::string& value) {
199 marker = value;
200 },
201 "middleware-seen");
202 res->status(200).send("next() reached final handler (middleware=" + marker + ")");
203 });
204
205 app.get(
206 "/next/async/basic",
208 req->setAttribute<std::string>("middleware-seen-async", "yes");
210 next();
211 });
212 },
214 std::string marker = "no";
215 req->getAttribute<std::string>(
216 [&](const std::string& value) {
217 marker = value;
218 },
219 "middleware-seen-async");
220 res->status(200).send("next(async) reached final handler (middleware=" + marker + ")");
221 });
222
223 app.get(
224 "/next/route/:id(\\d+)",
226 if (req->params["id"] == "0") {
227 next("route");
228 return;
229 }
230 next();
231 },
233 res->status(200).send("next(route) primary for id=" + req->params["id"]);
234 });
235
236 app.get(
"/next/route/:id(\\d+)", []
APPLICATION(req, res) {
237 res->status(200).send("next(route) fallback for id=" + req->params["id"]);
238 });
239
240 app.get(
241 "/next/async/route/:id(\\d+)",
243 if (req->params["id"] == "0") {
245 next("route");
246 });
247 return;
248 }
250 next();
251 });
252 },
254 res->status(200).send("next(async route) primary for id=" + req->params["id"]);
255 });
256
257 app.get(
"/next/async/route/:id(\\d+)", []
APPLICATION(req, res) {
258 res->status(200).send("next(async route) fallback for id=" + req->params["id"]);
259 });
260
263 if (req->queries["allow"] != "true") {
264 next("router");
265 return;
266 }
267 next();
268 });
270 res->status(200).send("next(router) router handler");
271 });
272
273 app.
use(
"/next/router", guarded);
275 res->status(403).send("next(router) denied by fallback");
276 });
277
278 const Router guardedAsync;
280 if (req->queries["allow"] != "true") {
282 next("router");
283 });
284 return;
285 }
287 next();
288 });
289 });
291 res->status(200).send("next(async router) router handler");
292 });
293
294 app.
use(
"/next/async/router", guardedAsync);
295 app.
get(
"/next/async/router/:rest(.*)", []
APPLICATION(req, res) {
296 res->status(403).send("next(async router) denied by fallback");
297 });
298
299 app.getConfig()->setReuseAddress();
300
302 switch (state) {
304 VLOG(1) <<
"testexpressnext listening on '" << socketAddress.toString() <<
"'";
305 break;
307 VLOG(1) <<
"testexpressnext disabled";
308 break;
310 LOG(ERROR) <<
"testexpressnext " << socketAddress.toString() <<
": " << state.
what();
311 break;
313 LOG(FATAL) <<
"testexpressnext " << socketAddress.toString() <<
": " << state.
what();
314 break;
315 }
316 });
317
320 [&nextTester] {
322 },
323 1);
324
327 LOG(ERROR) <<
"testexpressnext finished with failures=" << nextTester.
getFailures();
328 return 1;
329 }
330
331 LOG(INFO) <<
"testexpressnext finished successfully";
332 return rc;
333}
#define APPLICATION(req, res)
#define MIDDLEWARE(req, res, next)
std::size_t getFailures() const
static void atNextTick(const std::function< void(void)> &callBack)
static void init(int argc, char *argv[])
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
static constexpr int DISABLED
static constexpr int ERROR
static constexpr int FATAL
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Route & use(const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &lambda) const
Route & get(const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &lambda) const
Route & use(const Router &router) const
Route & get(const Router &router) const
typename Server::SocketAddress SocketAddress
WebAppT< web::http::legacy::in::Server > WebApp