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 <edtanous@google.com>
diff --git a/http/http_connection.hpp b/http/http_connection.hpp
index c64d511..175912d 100644
--- a/http/http_connection.hpp
+++ b/http/http_connection.hpp
@@ -364,7 +364,7 @@
         res.keepAlive(keepAlive);
 
         completeResponseFields(*req, res);
-
+        res.addHeader(boost::beast::http::field::date, getCachedDateStr());
         if (!isAlive())
         {
             res.setCompleteRequestHandler(nullptr);
diff --git a/test/http/http_connection_test.cpp b/test/http/http_connection_test.cpp
index c4252e1..6647e61 100644
--- a/test/http/http_connection_test.cpp
+++ b/test/http/http_connection_test.cpp
@@ -31,19 +31,31 @@
     {
         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 @@
         "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<std::string()> date(&getDateStr);
+    std::function<std::string()> date(
+        std::bind_front(&ClockFake::getDateStr, &clock));
     std::shared_ptr<crow::Connection<boost::beast::test::stream, FakeHandler>>
         conn = std::make_shared<
             crow::Connection<boost::beast::test::stream, FakeHandler>>(
@@ -77,9 +90,10 @@
         "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