Skip to content

Commit f8407ab

Browse files
committed
feat: route_params::is_method
1 parent 47ebf32 commit f8407ab

4 files changed

Lines changed: 65 additions & 1 deletion

File tree

include/boost/http_proto/server/router.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
namespace boost {
1717
namespace http_proto {
1818

19+
struct route_params;
20+
1921
/** A router for HTTP servers
2022
2123
This is a specialization of `basic_router` using

include/boost/http_proto/server/router_types.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,20 @@ class basic_router;
299299
class route_params_base
300300
{
301301
public:
302+
/** Return true if the request method matches `m`
303+
*/
304+
bool is_method(
305+
http_proto::method m) const noexcept
306+
{
307+
return verb_ == m;
308+
}
309+
310+
/** Return true if the request method matches `s`
311+
*/
312+
BOOST_HTTP_PROTO_DECL
313+
bool is_method(
314+
core::string_view s) const noexcept;
315+
302316
/** The mount path of the current router
303317
304318
This is the portion of the request path

src/server/router_types.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99

1010
#include <boost/http_proto/server/router_types.hpp>
11+
#include <boost/url/grammar/ci_string.hpp>
1112
#include <boost/assert.hpp>
1213
#include <cstring>
1314

@@ -78,5 +79,16 @@ do_detach()
7879
detail::throw_logic_error();
7980
}
8081

82+
bool
83+
route_params_base::
84+
is_method(
85+
core::string_view s) const noexcept
86+
{
87+
auto m = http_proto::string_to_method(s);
88+
if(m != http_proto::method::unknown)
89+
return verb_ == m;
90+
return s == verb_str_;
91+
}
92+
8193
} // http_proto
8294
} // boost

test/unit/server/router_types.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot com)
2+
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
33
//
44
// Distributed under the Boost Software License, Version 1.0. (See accompanying
55
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -9,3 +9,39 @@
99

1010
// Test that header file is self-contained.
1111
#include <boost/http_proto/server/router_types.hpp>
12+
#include <boost/http_proto/server/router.hpp>
13+
14+
#include "test_suite.hpp"
15+
16+
namespace boost {
17+
namespace http_proto {
18+
19+
struct router_types_test
20+
{
21+
void
22+
run()
23+
{
24+
basic_router<route_params_base> r;
25+
r.add(http_proto::method::post, "/",
26+
[](route_params_base& rp) ->
27+
route_result
28+
{
29+
BOOST_TEST( rp.is_method(http_proto::method::post));
30+
BOOST_TEST(! rp.is_method(http_proto::method::get));
31+
BOOST_TEST(! rp.is_method("GET"));
32+
BOOST_TEST(! rp.is_method("Post"));
33+
BOOST_TEST( rp.is_method("POST"));
34+
return route::send;
35+
});
36+
route_params_base rp;
37+
auto rv = r.dispatch("POST", "/", rp);
38+
BOOST_TEST(rv = route::send);
39+
}
40+
};
41+
42+
TEST_SUITE(
43+
router_types_test,
44+
"boost.http_proto.server.router_types");
45+
46+
} // http_proto
47+
} // boost

0 commit comments

Comments
 (0)