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
|
# 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")
|