Skip to content

Commit 6c0c79d

Browse files
committed
chore: route type tests
1 parent 59f6d1b commit 6c0c79d

File tree

3 files changed

+106
-7
lines changed

3 files changed

+106
-7
lines changed

include/boost/http_proto/server/route_handler.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,45 @@ struct BOOST_HTTP_PROTO_SYMBOL_VISIBLE
110110
route_params&
111111
set_body(std::string s);
112112

113+
/** Read the request body and receive a value.
114+
115+
This function reads the entire request body into the specified sink.
116+
When the read operation completes, the given callback is invoked with
117+
the result.
118+
119+
The @p callback parameter must be a function object with this
120+
equivalent signature, where `T` is the type produced by the value sink:
121+
@code
122+
void ( T&& t );
123+
@endcode
124+
125+
@par Example
126+
@code
127+
rp.read_body(
128+
capy::string_body_sink(),
129+
[]( std::string s )
130+
{
131+
// body read successfully
132+
});
133+
@endcode
134+
135+
If an error or an exception occurs during the read, it is propagated
136+
through the router to the next error or exception handler.
137+
138+
@param sink The body sink to read into.
139+
@param callback The function to call when the read completes.
140+
@return The route result, which must be returned immediately
141+
from the route handler.
142+
*/
143+
template<
144+
class ValueSink,
145+
class Callback>
146+
auto
147+
read_body(
148+
ValueSink&& sink,
149+
Callback&& callback) ->
150+
route_result;
151+
113152
#ifdef BOOST_HTTP_PROTO_HAS_CORO
114153

115154
/** Spawn a coroutine for this route.
@@ -205,6 +244,36 @@ struct BOOST_HTTP_PROTO_SYMBOL_VISIBLE
205244

206245
//-----------------------------------------------
207246

247+
template<
248+
class ValueSink,
249+
class Callback>
250+
auto
251+
route_params::
252+
read_body(
253+
ValueSink&& sink,
254+
Callback&& callback) ->
255+
route_result
256+
{
257+
(void)callback;
258+
/*
259+
return suspend(
260+
[&](resumer resume)
261+
{
262+
parser.read_body(
263+
std::forward<ValueSink>(sink),
264+
[resume, cb = std::forward<Callback>(callback)]
265+
(auto&&... args)
266+
{
267+
cb(std::forward<decltype(args)>(args)...);
268+
resume(route_result::next);
269+
});
270+
});
271+
*/
272+
return route::next;
273+
}
274+
275+
//-----------------------------------------------
276+
208277
template<class F>
209278
auto
210279
route_params::

src/server/router_types.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const char*
2121
route_cat_type::
2222
name() const noexcept
2323
{
24-
return "boost.http";
24+
return "boost.http.route";
2525
}
2626

2727
std::string
@@ -40,12 +40,12 @@ message(
4040
{
4141
switch(static_cast<route>(code))
4242
{
43-
case route::close: return "route::close";
44-
case route::complete: return "route::complete";
45-
case route::suspend: return "route::suspend";
46-
case route::next: return "route::next";
47-
case route::next_route: return "route::next_route";
48-
case route::send: return "route::send";
43+
case route::close: return "close";
44+
case route::complete: return "complete";
45+
case route::suspend: return "suspend";
46+
case route::next: return "next";
47+
case route::next_route: return "next_route";
48+
case route::send: return "send";
4949
default:
5050
return "?";
5151
}

test/unit/server/router_types.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,39 @@ namespace http_proto {
1818

1919
struct router_types_test
2020
{
21+
template<typename Error>
22+
void
23+
check(
24+
char const* name,
25+
Error ev)
26+
{
27+
auto const ec = make_error_code(ev);
28+
BOOST_TEST(std::string(ec.category().name()) == name);
29+
BOOST_TEST(! ec.message().empty());
30+
BOOST_TEST(
31+
std::addressof(ec.category()) ==
32+
std::addressof(make_error_code(ev).category()));
33+
BOOST_TEST(ec.category().equivalent(
34+
static_cast<typename std::underlying_type<Error>::type>(ev),
35+
ec.category().default_error_condition(
36+
static_cast<typename std::underlying_type<Error>::type>(ev))));
37+
BOOST_TEST(ec.category().equivalent(ec,
38+
static_cast<typename std::underlying_type<Error>::type>(ev)));
39+
}
40+
2141
void
2242
run()
2343
{
44+
{
45+
char const* const n = "boost.http.route";
46+
check(n, route::close);
47+
check(n, route::complete);
48+
check(n, route::suspend);
49+
check(n, route::next);
50+
check(n, route::next_route);
51+
check(n, route::send);
52+
}
53+
2454
basic_router<route_params_base> r;
2555
r.add(http_proto::method::post, "/",
2656
[](route_params_base& rp) ->

0 commit comments

Comments
 (0)