blob: 69cf21a7a977f80754c645bba674a8c1b81debb1 [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"
Ed Tanous796ba932020-08-02 04:29:21 +00007#include "http_connect_types.hpp"
8#include "test_stream.hpp"
Ed Tanous4fa45df2023-09-01 14:20:50 -07009
Ed Tanousf0b59af2024-03-20 13:38:04 -070010#include <boost/asio/buffer.hpp>
11#include <boost/asio/io_context.hpp>
Ed Tanous796ba932020-08-02 04:29:21 +000012#include <boost/asio/ssl/context.hpp>
Ed Tanous4fa45df2023-09-01 14:20:50 -070013#include <boost/asio/steady_timer.hpp>
14#include <boost/beast/_experimental/test/stream.hpp>
Ed Tanousf0b59af2024-03-20 13:38:04 -070015#include <boost/beast/http/field.hpp>
16#include <boost/beast/http/verb.hpp>
Ed Tanous4fa45df2023-09-01 14:20:50 -070017
Ed Tanousf0b59af2024-03-20 13:38:04 -070018#include <chrono>
Ed Tanous4fa45df2023-09-01 14:20:50 -070019#include <functional>
20#include <memory>
21#include <string>
Ed Tanousf0b59af2024-03-20 13:38:04 -070022#include <utility>
Ed Tanous4fa45df2023-09-01 14:20:50 -070023
24#include "gtest/gtest.h"
25namespace crow
26{
27
28struct FakeHandler
29{
Ed Tanous796ba932020-08-02 04:29:21 +000030 template <typename Adaptor>
Patrick Williams504af5a2025-02-03 14:29:03 -050031 static void handleUpgrade(
32 const std::shared_ptr<Request>& /*req*/,
33 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
Ed Tanous796ba932020-08-02 04:29:21 +000034 Adaptor&& /*adaptor*/)
Ed Tanous4fa45df2023-09-01 14:20:50 -070035 {
36 // Handle Upgrade should never be called
37 EXPECT_FALSE(true);
38 }
39
Jonathan Doman102a4cd2024-04-15 16:56:23 -070040 void handle(const std::shared_ptr<Request>& req,
Ed Tanous4fa45df2023-09-01 14:20:50 -070041 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/)
42 {
Jonathan Doman102a4cd2024-04-15 16:56:23 -070043 EXPECT_EQ(req->method(), boost::beast::http::verb::get);
44 EXPECT_EQ(req->target(), "/");
45 EXPECT_EQ(req->getHeaderValue(boost::beast::http::field::host),
Ed Tanous998e0cb2023-09-06 13:57:30 -070046 "openbmc_project.xyz");
Jonathan Doman102a4cd2024-04-15 16:56:23 -070047 EXPECT_FALSE(req->keepAlive());
48 EXPECT_EQ(req->version(), 11);
49 EXPECT_EQ(req->body(), "");
Ed Tanous998e0cb2023-09-06 13:57:30 -070050
Ed Tanous4fa45df2023-09-01 14:20:50 -070051 called = true;
52 }
53 bool called = false;
54};
55
Ed Tanous998e0cb2023-09-06 13:57:30 -070056struct ClockFake
Ed Tanous4fa45df2023-09-01 14:20:50 -070057{
Ed Tanous998e0cb2023-09-06 13:57:30 -070058 bool wascalled = false;
59 std::string getDateStr()
60 {
61 wascalled = true;
62 return "TestTime";
63 }
64};
Ed Tanous4fa45df2023-09-01 14:20:50 -070065
66TEST(http_connection, RequestPropogates)
67{
68 boost::asio::io_context io;
Ed Tanous998e0cb2023-09-06 13:57:30 -070069 ClockFake clock;
Ed Tanous796ba932020-08-02 04:29:21 +000070 TestStream stream(io);
71 TestStream out(io);
Ed Tanous4fa45df2023-09-01 14:20:50 -070072 stream.connect(out);
73
74 out.write_some(boost::asio::buffer(
75 "GET / HTTP/1.1\r\nHost: openbmc_project.xyz\r\nConnection: close\r\n\r\n"));
76 FakeHandler handler;
77 boost::asio::steady_timer timer(io);
Ed Tanous998e0cb2023-09-06 13:57:30 -070078 std::function<std::string()> date(
79 std::bind_front(&ClockFake::getDateStr, &clock));
Ed Tanous796ba932020-08-02 04:29:21 +000080
81 boost::asio::ssl::context context{boost::asio::ssl::context::tls};
82 std::shared_ptr<crow::Connection<TestStream, FakeHandler>> conn =
83 std::make_shared<crow::Connection<TestStream, FakeHandler>>(
84 &handler, HttpType::HTTP, std::move(timer), date,
85 boost::asio::ssl::stream<TestStream>(std::move(stream), context));
86 conn->disableAuth();
Ed Tanous4fa45df2023-09-01 14:20:50 -070087 conn->start();
88 io.run_for(std::chrono::seconds(1000));
89 EXPECT_TRUE(handler.called);
90 std::string outStr = out.str();
91
92 std::string expected =
93 "HTTP/1.1 200 OK\r\n"
94 "Connection: close\r\n"
95 "Strict-Transport-Security: max-age=31536000; includeSubdomains\r\n"
Ed Tanous4fa45df2023-09-01 14:20:50 -070096 "Pragma: no-cache\r\n"
97 "Cache-Control: no-store, max-age=0\r\n"
98 "X-Content-Type-Options: nosniff\r\n"
Ed Tanous998e0cb2023-09-06 13:57:30 -070099 "Date: TestTime\r\n"
100 "Content-Length: 0\r\n\r\n";
Ed Tanous4fa45df2023-09-01 14:20:50 -0700101 EXPECT_EQ(outStr, expected);
Ed Tanous998e0cb2023-09-06 13:57:30 -0700102 EXPECT_TRUE(clock.wascalled);
Ed Tanous4fa45df2023-09-01 14:20:50 -0700103}
104
105} // namespace crow