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 | |
| 28 | using ::crow::black_magic::getParameterTag; |
| 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 | |
| 39 | constexpr const std::string_view url = "/foo"; |
| 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(); |
| 45 | EXPECT_EQ(router.buildAllowHeader(req), ""); |
| 46 | |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 47 | router.newRuleTagged<getParameterTag(url)>(std::string(url)) |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 48 | .methods(boost::beast::http::verb::get)(nullCallback); |
| 49 | router.validate(); |
| 50 | EXPECT_EQ(router.buildAllowHeader(req), "GET"); |
| 51 | |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 52 | router.newRuleTagged<getParameterTag(url)>(std::string(url)) |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 53 | .methods(boost::beast::http::verb::patch)(nullCallback); |
| 54 | router.validate(); |
| 55 | EXPECT_EQ(router.buildAllowHeader(req), "GET, PATCH"); |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 56 | } |
| 57 | } // namespace |
| 58 | } // namespace crow |