blob: 4dda70ecf834d40e88ac2bcfbe2a5a3637387b98 [file] [log] [blame]
Ed Tanousf0b59af2024-03-20 13:38:04 -07001#include "async_resp.hpp"
Ed Tanous4fa45df2023-09-01 14:20:50 -07002#include "http/http_connection.hpp"
3#include "http/http_request.hpp"
4#include "http/http_response.hpp"
5
Ed Tanousf0b59af2024-03-20 13:38:04 -07006#include <boost/asio/buffer.hpp>
7#include <boost/asio/io_context.hpp>
Ed Tanous4fa45df2023-09-01 14:20:50 -07008#include <boost/asio/steady_timer.hpp>
9#include <boost/beast/_experimental/test/stream.hpp>
Ed Tanousf0b59af2024-03-20 13:38:04 -070010#include <boost/beast/http/field.hpp>
11#include <boost/beast/http/verb.hpp>
Ed Tanous4fa45df2023-09-01 14:20:50 -070012
Ed Tanousf0b59af2024-03-20 13:38:04 -070013#include <chrono>
Ed Tanous4fa45df2023-09-01 14:20:50 -070014#include <functional>
15#include <memory>
16#include <string>
Ed Tanousf0b59af2024-03-20 13:38:04 -070017#include <utility>
Ed Tanous4fa45df2023-09-01 14:20:50 -070018
19#include "gtest/gtest.h"
20namespace crow
21{
22
23struct FakeHandler
24{
25 static void
26 handleUpgrade(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
34 void handle(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(), "/");
Ed Tanous998e0cb2023-09-06 13:57:30 -070039 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
Ed Tanous4fa45df2023-09-01 14:20:50 -070045 called = true;
46 }
47 bool called = false;
48};
49
Ed Tanous998e0cb2023-09-06 13:57:30 -070050struct ClockFake
Ed Tanous4fa45df2023-09-01 14:20:50 -070051{
Ed Tanous998e0cb2023-09-06 13:57:30 -070052 bool wascalled = false;
53 std::string getDateStr()
54 {
55 wascalled = true;
56 return "TestTime";
57 }
58};
Ed Tanous4fa45df2023-09-01 14:20:50 -070059
60TEST(http_connection, RequestPropogates)
61{
62 boost::asio::io_context io;
Ed Tanous998e0cb2023-09-06 13:57:30 -070063 ClockFake clock;
Ed Tanous4fa45df2023-09-01 14:20:50 -070064 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);
Ed Tanous998e0cb2023-09-06 13:57:30 -070072 std::function<std::string()> date(
73 std::bind_front(&ClockFake::getDateStr, &clock));
Ed Tanous4fa45df2023-09-01 14:20:50 -070074 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 "X-Frame-Options: DENY\r\n"
88 "Pragma: no-cache\r\n"
89 "Cache-Control: no-store, max-age=0\r\n"
90 "X-Content-Type-Options: nosniff\r\n"
91 "Referrer-Policy: no-referrer\r\n"
92 "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"
93 "X-Permitted-Cross-Domain-Policies: none\r\n"
94 "Cross-Origin-Embedder-Policy: require-corp\r\n"
95 "Cross-Origin-Opener-Policy: same-origin\r\n"
96 "Cross-Origin-Resource-Policy: same-origin\r\n"
97 "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 -070098 "Date: TestTime\r\n"
99 "Content-Length: 0\r\n\r\n";
Ed Tanous4fa45df2023-09-01 14:20:50 -0700100 EXPECT_EQ(outStr, expected);
Ed Tanous998e0cb2023-09-06 13:57:30 -0700101 EXPECT_TRUE(clock.wascalled);
Ed Tanous4fa45df2023-09-01 14:20:50 -0700102}
103
104} // namespace crow