diff options
Diffstat (limited to '')
| -rw-r--r-- | tests/test_hostname.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/tests/test_hostname.py b/tests/test_hostname.py new file mode 100644 index 0000000..0faaacd --- /dev/null +++ b/tests/test_hostname.py @@ -0,0 +1,72 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink <me+coding@dennisfink.me> +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Regression tests for the one shared hostname normalizer.""" + +import pytest + +from webmentions_ssg.hostname import normalize_hostname + + +@pytest.mark.parametrize( + ("value", "expected"), + [ + ("Example.COM.", "example.com"), + ("blog.Example.COM", "blog.example.com"), + ("bücher.example", "xn--bcher-kva.example"), + ("198.51.100.10", "198.51.100.10"), + ("2001:db8::1", "2001:db8::1"), + ], +) +def test_normalize_bare_hostname(value: str, expected: str) -> None: + assert normalize_hostname(value) == expected + + +@pytest.mark.parametrize( + "value", + [ + "", + " example.com", + "https://example.com", + "example.com/path", + "*.example.com", + "-bad.example", + "bad-.example", + "example..com", + "example.com:8080", + "hello_foo.example", + "a" * 64 + ".example", + ], +) +def test_reject_invalid_bare_hostname(value: str) -> None: + with pytest.raises(ValueError): + normalize_hostname(value) + + +@pytest.mark.parametrize( + ("url", "expected"), + [ + ("https://EXAMPLE.com./path", "example.com"), + ("http://[2001:db8::1]/", "2001:db8::1"), + ("https://BÜCHER.example.:8443/post", "xn--bcher-kva.example"), + ], +) +def test_normalize_url_hostname(url: str, expected: str) -> None: + assert normalize_hostname(url, from_url=True) == expected + + +@pytest.mark.parametrize("url", ["https:///path", "http://[::1", "invalid"]) +def test_reject_url_without_valid_hostname(url: str) -> None: + with pytest.raises(ValueError): + normalize_hostname(url, from_url=True) + + +def test_wildcard_mode_is_explicit() -> None: + assert normalize_hostname("*.EXAMPLE.com.", allow_patterns=True) == "*.example.com" + assert ( + normalize_hostname("blog?.example.com", allow_patterns=True) + == "blog?.example.com" + ) + with pytest.raises(ValueError): + normalize_hostname("*.example.com") |
