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