]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5205] Unit tests for the HTTP message formatting.
authorMarcin Siodelski <marcin@isc.org>
Tue, 29 May 2018 19:51:01 +0000 (21:51 +0200)
committerMarcin Siodelski <marcin@isc.org>
Tue, 29 May 2018 19:51:01 +0000 (21:51 +0200)
src/lib/http/http_message_parser_base.cc
src/lib/http/tests/request_parser_unittests.cc
src/lib/http/tests/response_parser_unittests.cc

index 715a435269434fa5c8854564911744b0545e41b5..4b346d76a221b8c71511540f77e512cee61fe603 100644 (file)
@@ -79,11 +79,9 @@ HttpMessageParserBase::getBufferAsString(const size_t limit) const {
 std::string
 HttpMessageParserBase::logFormatHttpMessage(const std::string& message,
                                             const size_t limit) {
-    if (!message.empty()) {
+    if ((limit > 0) && !message.empty()) {
         if (limit < message.size()) {
             std::ostringstream s;
-            s << ".........\n(truncating HTTP message larger than "
-              << limit << " characters)\n";
             s << message.substr(0, limit)
               << ".........\n(truncating HTTP message larger than "
               << limit << " characters)\n";
index 049a1bd676eeb6083297bd72f2be9b55650a0a3a..57e183ab20670f4c1c34fd5aa40b6bc1d7ef42c7 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2016-2018 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
@@ -341,4 +341,31 @@ TEST_F(HttpRequestParserTest, noColonInHttpHeader) {
     testInvalidHttpRequest(http_req);
 }
 
+// This test verifies that the input buffer of the HTTP request can be
+// retrieved as text formatted for logging.
+TEST_F(HttpRequestParserTest, getBufferAsString) {
+    std::string http_req = "POST /foo/bar HTTP/1.0\r\n"
+        "Content-Type: application/json\r\n";
+
+    // Create HTTP request.
+    PostHttpRequestJson request;
+
+    // Create a parser and make it use the request we created.
+    HttpRequestParser parser(request);
+    ASSERT_NO_THROW(parser.initModel());
+
+    // Insert data into the request.
+    ASSERT_NO_THROW(parser.postBuffer(&http_req[0], http_req.size()));
+
+    // limit = 0 means no limit
+    EXPECT_EQ(http_req, parser.getBufferAsString(0));
+
+    // large enough limit should not cause the truncation.
+    EXPECT_EQ(http_req, parser.getBufferAsString(1024));
+
+    // Only 3 characters requested. The request should be truncated.
+    EXPECT_EQ("POS.........\n(truncating HTTP message larger than 3 characters)\n",
+              parser.getBufferAsString(3));
+}
+
 }
index 10ec1c0e4f36b5a143f23aa38ff216dafbb14dae..10e23b46e5264f43d14fea4ae4fdc608834b26e1 100644 (file)
@@ -310,5 +310,25 @@ TEST_F(HttpResponseParserTest, noColonInHttpHeader) {
     testInvalidHttpResponse(http_resp);
 }
 
+// This test verifies that the HTTP response is formatted for logging.
+TEST_F(HttpResponseParserTest, logFormatHttpMessage) {
+    std::string message = "POST / HTTP/1.1\r\n"
+        "Host: 127.0.0.1:8080\r\n"
+        "User-Agent: curl/7.59.0\r\n"
+        "Content-Type: application/json\r\n"
+        "Content-Length: 51\r\n\r\n"
+        "{ \"command\": \"config-get\", \"service\": [ \"dhcp4\" ] }";
+
+    // limit = 0 means no limit
+    EXPECT_EQ(message, HttpResponseParser::logFormatHttpMessage(message, 0));
+
+    // large enough limit should not cause the truncation.
+    EXPECT_EQ(message, HttpResponseParser::logFormatHttpMessage(message, 1024));
+
+    // Only 3 characters requested. The request should be truncated.
+    EXPECT_EQ("POS.........\n(truncating HTTP message larger than 3 characters)\n",
+              HttpResponseParser::logFormatHttpMessage(message, 3));
+}
+
 
 }