Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 3 | #include "async_resp.hpp" |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 4 | #include "http/http_connection.hpp" |
| 5 | #include "http/http_request.hpp" |
| 6 | #include "http/http_response.hpp" |
| 7 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 8 | #include <boost/asio/buffer.hpp> |
| 9 | #include <boost/asio/io_context.hpp> |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 10 | #include <boost/asio/steady_timer.hpp> |
| 11 | #include <boost/beast/_experimental/test/stream.hpp> |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 12 | #include <boost/beast/http/field.hpp> |
| 13 | #include <boost/beast/http/verb.hpp> |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 14 | |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 15 | #include <chrono> |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 16 | #include <functional> |
| 17 | #include <memory> |
| 18 | #include <string> |
Ed Tanous | f0b59af | 2024-03-20 13:38:04 -0700 | [diff] [blame] | 19 | #include <utility> |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 20 | |
| 21 | #include "gtest/gtest.h" |
| 22 | namespace crow |
| 23 | { |
| 24 | |
| 25 | struct FakeHandler |
| 26 | { |
| 27 | static void |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 28 | handleUpgrade(const std::shared_ptr<Request>& /*req*/, |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 29 | const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, |
| 30 | boost::beast::test::stream&& /*adaptor*/) |
| 31 | { |
| 32 | // Handle Upgrade should never be called |
| 33 | EXPECT_FALSE(true); |
| 34 | } |
| 35 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 36 | void handle(const std::shared_ptr<Request>& req, |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 37 | const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/) |
| 38 | { |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 39 | 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 Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 42 | "openbmc_project.xyz"); |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 43 | EXPECT_FALSE(req->keepAlive()); |
| 44 | EXPECT_EQ(req->version(), 11); |
| 45 | EXPECT_EQ(req->body(), ""); |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 46 | |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 47 | called = true; |
| 48 | } |
| 49 | bool called = false; |
| 50 | }; |
| 51 | |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 52 | struct ClockFake |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 53 | { |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 54 | bool wascalled = false; |
| 55 | std::string getDateStr() |
| 56 | { |
| 57 | wascalled = true; |
| 58 | return "TestTime"; |
| 59 | } |
| 60 | }; |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 61 | |
| 62 | TEST(http_connection, RequestPropogates) |
| 63 | { |
| 64 | boost::asio::io_context io; |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 65 | ClockFake clock; |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 66 | 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 Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 74 | std::function<std::string()> date( |
| 75 | std::bind_front(&ClockFake::getDateStr, &clock)); |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 76 | 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 Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 89 | "Pragma: no-cache\r\n" |
| 90 | "Cache-Control: no-store, max-age=0\r\n" |
| 91 | "X-Content-Type-Options: nosniff\r\n" |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 92 | "Date: TestTime\r\n" |
| 93 | "Content-Length: 0\r\n\r\n"; |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 94 | EXPECT_EQ(outStr, expected); |
Ed Tanous | 998e0cb | 2023-09-06 13:57:30 -0700 | [diff] [blame] | 95 | EXPECT_TRUE(clock.wascalled); |
Ed Tanous | 4fa45df | 2023-09-01 14:20:50 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | } // namespace crow |