Ed Tanous | 478b7ad | 2024-07-15 19:11:54 -0700 | [diff] [blame] | 1 | #include "async_resp.hpp" |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 2 | #include "http_request.hpp" |
| 3 | #include "routing.hpp" |
Nan Zhou | dff2f9b | 2022-06-26 23:50:39 +0000 | [diff] [blame] | 4 | #include "utility.hpp" |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 5 | |
Nan Zhou | dff2f9b | 2022-06-26 23:50:39 +0000 | [diff] [blame] | 6 | #include <boost/beast/http/verb.hpp> |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | #include <string_view> |
| 11 | #include <system_error> |
| 12 | |
Ed Tanous | 478b7ad | 2024-07-15 19:11:54 -0700 | [diff] [blame] | 13 | #include <gtest/gtest.h> |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 14 | |
Nan Zhou | d5c80ad | 2022-07-11 01:16:31 +0000 | [diff] [blame] | 15 | // IWYU pragma: no_forward_declare bmcweb::AsyncResp |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 16 | |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 17 | namespace crow |
| 18 | { |
| 19 | namespace |
| 20 | { |
| 21 | |
Ed Tanous | 47488a9 | 2023-06-26 18:19:33 -0700 | [diff] [blame] | 22 | using ::crow::utility::getParameterTag; |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 23 | |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 24 | TEST(Router, AllowHeader) |
| 25 | { |
| 26 | // Callback handler that does nothing |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 27 | auto nullCallback = |
| 28 | [](const Request&, const std::shared_ptr<bmcweb::AsyncResp>&) {}; |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 29 | |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 30 | Router router; |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 31 | std::error_code ec; |
| 32 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 33 | constexpr std::string_view url = "/foo"; |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 34 | |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 35 | Request req{{boost::beast::http::verb::get, url, 11}, ec}; |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 36 | |
| 37 | // No route should return no methods. |
| 38 | router.validate(); |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 39 | EXPECT_EQ(router.findRoute(req).allowHeader, ""); |
| 40 | EXPECT_EQ(router.findRoute(req).route.rule, nullptr); |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 41 | |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 42 | router.newRuleTagged<getParameterTag(url)>(std::string(url)) |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 43 | .methods(boost::beast::http::verb::get)(nullCallback); |
| 44 | router.validate(); |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 45 | EXPECT_EQ(router.findRoute(req).allowHeader, "GET"); |
| 46 | EXPECT_NE(router.findRoute(req).route.rule, nullptr); |
| 47 | |
| 48 | Request patchReq{{boost::beast::http::verb::patch, url, 11}, ec}; |
| 49 | EXPECT_EQ(router.findRoute(patchReq).route.rule, nullptr); |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 50 | |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 51 | router.newRuleTagged<getParameterTag(url)>(std::string(url)) |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 52 | .methods(boost::beast::http::verb::patch)(nullCallback); |
| 53 | router.validate(); |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 54 | EXPECT_EQ(router.findRoute(req).allowHeader, "GET, PATCH"); |
| 55 | EXPECT_NE(router.findRoute(req).route.rule, nullptr); |
| 56 | EXPECT_NE(router.findRoute(patchReq).route.rule, nullptr); |
| 57 | } |
| 58 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 59 | TEST(Router, OverlapingRoutes) |
| 60 | { |
| 61 | // Callback handler that does nothing |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 62 | auto fooCallback = |
| 63 | [](const Request&, const std::shared_ptr<bmcweb::AsyncResp>&) { |
| 64 | EXPECT_FALSE(true); |
| 65 | }; |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 66 | bool barCalled = false; |
| 67 | auto foobarCallback = |
| 68 | [&barCalled](const Request&, const std::shared_ptr<bmcweb::AsyncResp>&, |
| 69 | const std::string& bar) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 70 | barCalled = true; |
| 71 | EXPECT_EQ(bar, "bar"); |
| 72 | }; |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 73 | |
| 74 | Router router; |
| 75 | std::error_code ec; |
| 76 | |
| 77 | router.newRuleTagged<getParameterTag("/foo/<str>")>("/foo/<str>")( |
| 78 | foobarCallback); |
| 79 | router.newRuleTagged<getParameterTag("/foo")>("/foo")(fooCallback); |
| 80 | router.validate(); |
| 81 | { |
| 82 | constexpr std::string_view url = "/foo/bar"; |
| 83 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 84 | auto req = std::make_shared<Request>( |
| 85 | Request::Body{boost::beast::http::verb::get, url, 11}, ec); |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 86 | |
| 87 | std::shared_ptr<bmcweb::AsyncResp> asyncResp = |
| 88 | std::make_shared<bmcweb::AsyncResp>(); |
| 89 | |
| 90 | router.handle(req, asyncResp); |
| 91 | } |
| 92 | EXPECT_TRUE(barCalled); |
| 93 | } |
| 94 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 95 | TEST(Router, 404) |
| 96 | { |
| 97 | bool notFoundCalled = false; |
| 98 | // Callback handler that does nothing |
| 99 | auto nullCallback = |
| 100 | [¬FoundCalled](const Request&, |
| 101 | const std::shared_ptr<bmcweb::AsyncResp>&) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 102 | notFoundCalled = true; |
| 103 | }; |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 104 | |
| 105 | Router router; |
| 106 | std::error_code ec; |
| 107 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 108 | constexpr std::string_view url = "/foo/bar"; |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 109 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 110 | auto req = std::make_shared<Request>( |
| 111 | Request::Body{boost::beast::http::verb::get, url, 11}, ec); |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 112 | |
| 113 | router.newRuleTagged<getParameterTag(url)>("/foo/<path>") |
| 114 | .notFound()(nullCallback); |
| 115 | router.validate(); |
| 116 | { |
| 117 | std::shared_ptr<bmcweb::AsyncResp> asyncResp = |
| 118 | std::make_shared<bmcweb::AsyncResp>(); |
| 119 | |
| 120 | router.handle(req, asyncResp); |
| 121 | } |
| 122 | EXPECT_TRUE(notFoundCalled); |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 123 | } |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 124 | |
| 125 | TEST(Router, 405) |
| 126 | { |
| 127 | // Callback handler that does nothing |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 128 | auto nullCallback = |
| 129 | [](const Request&, const std::shared_ptr<bmcweb::AsyncResp>&) {}; |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 130 | bool called = false; |
| 131 | auto notAllowedCallback = |
| 132 | [&called](const Request&, const std::shared_ptr<bmcweb::AsyncResp>&) { |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 133 | called = true; |
| 134 | }; |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 135 | |
| 136 | Router router; |
| 137 | std::error_code ec; |
| 138 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 139 | constexpr std::string_view url = "/foo/bar"; |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 140 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 141 | auto req = std::make_shared<Request>( |
| 142 | Request::Body{boost::beast::http::verb::patch, url, 11}, ec); |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 143 | |
| 144 | router.newRuleTagged<getParameterTag(url)>(std::string(url)) |
| 145 | .methods(boost::beast::http::verb::get)(nullCallback); |
| 146 | router.newRuleTagged<getParameterTag(url)>("/foo/<path>") |
| 147 | .methodNotAllowed()(notAllowedCallback); |
| 148 | router.validate(); |
| 149 | { |
| 150 | std::shared_ptr<bmcweb::AsyncResp> asyncResp = |
| 151 | std::make_shared<bmcweb::AsyncResp>(); |
| 152 | |
| 153 | router.handle(req, asyncResp); |
| 154 | } |
| 155 | EXPECT_TRUE(called); |
| 156 | } |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 157 | } // namespace |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 158 | } // namespace crow |