1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
|
"""Tests for the command line interface."""
import subprocess
import sys
from unittest.mock import patch
import pytest
from url_normalize import __version__
from url_normalize.cli import main
def run_cli(*args: str) -> subprocess.CompletedProcess:
"""Run the CLI command with given arguments.
Params:
*args: Command line arguments to pass to the CLI.
Returns:
A completed process with stdout, stderr, and return code.
"""
command = [sys.executable, "-m", "url_normalize.cli", *list(args)]
return subprocess.run( # noqa: S603
command, capture_output=True, text=True, check=False
)
def test_cli_error_handling(capsys, monkeypatch):
"""Test CLI error handling when URL normalization fails."""
with patch("url_normalize.cli.url_normalize") as mock_normalize:
mock_normalize.side_effect = Exception("Simulated error")
monkeypatch.setattr("sys.argv", ["url-normalize", "http://example.com"])
with pytest.raises(SystemExit) as excinfo:
main()
assert excinfo.value.code == 1
captured = capsys.readouterr()
assert "Error normalizing URL: Simulated error" in captured.err
assert not captured.out
def test_cli_basic_normalization() -> None:
"""Test basic URL normalization via CLI."""
url = "http://EXAMPLE.com/./path/../other/"
expected = "http://example.com/other/"
result = run_cli(url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_basic_normalization_short_args() -> None:
"""Test basic URL normalization via CLI using short arguments."""
url = "http://EXAMPLE.com/./path/../other/"
expected = "http://example.com/other/"
# Using short args where applicable (none for the URL itself)
result = run_cli(url) # No short args needed for basic case
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_default_scheme() -> None:
"""Test default scheme addition via CLI."""
url = "//example.com"
expected = "https://example.com/"
result = run_cli(url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_default_scheme_short_arg() -> None:
"""Test default scheme addition via CLI using short argument."""
url = "//example.com"
expected = "https://example.com/"
result = run_cli(url) # Default scheme is implicit, no arg needed
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_custom_default_scheme() -> None:
"""Test custom default scheme via CLI."""
url = "//example.com"
expected = "ftp://example.com/"
result = run_cli("--default-scheme", "ftp", url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_custom_default_scheme_short_arg() -> None:
"""Test custom default scheme via CLI using short argument."""
url = "//example.com"
expected = "ftp://example.com/"
result = run_cli("-s", "ftp", url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_filter_params() -> None:
"""Test parameter filtering via CLI."""
url = "http://google.com?utm_source=test&q=1"
expected = "http://google.com/?q=1"
result = run_cli("--filter-params", url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_filter_params_short_arg() -> None:
"""Test parameter filtering via CLI using short argument."""
url = "http://google.com?utm_source=test&q=1"
expected = "http://google.com/?q=1"
result = run_cli("-f", url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_param_allowlist() -> None:
"""Test parameter allowlist via CLI."""
url = "http://example.com?remove=me&keep=this&remove_too=true"
expected = "http://example.com/?keep=this"
# Use filter_params to enable filtering, then allowlist to keep specific ones
result = run_cli("-f", "-p", "keep", url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_param_allowlist_multiple() -> None:
"""Test parameter allowlist with multiple params via CLI."""
url = "http://example.com?remove=me&keep=this&keep_too=yes&remove_too=true"
expected = "http://example.com/?keep=this&keep_too=yes"
result = run_cli("-f", "-p", "keep,keep_too", url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_param_allowlist_without_filtering() -> None:
"""Test allowlist has no effect if filtering is not enabled."""
url = "http://example.com?remove=me&keep=this&remove_too=true"
expected = "http://example.com/?remove=me&keep=this&remove_too=true"
# Not using -f, so allowlist should be ignored
result = run_cli("-p", "keep", url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_no_url() -> None:
"""Test CLI error when no URL is provided."""
result = run_cli()
assert result.returncode != 0
assert "the following arguments are required: url" in result.stderr
def test_cli_version_long() -> None:
"""Test version output with --version flag."""
result = run_cli("--version")
assert result.returncode == 0
assert __version__ in result.stdout
assert not result.stderr
def test_cli_version_short() -> None:
"""Test version output with -v flag."""
result = run_cli("-v")
assert result.returncode == 0
assert __version__ in result.stdout
assert not result.stderr
@pytest.mark.skipif(
sys.platform == "win32", reason="Charset handling differs on Windows CLI"
)
def test_cli_charset() -> None:
"""Test charset handling via CLI (might be platform-dependent)."""
# Example using Cyrillic characters which need correct encoding
url = "http://пример.рф/path"
expected_idn = "http://xn--e1afmkfd.xn--p1ai/path"
# Test with default UTF-8
result_utf8 = run_cli(url)
assert result_utf8.returncode == 0
assert result_utf8.stdout.strip() == expected_idn
assert not result_utf8.stderr
# Test specifying UTF-8 explicitly
result_charset = run_cli("--charset", "utf-8", url)
assert result_charset.returncode == 0
assert result_charset.stdout.strip() == expected_idn
assert not result_charset.stderr
# Test specifying UTF-8 explicitly using short arg
result_charset_short = run_cli("-c", "utf-8", url)
assert result_charset_short.returncode == 0
assert result_charset_short.stdout.strip() == expected_idn
assert not result_charset_short.stderr
def test_cli_default_domain() -> None:
"""Test adding default domain to absolute path via CLI."""
url = "/path/to/image.png"
expected = "https://example.com/path/to/image.png"
result = run_cli("--default-domain", "example.com", url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_default_domain_short_arg() -> None:
"""Test adding default domain using short argument."""
url = "/path/to/image.png"
expected = "https://example.com/path/to/image.png"
result = run_cli("-d", "example.com", url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_default_domain_with_scheme() -> None:
"""Test adding default domain with custom scheme."""
url = "/path/to/image.png"
expected = "http://example.com/path/to/image.png"
result = run_cli("-d", "example.com", "-s", "http", url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_default_domain_no_effect_on_absolute_urls() -> None:
"""Test default domain has no effect on absolute URLs."""
url = "http://original-domain.com/path"
expected = "http://original-domain.com/path"
result = run_cli("-d", "example.com", url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
def test_cli_default_domain_no_effect_on_relative_paths() -> None:
"""Test default domain has no effect on relative paths."""
url = "path/to/file.html"
# This becomes a regular URL with the default scheme
expected = "https://path/to/file.html"
result = run_cli("-d", "example.com", url)
assert result.returncode == 0
assert result.stdout.strip() == expected
assert not result.stderr
|