1 #include "http/http_connection.hpp"
2 #include "http/http_request.hpp"
3 #include "http/http_response.hpp"
4 
5 #include <boost/asio/steady_timer.hpp>
6 #include <boost/beast/_experimental/test/stream.hpp>
7 
8 #include <filesystem>
9 #include <fstream>
10 #include <functional>
11 #include <memory>
12 #include <string>
13 
14 #include "gtest/gtest.h"
15 namespace crow
16 {
17 
18 struct FakeHandler
19 {
20     static void
21         handleUpgrade(Request& /*req*/,
22                       const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
23                       boost::beast::test::stream&& /*adaptor*/)
24     {
25         // Handle Upgrade should never be called
26         EXPECT_FALSE(true);
27     }
28 
29     void handle(Request& req,
30                 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/)
31     {
32         EXPECT_EQ(req.method(), boost::beast::http::verb::get);
33         EXPECT_EQ(req.target(), "/");
34         called = true;
35     }
36     bool called = false;
37 };
38 
39 static std::string getDateStr()
40 {
41     return "TestTime";
42 }
43 
44 TEST(http_connection, RequestPropogates)
45 {
46     boost::asio::io_context io;
47     boost::beast::test::stream stream(io);
48     boost::beast::test::stream out(io);
49     stream.connect(out);
50 
51     out.write_some(boost::asio::buffer(
52         "GET / HTTP/1.1\r\nHost: openbmc_project.xyz\r\nConnection: close\r\n\r\n"));
53     FakeHandler handler;
54     boost::asio::steady_timer timer(io);
55     std::function<std::string()> date(&getDateStr);
56     std::shared_ptr<crow::Connection<boost::beast::test::stream, FakeHandler>>
57         conn = std::make_shared<
58             crow::Connection<boost::beast::test::stream, FakeHandler>>(
59             &handler, std::move(timer), date, std::move(stream));
60     conn->start();
61     io.run_for(std::chrono::seconds(1000));
62     EXPECT_TRUE(handler.called);
63     std::string outStr = out.str();
64 
65     std::string expected =
66         "HTTP/1.1 200 OK\r\n"
67         "Connection: close\r\n"
68         "Strict-Transport-Security: max-age=31536000; includeSubdomains\r\n"
69         "X-Frame-Options: DENY\r\n"
70         "Pragma: no-cache\r\n"
71         "Cache-Control: no-store, max-age=0\r\n"
72         "X-Content-Type-Options: nosniff\r\n"
73         "Referrer-Policy: no-referrer\r\n"
74         "Permissions-Policy: accelerometer=(),ambient-light-sensor=(),autoplay=(),battery=(),camera=(),display-capture=(),document-domain=(),encrypted-media=(),fullscreen=(),gamepad=(),geolocation=(),gyroscope=(),layout-animations=(self),legacy-image-formats=(self),magnetometer=(),microphone=(),midi=(),oversized-images=(self),payment=(),picture-in-picture=(),publickey-credentials-get=(),speaker-selection=(),sync-xhr=(self),unoptimized-images=(self),unsized-media=(self),usb=(),screen-wak-lock=(),web-share=(),xr-spatial-tracking=()\r\n"
75         "X-Permitted-Cross-Domain-Policies: none\r\n"
76         "Cross-Origin-Embedder-Policy: require-corp\r\n"
77         "Cross-Origin-Opener-Policy: same-origin\r\n"
78         "Cross-Origin-Resource-Policy: same-origin\r\n"
79         "Content-Security-Policy: default-src 'none'; img-src 'self' data:; font-src 'self'; style-src 'self'; script-src 'self'; connect-src 'self' wss:; form-action 'none'; frame-ancestors 'none'; object-src 'none'; base-uri 'none'\r\n"
80         "Content-Length: 0\r\n"
81         "\r\n";
82     EXPECT_EQ(outStr, expected);
83 }
84 
85 } // namespace crow
86