Nan Zhou | dff2f9b | 2022-06-26 23:50:39 +0000 | [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 | |
| 13 | #include <gtest/gtest.h> |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 14 | |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 15 | namespace crow |
| 16 | { |
| 17 | namespace |
| 18 | { |
| 19 | |
| 20 | using ::crow::black_magic::getParameterTag; |
| 21 | |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 22 | TEST(Router, AllowHeader) |
| 23 | { |
| 24 | // Callback handler that does nothing |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 25 | auto nullCallback = [](const Request&, |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 26 | const std::shared_ptr<bmcweb::AsyncResp>&) {}; |
| 27 | |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 28 | Router router; |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 29 | std::error_code ec; |
| 30 | |
| 31 | constexpr const std::string_view url = "/foo"; |
| 32 | |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 33 | Request req{{boost::beast::http::verb::get, url, 11}, ec}; |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 34 | |
| 35 | // No route should return no methods. |
| 36 | router.validate(); |
| 37 | EXPECT_EQ(router.buildAllowHeader(req), ""); |
| 38 | |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 39 | router.newRuleTagged<getParameterTag(url)>(std::string(url)) |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 40 | .methods(boost::beast::http::verb::get)(nullCallback); |
| 41 | router.validate(); |
| 42 | EXPECT_EQ(router.buildAllowHeader(req), "GET"); |
| 43 | |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 44 | router.newRuleTagged<getParameterTag(url)>(std::string(url)) |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 45 | .methods(boost::beast::http::verb::patch)(nullCallback); |
| 46 | router.validate(); |
| 47 | EXPECT_EQ(router.buildAllowHeader(req), "GET, PATCH"); |
Nan Zhou | ef74026 | 2022-06-26 23:55:13 +0000 | [diff] [blame] | 48 | } |
| 49 | } // namespace |
| 50 | } // namespace crow |