Commit 28a2f5dc authored by Marcin Siodelski's avatar Marcin Siodelski

[5205] Unit tests for the HTTP message formatting.

parent 27cf2123
......@@ -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";
......
// 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));
}
}
......@@ -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));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment