From f5f56e1a1c4d9e9496fcb9d81131066a964ccd23 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 14:15:43 +0200 Subject: Adding upstream version 2.4.1. Signed-off-by: Daniel Baumann --- src/lib/http/tests/post_request_unittests.cc | 83 ++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/lib/http/tests/post_request_unittests.cc (limited to 'src/lib/http/tests/post_request_unittests.cc') diff --git a/src/lib/http/tests/post_request_unittests.cc b/src/lib/http/tests/post_request_unittests.cc new file mode 100644 index 0000000..18f2fda --- /dev/null +++ b/src/lib/http/tests/post_request_unittests.cc @@ -0,0 +1,83 @@ +// Copyright (C) 2016-2019 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 +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include + +#include +#include +#include + +using namespace isc::http; +using namespace isc::http::test; + +namespace { + +/// @brief Test fixture class for @ref PostHttpRequest. +class PostHttpRequestTest : public HttpRequestTestBase { +public: + + /// @brief Constructor. + PostHttpRequestTest() + : HttpRequestTestBase(), + json_body_("{ \"service\": \"dhcp4\", \"param1\": \"foo\" }") { + } + + /// @brief Default value of the JSON body. + std::string json_body_; +}; + +// This test verifies that PostHttpRequest class only accepts POST +// messages. +TEST_F(PostHttpRequestTest, requirePost) { + // Use a GET method that is not supported. + setContextBasics("GET", "/isc/org", HttpVersion(1, 0)); + addHeaderToContext("Content-Length", json_body_.length()); + addHeaderToContext("Content-Type", "application/json"); + + ASSERT_THROW(request_->create(), HttpRequestError); + + // Now use POST. It should be accepted. + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); + addHeaderToContext("Content-Length", json_body_.length()); + addHeaderToContext("Content-Type", "application/json"); + + EXPECT_NO_THROW(request_->create()); +} + +// This test verifies that PostHttpRequest requires "Content-Length" +// header. +TEST_F(PostHttpRequestTest, requireContentType) { + // No "Content-Type". It should fail. + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); + addHeaderToContext("Content-Length", json_body_.length()); + + ASSERT_THROW(request_->create(), HttpRequestError); + + // There is "Content-Type". It should pass. + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); + addHeaderToContext("Content-Length", json_body_.length()); + addHeaderToContext("Content-Type", "text/html"); + + EXPECT_NO_THROW(request_->create()); + +} + +// This test verifies that PostHttpRequest requires "Content-Type" +// header. +TEST_F(PostHttpRequestTest, requireContentLength) { + // No "Content-Length". It should fail. + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); + addHeaderToContext("Content-Type", "text/html"); + + ASSERT_THROW(request_->create(), HttpRequestError); + + // There is "Content-Length". It should pass. + setContextBasics("POST", "/isc/org", HttpVersion(1, 0)); + addHeaderToContext("Content-Length", json_body_.length()); + addHeaderToContext("Content-Type", "application/json"); +} + +} -- cgit v1.2.3