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/url_unittests.cc | 115 ++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/lib/http/tests/url_unittests.cc (limited to 'src/lib/http/tests/url_unittests.cc') diff --git a/src/lib/http/tests/url_unittests.cc b/src/lib/http/tests/url_unittests.cc new file mode 100644 index 0000000..f024e61 --- /dev/null +++ b/src/lib/http/tests/url_unittests.cc @@ -0,0 +1,115 @@ +// Copyright (C) 2017-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 +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +#include +#include +#include +#include + +using namespace isc::http; + +namespace { + +/// @brief Test fixture class for @c Url class. +class UrlTest : public ::testing::Test { +public: + + /// @brief Test valid URL. + /// + /// @param text_url URL is the text form. + /// @param expected_scheme Expected scheme. + /// @param expected_hostname Expected hostname. + /// @param expected_port Expected port. + /// @param expected_path Expected path. + void testValidUrl(const std::string& text_url, + const Url::Scheme& expected_scheme, + const std::string& expected_hostname, + const unsigned expected_port, + const std::string& expected_path) { + Url url(text_url); + ASSERT_TRUE(url.isValid()) << url.getErrorMessage(); + EXPECT_EQ(expected_scheme, url.getScheme()); + EXPECT_EQ(expected_hostname, url.getStrippedHostname()); + EXPECT_EQ(expected_port, url.getPort()); + EXPECT_EQ(expected_path, url.getPath()); + } + + /// @brief Test invalid URL. + /// + /// @param text_url URL is the text form. + void testInvalidUrl(const std::string& text_url) { + Url url(text_url); + EXPECT_FALSE(url.isValid()); + } +}; + +// URL contains scheme and hostname. +TEST_F(UrlTest, schemeHostname) { + testValidUrl("http://example.org", Url::HTTP, "example.org", 0, ""); +} + +// URL contains scheme, hostname and slash. +TEST_F(UrlTest, schemeHostnameSlash) { + testValidUrl("http://example.org/", Url::HTTP, "example.org", 0, "/"); +} + +// URL contains scheme, IPv6 address and slash. +TEST_F(UrlTest, schemeIPv6AddressSlash) { + testValidUrl("http://[2001:db8:1::100]/", Url::HTTP, "2001:db8:1::100", 0, "/"); +} + +// URL contains scheme, IPv4 address and slash. +TEST_F(UrlTest, schemeIPv4AddressSlash) { + testValidUrl("http://192.0.2.2/", Url::HTTP, "192.0.2.2", 0, "/"); +} + +// URL contains scheme, hostname and path. +TEST_F(UrlTest, schemeHostnamePath) { + testValidUrl("http://example.org/some/path", Url::HTTP, "example.org", 0, + "/some/path"); +} + +// URL contains scheme, hostname and port. +TEST_F(UrlTest, schemeHostnamePort) { + testValidUrl("http://example.org:8080/", Url::HTTP, "example.org", 8080, "/"); +} + +// URL contains scheme, hostname, port and slash. +TEST_F(UrlTest, schemeHostnamePortSlash) { + testValidUrl("http://example.org:8080/", Url::HTTP, "example.org", 8080, "/"); +} + +// URL contains scheme, IPv6 address and port. +TEST_F(UrlTest, schemeIPv6AddressPort) { + testValidUrl("http://[2001:db8:1::1]:8080/", Url::HTTP, "2001:db8:1::1", 8080, "/"); +} + +// URL contains scheme, hostname, port and path. +TEST_F(UrlTest, schemeHostnamePortPath) { + testValidUrl("http://example.org:8080/path/", Url::HTTP, "example.org", 8080, + "/path/"); +} + +// URL contains https scheme, hostname, port and path. +TEST_F(UrlTest, secureSchemeHostnamePortPath) { + testValidUrl("https://example.org:8080/path/", Url::HTTPS, "example.org", 8080, + "/path/"); +} + +// Tests various invalid URLS. +TEST_F(UrlTest, invalidUrls) { + testInvalidUrl("example.org"); + testInvalidUrl("file://example.org"); + testInvalidUrl("http//example.org"); + testInvalidUrl("http:/example.org"); + testInvalidUrl("http://"); + testInvalidUrl("http://[]"); + testInvalidUrl("http://[2001:db8:1::1"); + testInvalidUrl("http://example.org:"); + testInvalidUrl("http://example.org:abc"); +} + +} -- cgit v1.2.3