blob: 5e58f62f1a05e0a468692017bcdc8130d6d81096 [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanousf0b59af2024-03-20 13:38:04 -07003#include "async_resp.hpp"
Ed Tanous4fa45df2023-09-01 14:20:50 -07004#include "http/http_connection.hpp"
5#include "http/http_request.hpp"
6#include "http/http_response.hpp"
7
Ed Tanousf0b59af2024-03-20 13:38:04 -07008#include <boost/asio/buffer.hpp>
9#include <boost/asio/io_context.hpp>
Ed Tanous4fa45df2023-09-01 14:20:50 -070010#include <boost/asio/steady_timer.hpp>
11#include <boost/beast/_experimental/test/stream.hpp>
Ed Tanousf0b59af2024-03-20 13:38:04 -070012#include <boost/beast/http/field.hpp>
13#include <boost/beast/http/verb.hpp>
Ed Tanous4fa45df2023-09-01 14:20:50 -070014
Ed Tanousf0b59af2024-03-20 13:38:04 -070015#include <chrono>
Ed Tanous4fa45df2023-09-01 14:20:50 -070016#include <functional>
17#include <memory>
18#include <string>
Ed Tanousf0b59af2024-03-20 13:38:04 -070019#include <utility>
Ed Tanous4fa45df2023-09-01 14:20:50 -070020
21#include "gtest/gtest.h"
22namespace crow
23{
24
25struct FakeHandler
26{
Patrick Williams504af5a2025-02-03 14:29:03 -050027 static void handleUpgrade(
28 const std::shared_ptr<Request>& /*req*/,
29 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
30 boost::beast::test::stream&& /*adaptor*/)
Ed Tanous4fa45df2023-09-01 14:20:50 -070031 {
32 // Handle Upgrade should never be called
33 EXPECT_FALSE(true);
34 }
35
Jonathan Doman102a4cd2024-04-15 16:56:23 -070036 void handle(const std::shared_ptr<Request>& req,
Ed Tanous4fa45df2023-09-01 14:20:50 -070037 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/)
38 {
Jonathan Doman102a4cd2024-04-15 16:56:23 -070039 EXPECT_EQ(req->method(), boost::beast::http::verb::get);
40 EXPECT_EQ(req->target(), "/");
41 EXPECT_EQ(req->getHeaderValue(boost::beast::http::field::host),
Ed Tanous998e0cb2023-09-06 13:57:30 -070042 "openbmc_project.xyz");
Jonathan Doman102a4cd2024-04-15 16:56:23 -070043 EXPECT_FALSE(req->keepAlive());
44 EXPECT_EQ(req->version(), 11);
45 EXPECT_EQ(req->body(), "");
Ed Tanous998e0cb2023-09-06 13:57:30 -070046
Ed Tanous4fa45df2023-09-01 14:20:50 -070047 called = true;
48 }
49 bool called = false;
50};
51
Ed Tanous998e0cb2023-09-06 13:57:30 -070052struct ClockFake
Ed Tanous4fa45df2023-09-01 14:20:50 -070053{
Ed Tanous998e0cb2023-09-06 13:57:30 -070054 bool wascalled = false;
55 std::string getDateStr()
56 {
57 wascalled = true;
58 return "TestTime";
59 }
60};
Ed Tanous4fa45df2023-09-01 14:20:50 -070061
62TEST(http_connection, RequestPropogates)
63{
64 boost::asio::io_context io;
Ed Tanous998e0cb2023-09-06 13:57:30 -070065 ClockFake clock;
Ed Tanous4fa45df2023-09-01 14:20:50 -070066 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);
Ed Tanous998e0cb2023-09-06 13:57:30 -070074 std::function<std::string()> date(
75 std::bind_front(&ClockFake::getDateStr, &clock));
Ed Tanous4fa45df2023-09-01 14:20:50 -070076 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"
Ed Tanous4fa45df2023-09-01 14:20:50 -070089 "Pragma: no-cache\r\n"
90 "Cache-Control: no-store, max-age=0\r\n"
91 "X-Content-Type-Options: nosniff\r\n"
Ed Tanous998e0cb2023-09-06 13:57:30 -070092 "Date: TestTime\r\n"
93 "Content-Length: 0\r\n\r\n";
Ed Tanous4fa45df2023-09-01 14:20:50 -070094 EXPECT_EQ(outStr, expected);
Ed Tanous998e0cb2023-09-06 13:57:30 -070095 EXPECT_TRUE(clock.wascalled);
Ed Tanous4fa45df2023-09-01 14:20:50 -070096}
97
98} // namespace crow