blob: 393e209b8a91ffb1be174f65f45230c0351936e1 [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
Jonathan Doman102a4cd2024-04-15 16:56:23 -070026 handleUpgrade(const std::shared_ptr<Request>& /*req*/,
Ed Tanous4fa45df2023-09-01 14:20:50 -070027 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
Jonathan Doman102a4cd2024-04-15 16:56:23 -070034 void handle(const std::shared_ptr<Request>& req,
Ed Tanous4fa45df2023-09-01 14:20:50 -070035 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/)
36 {
Jonathan Doman102a4cd2024-04-15 16:56:23 -070037 EXPECT_EQ(req->method(), boost::beast::http::verb::get);
38 EXPECT_EQ(req->target(), "/");
39 EXPECT_EQ(req->getHeaderValue(boost::beast::http::field::host),
Ed Tanous998e0cb2023-09-06 13:57:30 -070040 "openbmc_project.xyz");
Jonathan Doman102a4cd2024-04-15 16:56:23 -070041 EXPECT_FALSE(req->keepAlive());
42 EXPECT_EQ(req->version(), 11);
43 EXPECT_EQ(req->body(), "");
Ed Tanous998e0cb2023-09-06 13:57:30 -070044
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"
Ed Tanous4fa45df2023-09-01 14:20:50 -070087 "Pragma: no-cache\r\n"
88 "Cache-Control: no-store, max-age=0\r\n"
89 "X-Content-Type-Options: nosniff\r\n"
Ed Tanous998e0cb2023-09-06 13:57:30 -070090 "Date: TestTime\r\n"
91 "Content-Length: 0\r\n\r\n";
Ed Tanous4fa45df2023-09-01 14:20:50 -070092 EXPECT_EQ(outStr, expected);
Ed Tanous998e0cb2023-09-06 13:57:30 -070093 EXPECT_TRUE(clock.wascalled);
Ed Tanous4fa45df2023-09-01 14:20:50 -070094}
95
96} // namespace crow