xref: /openbmc/bmcweb/test/http/http_connection_test.cpp (revision 40e9b92ec19acffb46f83a6e55b18974da5d708e)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #include "async_resp.hpp"
4 #include "http/http_connection.hpp"
5 #include "http/http_request.hpp"
6 #include "http/http_response.hpp"
7 
8 #include <boost/asio/buffer.hpp>
9 #include <boost/asio/io_context.hpp>
10 #include <boost/asio/steady_timer.hpp>
11 #include <boost/beast/_experimental/test/stream.hpp>
12 #include <boost/beast/http/field.hpp>
13 #include <boost/beast/http/verb.hpp>
14 
15 #include <chrono>
16 #include <functional>
17 #include <memory>
18 #include <string>
19 #include <utility>
20 
21 #include "gtest/gtest.h"
22 namespace crow
23 {
24 
25 struct FakeHandler
26 {
27     static void
28         handleUpgrade(const std::shared_ptr<Request>& /*req*/,
29                       const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
30                       boost::beast::test::stream&& /*adaptor*/)
31     {
32         // Handle Upgrade should never be called
33         EXPECT_FALSE(true);
34     }
35 
36     void handle(const std::shared_ptr<Request>& req,
37                 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/)
38     {
39         EXPECT_EQ(req->method(), boost::beast::http::verb::get);
40         EXPECT_EQ(req->target(), "/");
41         EXPECT_EQ(req->getHeaderValue(boost::beast::http::field::host),
42                   "openbmc_project.xyz");
43         EXPECT_FALSE(req->keepAlive());
44         EXPECT_EQ(req->version(), 11);
45         EXPECT_EQ(req->body(), "");
46 
47         called = true;
48     }
49     bool called = false;
50 };
51 
52 struct ClockFake
53 {
54     bool wascalled = false;
55     std::string getDateStr()
56     {
57         wascalled = true;
58         return "TestTime";
59     }
60 };
61 
62 TEST(http_connection, RequestPropogates)
63 {
64     boost::asio::io_context io;
65     ClockFake clock;
66     boost::beast::test::stream stream(io);
67     boost::beast::test::stream out(io);
68     stream.connect(out);
69 
70     out.write_some(boost::asio::buffer(
71         "GET / HTTP/1.1\r\nHost: openbmc_project.xyz\r\nConnection: close\r\n\r\n"));
72     FakeHandler handler;
73     boost::asio::steady_timer timer(io);
74     std::function<std::string()> date(
75         std::bind_front(&ClockFake::getDateStr, &clock));
76     std::shared_ptr<crow::Connection<boost::beast::test::stream, FakeHandler>>
77         conn = std::make_shared<
78             crow::Connection<boost::beast::test::stream, FakeHandler>>(
79             &handler, std::move(timer), date, std::move(stream));
80     conn->start();
81     io.run_for(std::chrono::seconds(1000));
82     EXPECT_TRUE(handler.called);
83     std::string outStr = out.str();
84 
85     std::string expected =
86         "HTTP/1.1 200 OK\r\n"
87         "Connection: close\r\n"
88         "Strict-Transport-Security: max-age=31536000; includeSubdomains\r\n"
89         "Pragma: no-cache\r\n"
90         "Cache-Control: no-store, max-age=0\r\n"
91         "X-Content-Type-Options: nosniff\r\n"
92         "Date: TestTime\r\n"
93         "Content-Length: 0\r\n\r\n";
94     EXPECT_EQ(outStr, expected);
95     EXPECT_TRUE(clock.wascalled);
96 }
97 
98 } // namespace crow
99