Repository navigation
Expand file tree
/
Copy pathtest_domain.py
More file actions
169 lines (153 loc) · 5.98 KB
/
Copy pathtest_domain.py
File metadata and controls
169 lines (153 loc) · 5.98 KB
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
"""Test Domain."""
# external
import pytest
# local
from validators import ValidationError, domain
@pytest.mark.parametrize(
("value", "rfc_1034", "rfc_2782"),
[
("example.com", False, False),
("exa_mple.com", False, True),
("xn----gtbspbbmkef.xn--p1ai", False, False),
("underscore_subdomain.example.com", False, True),
("something.versicherung", False, False),
("someThing.versicherung.", True, False),
("11.com", False, False),
("3.cn.", True, False),
("_example.com", False, True),
("example_.com", False, True),
("_exa_mple_.com", False, True),
("a.cn", False, False),
("sub1.sub2.sample.co.uk", False, False),
("somerandomexample.xn--fiqs8s", False, False),
("kräuter.com.", True, False),
("über.com", False, False),
],
)
def test_returns_true_on_valid_domain(value: str, rfc_1034: bool, rfc_2782: bool):
"""Test returns true on valid domain."""
assert domain(value, rfc_1034=rfc_1034, rfc_2782=rfc_2782)
@pytest.mark.parametrize(
("value", "consider_tld", "rfc_1034", "rfc_2782"),
[
("example.com", True, False, False),
("exa_mple.com", True, False, True),
("xn----gtbspbbmkef.xn--p1ai", True, False, False),
("underscore_subdomain.example.com", True, False, True),
("someThing.versicherung.", True, True, False),
("11.com", True, False, False),
("3.cn.", True, True, False),
("_example.com", True, False, True),
("example_.com", True, False, True),
("somerandomexample.xn--fiqs8s", True, False, False),
("somerandomexample.onion", True, False, False),
],
)
def test_returns_true_on_valid_top_level_domain(
value: str, consider_tld: bool, rfc_1034: bool, rfc_2782: bool
):
"""Test returns true on valid top level domain."""
assert domain(value, consider_tld=consider_tld, rfc_1034=rfc_1034, rfc_2782=rfc_2782)
@pytest.mark.parametrize(
("value", "rfc_1034", "rfc_2782"),
[
("example.com/.", True, False),
("example.com:4444", False, False),
("example.-com", False, False),
("example.", False, False),
("-example.com", False, False),
("example-.com.", True, False),
("_example.com", False, False),
("_example._com", False, False),
("example_.com", False, False),
("example", False, False),
("example.com!", True, False),
("example?.com", True, False),
("__exa__mple__.com", False, True),
("a......b.com", False, False),
("a.123", False, False),
("foo.ba_r", False, False),
("foo.ba_r", False, True),
("example.co_uk", False, False),
("123.123", False, False),
("123.123.123.", True, False),
("123.123.123.123", False, False),
],
)
def test_returns_failed_validation_on_invalid_domain(value: str, rfc_1034: bool, rfc_2782: bool):
"""Test returns failed validation on invalid domain."""
assert isinstance(domain(value, rfc_1034=rfc_1034, rfc_2782=rfc_2782), ValidationError)
@pytest.mark.parametrize(
("value", "consider_tld", "rfc_1034", "rfc_2782"),
[
("example.266", True, False, False),
("exa_mple.org_", True, False, True),
("xn----gtbspbbmkef.xn-p1ai", True, False, False),
("underscore_subdomain.example.flat", True, False, True),
("someThing.versicherung.reddit.", True, True, False),
("11.twitter", True, False, False),
("3.cnx.", True, True, False),
("_example.#13", True, False, True),
("example_.fo-ul", True, False, True),
("somerandomexample.xn-n-fiqs8s", True, False, False),
],
)
def test_returns_failed_validation_invalid_top_level_domain(
value: str, consider_tld: bool, rfc_1034: bool, rfc_2782: bool
):
"""Test returns failed validation invalid top level domain."""
assert isinstance(
domain(value, consider_tld=consider_tld, rfc_1034=rfc_1034, rfc_2782=rfc_2782),
ValidationError,
)
def _name_of_length(length: int):
"""Build a dotted name of exactly `length` characters, ending with `.com`."""
labels: list[str] = []
remaining = length - len(".com")
while remaining > 0:
size = min(61, remaining)
if remaining - size == 1: # avoid leaving a lone "." behind
size -= 1
labels.append("a" * size)
remaining -= size + 1
name = ".".join(labels) + ".com"
assert len(name) == length
return name
@pytest.mark.parametrize(
"value",
[
# total length: at most 253 characters (RFC 1035)
_name_of_length(253),
# label length: at most 63 characters (RFC 1035)
"a" * 63 + ".com",
# a label may start with a digit (RFC 1123)
"1host.example.com",
# the limit is in octets of the encoded name, not characters: 189 characters
# here, but 225 octets once IDNA-encoded
".".join(["ü" * 30] * 6) + ".com",
],
)
def test_returns_true_on_domain_within_rfc_length_limits(value: str):
"""Test returns true on domain within RFC length limits."""
assert domain(value)
@pytest.mark.parametrize(
"value",
[
_name_of_length(254),
_name_of_length(255),
# many short, dot-separated labels that exceed 253 characters; see #413
".".join(["kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXc"] * 4)
+ ".kld8MXQh6YalMqKRbfs895gMjW5T4p2EwToPoCSThPHHbXgmXcab",
"a" * 64 + ".com",
# fewer than 253 characters (220), but 262 octets once IDNA-encoded
".".join(["ü" * 30] * 7) + ".com",
# the example from #407 (261 characters)
"www." + ".".join(["abcdefghijklmnop"] * 15) + ".fr",
# a label must not start or end with a hyphen
"-host.example.com",
"host-.example.com",
],
)
def test_returns_failed_validation_on_domain_beyond_rfc_limits(value: str):
"""Test returns failed validation on domain beyond RFC limits."""
assert isinstance(domain(value), ValidationError)