SNode.C
Loading...
Searching...
No Matches
NextTester Class Reference
Collaboration diagram for NextTester:

Public Member Functions

 NextTester ()
void run ()
std::size_t getFailures () const
 NextTester ()
void run ()
std::size_t getFailures () const

Private Types

using Client = web::http::legacy::in::Client
using MasterRequest = Client::MasterRequest
using Request = Client::Request
using Response = Client::Response
using Client = web::http::legacy::in::Client
using MasterRequest = Client::MasterRequest
using Request = Client::Request
using Response = Client::Response

Private Member Functions

void dispatchNextRequest ()
void dispatchNextRequest ()

Private Attributes

std::deque< TestCasetestCases
std::shared_ptr< Clientclient
std::shared_ptr< MasterRequestmasterRequest
std::size_t failures = 0

Detailed Description

Definition at line 63 of file testexpressnext.cpp.

Member Typedef Documentation

◆ Client [1/2]

Definition at line 117 of file testexpressnext.cpp.

◆ Client [2/2]

Definition at line 117 of file testexpressnext.cpp.

◆ MasterRequest [1/2]

Definition at line 118 of file testexpressnext.cpp.

◆ MasterRequest [2/2]

Definition at line 118 of file testexpressnext.cpp.

◆ Request [1/2]

Definition at line 119 of file testexpressnext.cpp.

◆ Request [2/2]

Definition at line 119 of file testexpressnext.cpp.

◆ Response [1/2]

Definition at line 120 of file testexpressnext.cpp.

◆ Response [2/2]

Definition at line 120 of file testexpressnext.cpp.

Constructor & Destructor Documentation

◆ NextTester() [1/2]

NextTester::NextTester ( )
inline

Definition at line 65 of file testexpressnext.cpp.

66 : testCases(
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",
77 403,
78 "next(async router) denied by fallback",
79 "keep-alive"},
80 {"next('router') asynchronous inside router",
81 "/next/async/router/resource?allow=true",
82 200,
83 "next(async router) router handler",
84 "close"}}) {
85 }
std::deque< TestCase > testCases

References testCases.

◆ NextTester() [2/2]

NextTester::NextTester ( )
inline

Definition at line 65 of file testexpressnext.cpp.

66 : testCases(
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",
77 403,
78 "next(async router) denied by fallback",
79 "keep-alive"},
80 {"next('router') asynchronous inside router",
81 "/next/async/router/resource?allow=true",
82 200,
83 "next(async router) router handler",
84 "close"}}) {
85 }

Member Function Documentation

◆ dispatchNextRequest() [1/2]

void NextTester::dispatchNextRequest ( )
inlineprivate

Definition at line 122 of file testexpressnext.cpp.

122 {
123 if (testCases.empty()) {
124 LOG(INFO) << "All express next() tests executed. failures=" << failures;
125 if (masterRequest && masterRequest->isConnected()) {
126 masterRequest->disconnect();
127 }
129 [] {
131 },
132 1);
133 return;
134 }
135
136 if (!masterRequest || !masterRequest->isConnected()) {
137 ++failures;
138 LOG(ERROR) << "FAIL: master request not connected";
140 [] {
142 },
143 1);
144 return;
145 }
146
147 const TestCase current = testCases.front();
148 testCases.pop_front();
149
150 masterRequest->init();
151 masterRequest->method = "GET";
152 masterRequest->url = current.url;
153 masterRequest->set("Connection", current.connection);
154 masterRequest->end(
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());
157 const bool statusOk = std::stoi(res->statusCode) == current.expectedStatus;
158 const bool bodyOk = body.find(current.expectedBody) != std::string::npos;
159
160 if (statusOk && bodyOk) {
161 LOG(INFO) << "PASS: " << current.name << " status=" << res->statusCode << " body='" << body << "'";
162 } else {
163 ++failures;
164 LOG(ERROR) << "FAIL: " << current.name << " expected status=" << current.expectedStatus << " expected body fragment='"
165 << current.expectedBody << "'"
166 << " got status=" << res->statusCode << " body='" << body << "'";
167 }
168
170 },
171 [this, current]([[maybe_unused]] const std::shared_ptr<Request>& req, const std::string& reason) {
172 ++failures;
173 LOG(ERROR) << "FAIL: " << current.name << " parse-error: " << reason;
175 });
176 }
#define LOG(level)
Definition Logger.h:148
void dispatchNextRequest()
std::shared_ptr< MasterRequest > masterRequest
std::size_t failures
static void stop()
Definition SNodeC.cpp:64
static Timer singleshotTimer(const std::function< void()> &dispatcher, const utils::Timeval &timeout)
Definition Timer.cpp:57
std::string name
std::string connection
std::string expectedBody
std::string url

References web::http::client::Response::body, TestCase::connection, web::http::client::MasterRequest::disconnect(), dispatchNextRequest(), web::http::client::MasterRequest::end(), logger::ERROR, TestCase::expectedBody, TestCase::expectedStatus, failures, logger::INFO, web::http::client::MasterRequest::init(), web::http::client::MasterRequest::isConnected(), logger::LogMessage::LogMessage(), masterRequest, web::http::client::Request::method, TestCase::name, web::http::client::Request::set(), core::timer::Timer::singleshotTimer(), web::http::client::Response::statusCode, core::SNodeC::stop(), testCases, TestCase::url, and web::http::client::Request::url.

Referenced by dispatchNextRequest(), and run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dispatchNextRequest() [2/2]

void NextTester::dispatchNextRequest ( )
inlineprivate

Definition at line 122 of file testexpressnext.cpp.

122 {
123 if (testCases.empty()) {
124 LOG(INFO) << "All express next() tests executed. failures=" << failures;
125 if (masterRequest && masterRequest->isConnected()) {
126 masterRequest->disconnect();
127 }
129 [] {
131 },
132 1);
133 return;
134 }
135
136 if (!masterRequest || !masterRequest->isConnected()) {
137 ++failures;
138 LOG(ERROR) << "FAIL: master request not connected";
140 [] {
142 },
143 1);
144 return;
145 }
146
147 const TestCase current = testCases.front();
148 testCases.pop_front();
149
150 masterRequest->init();
151 masterRequest->method = "GET";
152 masterRequest->url = current.url;
153 masterRequest->set("Connection", current.connection);
154 masterRequest->end(
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());
157 const bool statusOk = std::stoi(res->statusCode) == current.expectedStatus;
158 const bool bodyOk = body.find(current.expectedBody) != std::string::npos;
159
160 if (statusOk && bodyOk) {
161 LOG(INFO) << "PASS: " << current.name << " status=" << res->statusCode << " body='" << body << "'";
162 } else {
163 ++failures;
164 LOG(ERROR) << "FAIL: " << current.name << " expected status=" << current.expectedStatus << " expected body fragment='"
165 << current.expectedBody << "'"
166 << " got status=" << res->statusCode << " body='" << body << "'";
167 }
168
170 },
171 [this, current]([[maybe_unused]] const std::shared_ptr<Request>& req, const std::string& reason) {
172 ++failures;
173 LOG(ERROR) << "FAIL: " << current.name << " parse-error: " << reason;
175 });
176 }

◆ getFailures() [1/2]

std::size_t NextTester::getFailures ( ) const
inline

Definition at line 112 of file testexpressnext.cpp.

112 {
113 return failures;
114 }

References failures.

Referenced by main().

Here is the caller graph for this function:

◆ getFailures() [2/2]

std::size_t NextTester::getFailures ( ) const
inline

Definition at line 112 of file testexpressnext.cpp.

112 {
113 return failures;
114 }

◆ run() [1/2]

void NextTester::run ( )
inline

Definition at line 87 of file testexpressnext.cpp.

87 {
88 client = std::make_shared<Client>( //
89 [this](const std::shared_ptr<MasterRequest>& connectedRequest) {
90 masterRequest = connectedRequest;
92 },
93 [](const std::shared_ptr<MasterRequest>&) {
94 });
95
96 client->connect( //
97 "localhost",
98 18080,
99 [this](const Client::SocketAddress&, const core::socket::State& state) {
100 if (state != core::socket::State::OK) {
101 ++failures;
102 LOG(ERROR) << "FAIL: connect failed: " << state.what();
104 [] {
106 },
107 1);
108 }
109 });
110 }
std::shared_ptr< Client > client
std::string what() const
Definition State.cpp:114
static constexpr int OK
Definition State.h:55

References client, net::in::stream::SocketClient< SocketConnectorT, ConfigSocketClientT, SocketContextFactoryT, Args >::connect(), dispatchNextRequest(), logger::ERROR, failures, logger::LogMessage::LogMessage(), masterRequest, core::socket::State::OK, core::socket::State::operator==(), core::timer::Timer::singleshotTimer(), core::SNodeC::stop(), and core::socket::State::what().

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ run() [2/2]

void NextTester::run ( )
inline

Definition at line 87 of file testexpressnext.cpp.

87 {
88 client = std::make_shared<Client>( //
89 [this](const std::shared_ptr<MasterRequest>& connectedRequest) {
90 masterRequest = connectedRequest;
92 },
93 [](const std::shared_ptr<MasterRequest>&) {
94 });
95
96 client->connect( //
97 "localhost",
98 18080,
99 [this](const Client::SocketAddress&, const core::socket::State& state) {
100 if (state != core::socket::State::OK) {
101 ++failures;
102 LOG(ERROR) << "FAIL: connect failed: " << state.what();
104 [] {
106 },
107 1);
108 }
109 });
110 }

Member Data Documentation

◆ client

std::shared_ptr< Client > NextTester::client
private

Definition at line 179 of file testexpressnext.cpp.

Referenced by run().

◆ failures

std::size_t NextTester::failures = 0
private

Definition at line 181 of file testexpressnext.cpp.

Referenced by dispatchNextRequest(), getFailures(), and run().

◆ masterRequest

std::shared_ptr< MasterRequest > NextTester::masterRequest
private

Definition at line 180 of file testexpressnext.cpp.

Referenced by dispatchNextRequest(), and run().

◆ testCases

std::deque< TestCase > NextTester::testCases
private

Definition at line 178 of file testexpressnext.cpp.

Referenced by dispatchNextRequest(), and NextTester().


The documentation for this class was generated from the following file: