blob: a08a8109c883ecb6b5b691f6779bc8a9bf1ee24b [file] [log] [blame]
Nan Zhoudff2f9b2022-06-26 23:50:39 +00001#include "async_resp.hpp"
Ed Tanous88a03c52022-03-14 10:16:07 -07002#include "http_request.hpp"
3#include "routing.hpp"
Nan Zhoudff2f9b2022-06-26 23:50:39 +00004#include "utility.hpp"
Ed Tanous88a03c52022-03-14 10:16:07 -07005
Nan Zhoudff2f9b2022-06-26 23:50:39 +00006#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 Tanous88a03c52022-03-14 10:16:07 -070014
Nan Zhouef740262022-06-26 23:55:13 +000015namespace crow
16{
17namespace
18{
19
20using ::crow::black_magic::getParameterTag;
21
Ed Tanous88a03c52022-03-14 10:16:07 -070022TEST(Router, AllowHeader)
23{
24 // Callback handler that does nothing
Nan Zhouef740262022-06-26 23:55:13 +000025 auto nullCallback = [](const Request&,
Ed Tanous88a03c52022-03-14 10:16:07 -070026 const std::shared_ptr<bmcweb::AsyncResp>&) {};
27
Nan Zhouef740262022-06-26 23:55:13 +000028 Router router;
Ed Tanous88a03c52022-03-14 10:16:07 -070029 std::error_code ec;
30
31 constexpr const std::string_view url = "/foo";
32
Nan Zhouef740262022-06-26 23:55:13 +000033 Request req{{boost::beast::http::verb::get, url, 11}, ec};
Ed Tanous88a03c52022-03-14 10:16:07 -070034
35 // No route should return no methods.
36 router.validate();
37 EXPECT_EQ(router.buildAllowHeader(req), "");
38
Nan Zhouef740262022-06-26 23:55:13 +000039 router.newRuleTagged<getParameterTag(url)>(std::string(url))
Ed Tanous88a03c52022-03-14 10:16:07 -070040 .methods(boost::beast::http::verb::get)(nullCallback);
41 router.validate();
42 EXPECT_EQ(router.buildAllowHeader(req), "GET");
43
Nan Zhouef740262022-06-26 23:55:13 +000044 router.newRuleTagged<getParameterTag(url)>(std::string(url))
Ed Tanous88a03c52022-03-14 10:16:07 -070045 .methods(boost::beast::http::verb::patch)(nullCallback);
46 router.validate();
47 EXPECT_EQ(router.buildAllowHeader(req), "GET, PATCH");
Nan Zhouef740262022-06-26 23:55:13 +000048}
49} // namespace
50} // namespace crow