Skip to content

Commit 705e5f7

Browse files
committed
more http_worker tests for unhandled routes
1 parent 3ee2f01 commit 705e5f7

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

test/unit/server/http_worker.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,72 @@ struct http_worker_test
7575
status::ok, R"({"ok":true})");
7676
}
7777

78+
void
79+
testRouteOutcomes()
80+
{
81+
// route_next: handler declines, path has
82+
// a route so router returns 405
83+
check(GET, "/x", h_next,
84+
status::method_not_allowed);
85+
86+
// route_next_route: skip route, path has
87+
// a route so router returns 405
88+
check(GET, "/x", h_next_route,
89+
status::method_not_allowed);
90+
91+
// route_next via middleware -> 404
92+
{
93+
test_router r;
94+
r.use(h_next);
95+
check(r, GET, "/x",
96+
status::not_found);
97+
}
98+
99+
// route_close: session ends, no response
100+
{
101+
test_router r;
102+
r.add(GET, "/bye", h_close);
103+
auto raw = exchange_raw(r, GET, "/bye");
104+
BOOST_TEST(raw.empty());
105+
}
106+
107+
// route_error: handler fails -> 500
108+
check(GET, "/err",
109+
h_error(http::error::bad_content_length),
110+
status::internal_server_error);
111+
112+
// Empty router -> 404
113+
{
114+
test_router r;
115+
check(r, GET, "/anything",
116+
status::not_found);
117+
}
118+
119+
// route_next falls through to second handler
120+
{
121+
test_router r;
122+
r.route("/x")
123+
.add(GET, h_next)
124+
.add(GET, h_text("ok"));
125+
check(r, GET, "/x",
126+
status::ok, "ok");
127+
}
128+
129+
// route_next_route skips entire first route
130+
{
131+
test_router r;
132+
r.add(GET, "/x", h_next_route);
133+
r.add(GET, "/x", h_text("second"));
134+
check(r, GET, "/x",
135+
status::ok, "second");
136+
}
137+
}
138+
78139
void
79140
run()
80141
{
81142
testBasicRoute();
143+
testRouteOutcomes();
82144
}
83145
};
84146

test/unit/server/test_worker.hpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,29 @@ struct test_worker
9999
};
100100
}
101101

102+
/** Handler that returns route_next (decline). */
103+
static auto
104+
h_next(route_params&) -> route_task
105+
{ co_return route_next; }
106+
107+
/** Handler that returns route_next_route. */
108+
static auto
109+
h_next_route(route_params&) -> route_task
110+
{ co_return route_next_route; }
111+
112+
/** Handler that returns route_close. */
113+
static auto
114+
h_close(route_params&) -> route_task
115+
{ co_return route_close; }
116+
117+
/** Handler that returns route_error. */
118+
static auto
119+
h_error(system::error_code ec)
120+
{
121+
return [ec](route_params&) -> route_task
122+
{ co_return route_error(ec); };
123+
}
124+
102125
/** Handler that sends 200 with text body and
103126
a custom Content-Type.
104127
*/
@@ -192,6 +215,42 @@ struct test_worker
192215
// Run one request/response exchange
193216
//--------------------------------------------
194217

218+
/** Run a raw request and return the raw
219+
bytes written by the worker.
220+
*/
221+
static std::string
222+
exchange_raw(
223+
test_router const& r,
224+
std::string_view request)
225+
{
226+
auto [client, server] =
227+
capy::test::make_stream_pair();
228+
229+
http_worker w(
230+
server,
231+
test_router(r),
232+
pcfg(),
233+
scfg());
234+
235+
client.provide(request);
236+
client.close();
237+
238+
capy::test::run_blocking()(
239+
w.do_http_session());
240+
241+
return std::string(client.data());
242+
}
243+
244+
static std::string
245+
exchange_raw(
246+
test_router const& r,
247+
http::method m,
248+
std::string_view path)
249+
{
250+
return exchange_raw(r,
251+
make_request(m, path));
252+
}
253+
195254
/** Run a raw request through a worker and
196255
return the parsed response.
197256
*/

0 commit comments

Comments
 (0)