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