From 998e0cbdc5092bdaa783d11c2760bf62084403ce Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Wed, 6 Sep 2023 13:57:30 -0700 Subject: Fix missing date At some point, the date got removed from http1 requests. HTTP2 does not show this issue, but this showed up in unit tests (which is why the prior commit is adding unit tests). The Date Header is useful for synchronizing things like Cache-Control-Policy, with the actual server time, instead of the local system time. Tested: Unit tests pass. Change-Id: I8f105f0cbb6c816c5ec6b14cbeae587d728a20d2 Signed-off-by: Ed Tanous --- test/http/http_connection_test.cpp | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'test') diff --git a/test/http/http_connection_test.cpp b/test/http/http_connection_test.cpp index c4252e1ee7..6647e61d99 100644 --- a/test/http/http_connection_test.cpp +++ b/test/http/http_connection_test.cpp @@ -31,19 +31,31 @@ struct FakeHandler { EXPECT_EQ(req.method(), boost::beast::http::verb::get); EXPECT_EQ(req.target(), "/"); + EXPECT_EQ(req.getHeaderValue(boost::beast::http::field::host), + "openbmc_project.xyz"); + EXPECT_FALSE(req.keepAlive()); + EXPECT_EQ(req.version(), 11); + EXPECT_EQ(req.body(), ""); + called = true; } bool called = false; }; -static std::string getDateStr() +struct ClockFake { - return "TestTime"; -} + bool wascalled = false; + std::string getDateStr() + { + wascalled = true; + return "TestTime"; + } +}; TEST(http_connection, RequestPropogates) { boost::asio::io_context io; + ClockFake clock; boost::beast::test::stream stream(io); boost::beast::test::stream out(io); stream.connect(out); @@ -52,7 +64,8 @@ TEST(http_connection, RequestPropogates) "GET / HTTP/1.1\r\nHost: openbmc_project.xyz\r\nConnection: close\r\n\r\n")); FakeHandler handler; boost::asio::steady_timer timer(io); - std::function date(&getDateStr); + std::function date( + std::bind_front(&ClockFake::getDateStr, &clock)); std::shared_ptr> conn = std::make_shared< crow::Connection>( @@ -77,9 +90,10 @@ TEST(http_connection, RequestPropogates) "Cross-Origin-Opener-Policy: same-origin\r\n" "Cross-Origin-Resource-Policy: same-origin\r\n" "Content-Security-Policy: default-src 'none'; img-src 'self' data:; font-src 'self'; style-src 'self'; script-src 'self'; connect-src 'self' wss:; form-action 'none'; frame-ancestors 'none'; object-src 'none'; base-uri 'none'\r\n" - "Content-Length: 0\r\n" - "\r\n"; + "Date: TestTime\r\n" + "Content-Length: 0\r\n\r\n"; EXPECT_EQ(outStr, expected); + EXPECT_TRUE(clock.wascalled); } } // namespace crow -- cgit v1.2.3