2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
42#include "core/SNodeC.h"
43#include "core/timer/Timer.h"
44#include "express/legacy/in/WebApp.h"
45#include "log/Logger.h"
46#include "web/http/legacy/in/Client.h"
67 {{
"next() middleware chain",
"/next/basic", 200,
"next() reached final handler",
"keep-alive"},
68 {
"next() asynchronous middleware chain",
"/next/async/basic", 200,
"next(async) reached final handler",
"keep-alive"},
69 {
"next('route') fallback",
"/next/route/0", 200,
"next(route) fallback for id=0",
"keep-alive"},
70 {
"next('route') primary",
"/next/route/7", 200,
"next(route) primary for id=7",
"keep-alive"},
71 {
"next('route') asynchronous fallback",
"/next/async/route/0", 200,
"next(async route) fallback for id=0",
"keep-alive"},
72 {
"next('route') asynchronous primary",
"/next/async/route/7", 200,
"next(async route) primary for id=7",
"keep-alive"},
73 {
"next('router') fallback",
"/next/router/resource?allow=false", 403,
"next(router) denied by fallback",
"keep-alive"},
74 {
"next('router') inside router",
"/next/router/resource?allow=true", 200,
"next(router) router handler",
"keep-alive"},
75 {
"next('router') asynchronous fallback",
76 "/next/async/router/resource?allow=false",
78 "next(async router) denied by fallback",
80 {
"next('router') asynchronous inside router",
81 "/next/async/router/resource?allow=true",
83 "next(async router) router handler",
89 [
this](
const std::shared_ptr<
MasterRequest>& connectedRequest) {
99 [
this](
const Client::SocketAddress&,
const core::socket::
State& state) {
138 LOG(ERROR) <<
"FAIL: master request not connected";
155 [
this, current]([[maybe_unused]]
const std::shared_ptr<
Request>& req,
const std::shared_ptr<
Response>& res) {
156 const std::string body(res->
body.begin(), res->
body.end());
158 const bool bodyOk = body.find(current
.expectedBody) != std::string::npos;
160 if (statusOk && bodyOk) {
166 <<
" got status=" << res->
statusCode <<
" body='" << body <<
"'";
171 [
this, current]([[maybe_unused]]
const std::shared_ptr<
Request>& req,
const std::string& reason) {
173 LOG(ERROR) <<
"FAIL: " << current
.name <<
" parse-error: " << reason;
184int main(
int argc,
char* argv[]) {
187 const express::legacy::
in::WebApp app
("testexpressnext-server");
196 std::string marker =
"no";
198 [&](
const std::string& value) {
202 res->
status(200
).send("next() reached final handler (middleware=" + marker +
")");
214 std::string marker =
"no";
216 [&](
const std::string& value) {
219 "middleware-seen-async");
220 res->
status(200
).send("next(async) reached final handler (middleware=" + marker +
")");
224 "/next/route/:id(\\d+)",
226 if (req->
params[
"id"] ==
"0") {
236 app.get(
"/next/route/:id(\\d+)", []
APPLICATION(req, res) {
241 "/next/async/route/:id(\\d+)",
243 if (req->
params[
"id"] ==
"0") {
257 app.get(
"/next/async/route/:id(\\d+)", []
APPLICATION(req, res) {
263 if (req->queries[
"allow"] !=
"true") {
269 guarded.get(
"/resource", []
APPLICATION(req, res) {
273 app.use(
"/next/router", guarded);
274 app.get(
"/next/router/:rest(.*)", []
APPLICATION(req, res) {
278 const Router guardedAsync;
279 guardedAsync.use([]
MIDDLEWARE(req, res, next) {
280 if (req->queries[
"allow"] !=
"true") {
290 guardedAsync.get(
"/resource", []
APPLICATION(req, res) {
294 app.use(
"/next/async/router", guardedAsync);
295 app.get(
"/next/async/router/:rest(.*)", []
APPLICATION(req, res) {
301 app
.listen(18080
, [](
const express::legacy::
in::WebApp::SocketAddress& socketAddress,
const core::socket::
State& state) {
304 VLOG(1) <<
"testexpressnext listening on '" << socketAddress
.toString() <<
"'";
307 VLOG(1) <<
"testexpressnext disabled";
331 LOG(INFO) <<
"testexpressnext finished successfully";
#define APPLICATION(req, res)
#define MIDDLEWARE(req, res, next)
Client::MasterRequest MasterRequest
std::deque< TestCase > testCases
void dispatchNextRequest()
std::shared_ptr< MasterRequest > masterRequest
web::http::legacy::in::Client Client
std::shared_ptr< Client > client
Client::Response Response
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})
Config * getConfig() const
static constexpr int DISABLED
bool operator==(const int &state) const
static constexpr int ERROR
static constexpr int FATAL
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
void operator()(const std::string &how="") const
std::map< std::string, std::string > params
void send(const std::string &chunk)
Response & status(int status)
Route & get(const Router &router) const
WebAppT(const std::string &name)
LogMessage(Level level, int verboseLevel=-1, bool withErrno=false)
std::string toString(bool expanded=true) const override
const Super & connect(const std::string &ipOrHostname, uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
const Super & listen(uint16_t port, const std::function< void(const SocketAddress &, core::socket::State)> &onStatus) const
ConfigSocketServer * setReuseAddress(bool reuseAddress=true)
constexpr bool setAttribute(const Attribute &&attribute, const std::string &subKey="", bool overwrite=false)
bool getAttribute(const std::function< void(Attribute &)> &onFound, const std::string &subKey="") const
bool end(const std::function< void(const std::shared_ptr< Request > &, const std::shared_ptr< Response > &)> &onResponseReceived, const std::function< void(const std::shared_ptr< Request > &, const std::string &)> &onResponseParseError)
Request & set(const std::string &field, const std::string &value, bool overwrite=true)
int main(int argc, char *argv[])
web::http::client::Client< net::in::stream::legacy::SocketClient > Client