]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#3609] Added HSTS unit test
authorFrancis Dupont <fdupont@isc.org>
Tue, 12 Nov 2024 16:51:50 +0000 (17:51 +0100)
committerFrancis Dupont <fdupont@isc.org>
Fri, 22 Nov 2024 08:55:30 +0000 (09:55 +0100)
src/lib/http/tests/response_unittests.cc

index f54e65d6bbd738f9658ada66b7c6a38e4f532b99..f04a7ff74e03d8a4d14c127f4ebb0fd6d1c8888c 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2016-2018 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2016-2024 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -166,4 +166,36 @@ TEST_F(HttpResponseTest, getDateHeaderValue) {
     EXPECT_LT(parsed_to_current.seconds(), 10);
 }
 
+// Test that an arbitrary header can be added.
+TEST_F(HttpResponseTest, addHeader) {
+    // Create a response.
+    TestHttpResponse response(HttpVersion(1, 0), HttpStatusCode::OK);
+    // Add a HSTS header.
+    response.context()->headers_.push_back(HttpHeaderContext("Strict-Transport-Security",
+                                                             "max-age=31536000"));
+    // From responseOK.
+    // Include HTML body.
+    const std::string sample_body =
+        "<html>"
+        "<head><title>Kea page title</title></head>"
+        "<body><h1>Some header</h1></body>"
+        "</html>";
+        response.context()->headers_.push_back(HttpHeaderContext("Content-Type", "text/html"));
+    response.context()->headers_.push_back(HttpHeaderContext("Host", "kea.example.org"));
+    response.context()->body_ = sample_body;
+    ASSERT_NO_THROW(response.finalize());
+
+    // Expected value.
+    std::ostringstream response_string;
+    response_string <<
+        "HTTP/1.0 200 OK\r\n"
+        "Content-Length: " << sample_body.length() << "\r\n"
+        "Content-Type: text/html\r\n"
+        "Date: " << response.getDateHeaderValue() << "\r\n"
+        "Host: kea.example.org\r\n"
+        "Strict-Transport-Security: max-age=31536000\r\n\r\n" << sample_body;
+
+    EXPECT_EQ(response_string.str(), response.toString());
+}
+
 }