1 #include "http/http_connection.hpp" 2 #include "http/http_request.hpp" 3 #include "http/http_response.hpp" 4 5 #include <boost/asio/steady_timer.hpp> 6 #include <boost/beast/_experimental/test/stream.hpp> 7 8 #include <filesystem> 9 #include <fstream> 10 #include <functional> 11 #include <memory> 12 #include <string> 13 14 #include "gtest/gtest.h" 15 namespace crow 16 { 17 18 struct FakeHandler 19 { 20 static void 21 handleUpgrade(Request& /*req*/, 22 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, 23 boost::beast::test::stream&& /*adaptor*/) 24 { 25 // Handle Upgrade should never be called 26 EXPECT_FALSE(true); 27 } 28 29 void handle(Request& req, 30 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/) 31 { 32 EXPECT_EQ(req.method(), boost::beast::http::verb::get); 33 EXPECT_EQ(req.target(), "/"); 34 EXPECT_EQ(req.getHeaderValue(boost::beast::http::field::host), 35 "openbmc_project.xyz"); 36 EXPECT_FALSE(req.keepAlive()); 37 EXPECT_EQ(req.version(), 11); 38 EXPECT_EQ(req.body(), ""); 39 40 called = true; 41 } 42 bool called = false; 43 }; 44 45 struct ClockFake 46 { 47 bool wascalled = false; 48 std::string getDateStr() 49 { 50 wascalled = true; 51 return "TestTime"; 52 } 53 }; 54 55 TEST(http_connection, RequestPropogates) 56 { 57 boost::asio::io_context io; 58 ClockFake clock; 59 boost::beast::test::stream stream(io); 60 boost::beast::test::stream out(io); 61 stream.connect(out); 62 63 out.write_some(boost::asio::buffer( 64 "GET / HTTP/1.1\r\nHost: openbmc_project.xyz\r\nConnection: close\r\n\r\n")); 65 FakeHandler handler; 66 boost::asio::steady_timer timer(io); 67 std::function<std::string()> date( 68 std::bind_front(&ClockFake::getDateStr, &clock)); 69 std::shared_ptr<crow::Connection<boost::beast::test::stream, FakeHandler>> 70 conn = std::make_shared< 71 crow::Connection<boost::beast::test::stream, FakeHandler>>( 72 &handler, std::move(timer), date, std::move(stream)); 73 conn->start(); 74 io.run_for(std::chrono::seconds(1000)); 75 EXPECT_TRUE(handler.called); 76 std::string outStr = out.str(); 77 78 std::string expected = 79 "HTTP/1.1 200 OK\r\n" 80 "Connection: close\r\n" 81 "Strict-Transport-Security: max-age=31536000; includeSubdomains\r\n" 82 "X-Frame-Options: DENY\r\n" 83 "Pragma: no-cache\r\n" 84 "Cache-Control: no-store, max-age=0\r\n" 85 "X-Content-Type-Options: nosniff\r\n" 86 "Referrer-Policy: no-referrer\r\n" 87 "Permissions-Policy: accelerometer=(),ambient-light-sensor=(),autoplay=(),battery=(),camera=(),display-capture=(),document-domain=(),encrypted-media=(),fullscreen=(),gamepad=(),geolocation=(),gyroscope=(),layout-animations=(self),legacy-image-formats=(self),magnetometer=(),microphone=(),midi=(),oversized-images=(self),payment=(),picture-in-picture=(),publickey-credentials-get=(),speaker-selection=(),sync-xhr=(self),unoptimized-images=(self),unsized-media=(self),usb=(),screen-wak-lock=(),web-share=(),xr-spatial-tracking=()\r\n" 88 "X-Permitted-Cross-Domain-Policies: none\r\n" 89 "Cross-Origin-Embedder-Policy: require-corp\r\n" 90 "Cross-Origin-Opener-Policy: same-origin\r\n" 91 "Cross-Origin-Resource-Policy: same-origin\r\n" 92 "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" 93 "Date: TestTime\r\n" 94 "Content-Length: 0\r\n\r\n"; 95 EXPECT_EQ(outStr, expected); 96 EXPECT_TRUE(clock.wascalled); 97 } 98 99 } // namespace crow 100