SNode.C
Loading...
Searching...
No Matches
testexpressnext.cpp File Reference
#include "core/SNodeC.h"
#include "core/timer/Timer.h"
#include "express/legacy/in/WebApp.h"
#include "log/Logger.h"
#include "web/http/legacy/in/Client.h"
#include <cstddef>
#include <deque>
Include dependency graph for testexpressnext.cpp:

Go to the source code of this file.

Classes

struct  TestCase
class  NextTester

Functions

int main (int argc, char *argv[])

Function Documentation

◆ main()

int main ( int argc,
char * argv[] )

Definition at line 184 of file testexpressnext.cpp.

184 {
185 core::SNodeC::init(argc, argv);
186
187 const express::legacy::in::WebApp app("testexpressnext-server");
188
189 app.get(
190 "/next/basic",
191 [] MIDDLEWARE(req, res, next) {
192 req->setAttribute<std::string>("middleware-seen", "yes");
193 next();
194 },
195 [] APPLICATION(req, res) {
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",
207 [] MIDDLEWARE(req, res, next) {
208 req->setAttribute<std::string>("middleware-seen-async", "yes");
210 next();
211 });
212 },
213 [] APPLICATION(req, res) {
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+)",
225 [] MIDDLEWARE(req, res, next) {
226 if (req->params["id"] == "0") {
227 next("route");
228 return;
229 }
230 next();
231 },
232 [] APPLICATION(req, res) {
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+)",
242 [] MIDDLEWARE(req, res, next) {
243 if (req->params["id"] == "0") {
245 next("route");
246 });
247 return;
248 }
250 next();
251 });
252 },
253 [] APPLICATION(req, res) {
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
261 const Router guarded;
262 guarded.use([] MIDDLEWARE(req, res, next) {
263 if (req->queries["allow"] != "true") {
264 next("router");
265 return;
266 }
267 next();
268 });
269 guarded.get("/resource", [] APPLICATION(req, res) {
270 res->status(200).send("next(router) router handler");
271 });
272
273 app.use("/next/router", guarded);
274 app.get("/next/router/:rest(.*)", [] APPLICATION(req, res) {
275 res->status(403).send("next(router) denied by fallback");
276 });
277
278 const Router guardedAsync;
279 guardedAsync.use([] MIDDLEWARE(req, res, next) {
280 if (req->queries["allow"] != "true") {
282 next("router");
283 });
284 return;
285 }
287 next();
288 });
289 });
290 guardedAsync.get("/resource", [] APPLICATION(req, res) {
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
301 app.listen(18080, [](const express::legacy::in::WebApp::SocketAddress& socketAddress, const core::socket::State& state) {
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
318 NextTester nextTester;
320 [&nextTester] {
321 nextTester.run();
322 },
323 1);
324
325 const int rc = core::SNodeC::start();
326 if (nextTester.getFailures() > 0) {
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 LOG(level)
Definition Logger.h:148
#define VLOG(level)
Definition Logger.h:164
#define APPLICATION(req, res)
Definition Router.h:68
#define MIDDLEWARE(req, res, next)
Definition Router.h:63
std::size_t getFailures() const
static void atNextTick(const std::function< void(void)> &callBack)
static void init(int argc, char *argv[])
Definition SNodeC.cpp:54
static int start(const utils::Timeval &timeOut={LONG_MAX, 0})
Definition SNodeC.cpp:60
static constexpr int DISABLED
Definition State.h:56
static constexpr int ERROR
Definition State.h:57
std::string what() const
Definition State.cpp:114
static constexpr int FATAL
Definition State.h:58
static constexpr int OK
Definition State.h:55
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:57
Route & use(const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &lambda) const
Definition Route.cpp:102
Route & get(const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &lambda) const
Definition Route.cpp:104
Route & use(const Router &router) const
Definition Router.cpp:100
Route & get(const Router &router) const
Definition Router.cpp:102
typename Server::SocketAddress SocketAddress
Definition WebAppT.h:68
WebAppT< web::http::legacy::in::Server > WebApp
Definition WebApp.h:56

References core::EventReceiver::atNextTick(), core::socket::State::DISABLED, core::socket::State::ERROR, logger::ERROR, core::socket::State::FATAL, logger::FATAL, express::Router::get(), utils::MultibleAttributeInjector::getAttribute(), core::socket::Socket< ConfigT >::getConfig(), NextTester::getFailures(), logger::INFO, core::SNodeC::init(), net::in::stream::SocketServer< SocketAcceptorT, ConfigSocketServerT, SocketContextFactoryT, Args >::listen(), logger::LogMessage::LogMessage(), core::socket::State::OK, express::Next::operator()(), express::Request::params, NextTester::run(), express::Response::send(), utils::MultibleAttributeInjector::setAttribute(), net::in::stream::config::ConfigSocketServer::setReuseAddress(), core::timer::Timer::singleshotTimer(), core::SNodeC::start(), express::Response::status(), net::in::SocketAddress::toString(), express::WebAppT< ServerT >::WebAppT(), and core::socket::State::what().

Here is the call graph for this function: