|
| 1 | +// Copyright 2025 Glyn Matthews. |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// (See accompanying file LICENSE_1_0.txt of copy at |
| 4 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | +#include <exception> |
| 7 | + |
| 8 | +#include <catch2/catch_all.hpp> |
| 9 | + |
| 10 | +#include <skyr/url.hpp> |
| 11 | + |
| 12 | +TEST_CASE("url_sanitize_tests", "[url]") { |
| 13 | + using namespace std::string_literals; |
| 14 | + |
| 15 | + SECTION("sanitize_removes_credentials") { |
| 16 | + auto url = skyr::url("https://user:pass@example.com/path?query=1#fragment"); |
| 17 | + auto sanitized = url.sanitize(); |
| 18 | + |
| 19 | + CHECK(sanitized.username().empty()); |
| 20 | + CHECK(sanitized.password().empty()); |
| 21 | + CHECK(sanitized.hash().empty()); |
| 22 | + CHECK(sanitized.search() == "?query=1"); |
| 23 | + CHECK(sanitized.pathname() == "/path"); |
| 24 | + CHECK(sanitized.hostname() == "example.com"); |
| 25 | + CHECK(sanitized.href() == "https://example.com/path?query=1"); |
| 26 | + } |
| 27 | + |
| 28 | + SECTION("sanitize_removes_fragment") { |
| 29 | + auto url = skyr::url("https://example.com/path#section"); |
| 30 | + auto sanitized = url.sanitize(); |
| 31 | + |
| 32 | + CHECK(sanitized.hash().empty()); |
| 33 | + CHECK(sanitized.href() == "https://example.com/path"); |
| 34 | + } |
| 35 | + |
| 36 | + SECTION("without_query") { |
| 37 | + auto url = skyr::url("https://example.com/path?foo=1&bar=2#fragment"); |
| 38 | + auto result = url.without_query(); |
| 39 | + |
| 40 | + CHECK(result.search().empty()); |
| 41 | + CHECK(result.hash() == "#fragment"); |
| 42 | + CHECK(result.pathname() == "/path"); |
| 43 | + CHECK(result.href() == "https://example.com/path#fragment"); |
| 44 | + } |
| 45 | + |
| 46 | + SECTION("without_fragment") { |
| 47 | + auto url = skyr::url("https://example.com/path?query=1#fragment"); |
| 48 | + auto result = url.without_fragment(); |
| 49 | + |
| 50 | + CHECK(result.hash().empty()); |
| 51 | + CHECK(result.search() == "?query=1"); |
| 52 | + CHECK(result.href() == "https://example.com/path?query=1"); |
| 53 | + } |
| 54 | + |
| 55 | + SECTION("sanitize_then_without_query") { |
| 56 | + auto url = skyr::url("https://user:pass@example.com/path?query=1#fragment"); |
| 57 | + auto sanitized = url.sanitize().without_query(); |
| 58 | + |
| 59 | + CHECK(sanitized.username().empty()); |
| 60 | + CHECK(sanitized.password().empty()); |
| 61 | + CHECK(sanitized.hash().empty()); |
| 62 | + CHECK(sanitized.search().empty()); |
| 63 | + CHECK(sanitized.href() == "https://example.com/path"); |
| 64 | + } |
| 65 | + |
| 66 | + SECTION("sanitize_already_clean_url") { |
| 67 | + auto url = skyr::url("https://example.com/path"); |
| 68 | + auto sanitized = url.sanitize(); |
| 69 | + |
| 70 | + CHECK(sanitized.href() == url.href()); |
| 71 | + } |
| 72 | + |
| 73 | + SECTION("sanitize_preserves_port") { |
| 74 | + auto url = skyr::url("https://user:pass@example.com:8080/path#fragment"); |
| 75 | + auto sanitized = url.sanitize(); |
| 76 | + |
| 77 | + CHECK(sanitized.port() == "8080"); |
| 78 | + CHECK(sanitized.href() == "https://example.com:8080/path"); |
| 79 | + } |
| 80 | + |
| 81 | + SECTION("sanitize_is_immutable") { |
| 82 | + auto url = skyr::url("https://user:pass@example.com/path#fragment"); |
| 83 | + auto sanitized = url.sanitize(); |
| 84 | + |
| 85 | + // Original should be unchanged |
| 86 | + CHECK(url.username() == "user"); |
| 87 | + CHECK(url.password() == "pass"); |
| 88 | + CHECK(url.hash() == "#fragment"); |
| 89 | + |
| 90 | + // Sanitized should be clean |
| 91 | + CHECK(sanitized.username().empty()); |
| 92 | + CHECK(sanitized.password().empty()); |
| 93 | + CHECK(sanitized.hash().empty()); |
| 94 | + } |
| 95 | + |
| 96 | + SECTION("without_params_removes_single_param") { |
| 97 | + auto url = skyr::url("https://example.com/path?foo=1&bar=2&baz=3"); |
| 98 | + auto result = url.without_params({"bar"}); |
| 99 | + |
| 100 | + CHECK(result.search_parameters().contains("foo")); |
| 101 | + CHECK_FALSE(result.search_parameters().contains("bar")); |
| 102 | + CHECK(result.search_parameters().contains("baz")); |
| 103 | + } |
| 104 | + |
| 105 | + SECTION("without_params_removes_multiple_params") { |
| 106 | + auto url = skyr::url("https://example.com/path?foo=1&bar=2&baz=3&qux=4"); |
| 107 | + auto result = url.without_params({"bar", "qux"}); |
| 108 | + |
| 109 | + CHECK(result.search_parameters().contains("foo")); |
| 110 | + CHECK_FALSE(result.search_parameters().contains("bar")); |
| 111 | + CHECK(result.search_parameters().contains("baz")); |
| 112 | + CHECK_FALSE(result.search_parameters().contains("qux")); |
| 113 | + } |
| 114 | + |
| 115 | + SECTION("without_params_nonexistent_param") { |
| 116 | + auto url = skyr::url("https://example.com/path?foo=1"); |
| 117 | + auto result = url.without_params({"bar", "baz"}); |
| 118 | + |
| 119 | + CHECK(result.search_parameters().contains("foo")); |
| 120 | + CHECK(result.href() == url.href()); |
| 121 | + } |
| 122 | + |
| 123 | + SECTION("without_params_empty_list") { |
| 124 | + auto url = skyr::url("https://example.com/path?foo=1&bar=2"); |
| 125 | + auto result = url.without_params({}); |
| 126 | + |
| 127 | + CHECK(result.href() == url.href()); |
| 128 | + } |
| 129 | + |
| 130 | + SECTION("without_params_removes_all_params") { |
| 131 | + auto url = skyr::url("https://example.com/path?foo=1&bar=2"); |
| 132 | + auto result = url.without_params({"foo", "bar"}); |
| 133 | + |
| 134 | + CHECK(result.search().empty()); |
| 135 | + CHECK(result.href() == "https://example.com/path"); |
| 136 | + } |
| 137 | + |
| 138 | + SECTION("without_params_is_immutable") { |
| 139 | + auto url = skyr::url("https://example.com/path?foo=1&bar=2&baz=3"); |
| 140 | + auto result = url.without_params({"bar"}); |
| 141 | + |
| 142 | + // Original should be unchanged |
| 143 | + CHECK(url.search_parameters().contains("foo")); |
| 144 | + CHECK(url.search_parameters().contains("bar")); |
| 145 | + CHECK(url.search_parameters().contains("baz")); |
| 146 | + |
| 147 | + // Result should have bar removed |
| 148 | + CHECK(result.search_parameters().contains("foo")); |
| 149 | + CHECK_FALSE(result.search_parameters().contains("bar")); |
| 150 | + CHECK(result.search_parameters().contains("baz")); |
| 151 | + } |
| 152 | + |
| 153 | + SECTION("without_params_with_duplicate_params") { |
| 154 | + auto url = skyr::url("https://example.com/path?foo=1&foo=2&bar=3"); |
| 155 | + auto result = url.without_params({"foo"}); |
| 156 | + |
| 157 | + CHECK_FALSE(result.search_parameters().contains("foo")); |
| 158 | + CHECK(result.search_parameters().contains("bar")); |
| 159 | + } |
| 160 | + |
| 161 | + SECTION("combined_sanitize_and_without_params") { |
| 162 | + auto url = skyr::url("https://user:pass@example.com/path?foo=1&bar=2&baz=3#fragment"); |
| 163 | + auto result = url.sanitize().without_params({"bar"}); |
| 164 | + |
| 165 | + CHECK(result.username().empty()); |
| 166 | + CHECK(result.password().empty()); |
| 167 | + CHECK(result.hash().empty()); |
| 168 | + CHECK(result.search_parameters().contains("foo")); |
| 169 | + CHECK_FALSE(result.search_parameters().contains("bar")); |
| 170 | + CHECK(result.search_parameters().contains("baz")); |
| 171 | + CHECK(result.href() == "https://example.com/path?foo=1&baz=3"); |
| 172 | + } |
| 173 | + |
| 174 | + SECTION("without_query_is_immutable") { |
| 175 | + auto url = skyr::url("https://example.com/path?foo=1"); |
| 176 | + auto result = url.without_query(); |
| 177 | + |
| 178 | + CHECK(url.search() == "?foo=1"); |
| 179 | + CHECK(result.search().empty()); |
| 180 | + } |
| 181 | + |
| 182 | + SECTION("without_fragment_is_immutable") { |
| 183 | + auto url = skyr::url("https://example.com/path#fragment"); |
| 184 | + auto result = url.without_fragment(); |
| 185 | + |
| 186 | + CHECK(url.hash() == "#fragment"); |
| 187 | + CHECK(result.hash().empty()); |
| 188 | + } |
| 189 | + |
| 190 | + SECTION("chain_multiple_without_operations") { |
| 191 | + auto url = skyr::url("https://user:pass@example.com/path?foo=1&bar=2#fragment"); |
| 192 | + auto result = url.without_query().without_fragment(); |
| 193 | + |
| 194 | + CHECK(result.search().empty()); |
| 195 | + CHECK(result.hash().empty()); |
| 196 | + CHECK(result.username() == "user"); // Credentials preserved |
| 197 | + CHECK(result.href() == "https://user:pass@example.com/path"); |
| 198 | + } |
| 199 | + |
| 200 | + SECTION("fully_clean_url") { |
| 201 | + auto url = skyr::url("https://user:pass@example.com/path?foo=1&bar=2#fragment"); |
| 202 | + auto result = url.sanitize().without_query(); |
| 203 | + |
| 204 | + CHECK(result.username().empty()); |
| 205 | + CHECK(result.password().empty()); |
| 206 | + CHECK(result.search().empty()); |
| 207 | + CHECK(result.hash().empty()); |
| 208 | + CHECK(result.href() == "https://example.com/path"); |
| 209 | + } |
| 210 | +} |
0 commit comments