blob: 6647e61d99cd33749a8ba617fe3b7f413df211e5 [file] [log] [blame]
Ed Tanous4fa45df2023-09-01 14:20:50 -07001#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"
15namespace crow
16{
17
18struct 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(), "/");
Ed Tanous998e0cb2023-09-06 13:57:30 -070034 EXPECT_EQ(req.getHeaderValue(boost::beast::http::field::host),
35 "openbmc_project.xyz");
36 EXPECT_FALSE(req.keepAlive());
37 EXPECT_EQ(req.version(), 11);
38 EXPECT_EQ(req.body(), "");
39
Ed Tanous4fa45df2023-09-01 14:20:50 -070040 called = true;
41 }
42 bool called = false;
43};
44
Ed Tanous998e0cb2023-09-06 13:57:30 -070045struct ClockFake
Ed Tanous4fa45df2023-09-01 14:20:50 -070046{
Ed Tanous998e0cb2023-09-06 13:57:30 -070047 bool wascalled = false;
48 std::string getDateStr()
49 {
50 wascalled = true;
51 return "TestTime";
52 }
53};
Ed Tanous4fa45df2023-09-01 14:20:50 -070054
55TEST(http_connection, RequestPropogates)
56{
57 boost::asio::io_context io;
Ed Tanous998e0cb2023-09-06 13:57:30 -070058 ClockFake clock;
Ed Tanous4fa45df2023-09-01 14:20:50 -070059 boost::beast::test::stream stream(io);
60 boost::beast::test::stream out(io);
61 stream.connect(out);
62
63 out.write_some(boost::asio::buffer(
64 "GET / HTTP/1.1\r\nHost: openbmc_project.xyz\r\nConnection: close\r\n\r\n"));
65 FakeHandler handler;
66 boost::asio::steady_timer timer(io);
Ed Tanous998e0cb2023-09-06 13:57:30 -070067 std::function<std::string()> date(
68 std::bind_front(&ClockFake::getDateStr, &clock));
Ed Tanous4fa45df2023-09-01 14:20:50 -070069 std::shared_ptr<crow::Connection<boost::beast::test::stream, FakeHandler>>
70 conn = std::make_shared<
71 crow::Connection<boost::beast::test::stream, FakeHandler>>(
72 &handler, std::move(timer), date, std::move(stream));
73 conn->start();
74 io.run_for(std::chrono::seconds(1000));
75 EXPECT_TRUE(handler.called);
76 std::string outStr = out.str();
77
78 std::string expected =
79 "HTTP/1.1 200 OK\r\n"
80 "Connection: close\r\n"
81 "Strict-Transport-Security: max-age=31536000; includeSubdomains\r\n"
82 "X-Frame-Options: DENY\r\n"
83 "Pragma: no-cache\r\n"
84 "Cache-Control: no-store, max-age=0\r\n"
85 "X-Content-Type-Options: nosniff\r\n"
86 "Referrer-Policy: no-referrer\r\n"
87 "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"
88 "X-Permitted-Cross-Domain-Policies: none\r\n"
89 "Cross-Origin-Embedder-Policy: require-corp\r\n"
90 "Cross-Origin-Opener-Policy: same-origin\r\n"
91 "Cross-Origin-Resource-Policy: same-origin\r\n"
92 "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"
Ed Tanous998e0cb2023-09-06 13:57:30 -070093 "Date: TestTime\r\n"
94 "Content-Length: 0\r\n\r\n";
Ed Tanous4fa45df2023-09-01 14:20:50 -070095 EXPECT_EQ(outStr, expected);
Ed Tanous998e0cb2023-09-06 13:57:30 -070096 EXPECT_TRUE(clock.wascalled);
Ed Tanous4fa45df2023-09-01 14:20:50 -070097}
98
99} // namespace crow