# SPDX-FileCopyrightText: 2026 Dennis Fink # # 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")