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