Repository navigation
Expand file tree
/
Copy pathtest_uri.py
More file actions
75 lines (65 loc) · 2.03 KB
/
Copy pathtest_uri.py
File metadata and controls
75 lines (65 loc) · 2.03 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
"""Test URI."""
# external
import pytest
# local
from validators.uri import uri
@pytest.mark.parametrize(
"value",
[
"telnet://example.com",
"telnet://example.com/",
"telnet://example.com:23",
"telnet://example.com:23/",
"telnet://192.168.1.1",
"telnet://192.168.1.1:23",
"telnet://user:password@example.com",
"telnet://user:password@example.com:23/",
],
)
def test_valid_telnet_uri(value: str):
"""Test valid telnet URI."""
assert uri(value)
@pytest.mark.parametrize(
"value",
[
"telnet://",
"telnet://example.com/path",
"telnet://example.com?query=1",
"telnet://example.com#frag",
# urlsplit would silently strip these control characters
"telnet://example.com\n",
"telnet://exa\nmple.com",
"telnet://example.com\t",
],
)
def test_invalid_telnet_uri(value: str):
"""Test invalid telnet URI."""
assert not uri(value)
@pytest.mark.parametrize(
"value",
[
"file:///tmp/readme.txt",
"ipfs://QmYwAPJzv5CZsnAzt8auVZRnGiUncKEy6PBDoDeZ7hA9Rx/readme.txt",
"magnet:?xt=urn:btih:0123456789012345678901234567890123456789",
"tel:+1234567890",
"data:text/plain;base64,SGVsbG8=",
"urn:isbn:9780140328721",
"urc:example",
],
)
@pytest.mark.parametrize("invalid_char", ["\x00", "\t", "\n", "\r", " ", "\x7f"])
def test_rejects_raw_ascii_controls_and_spaces_in_uri(value: str, invalid_char: str):
"""Test rejects raw ASCII controls and spaces in URI."""
assert not uri(value + invalid_char)
@pytest.mark.parametrize(
"value",
[
"file:///tmp/readme%20name.txt",
"ipfs://QmYwAPJzv5CZsnAzt8auVZRnGiUncKEy6PBDoDeZ7hA9Rx/readme%0A.txt",
"magnet:?xt=urn:btih:0123456789012345678901234567890123456789&dn=readme%20name",
"data:text/plain,hello%20world",
],
)
def test_accepts_percent_encoded_characters_in_uri(value: str):
"""Test accepts percent encoded characters in URI."""
assert uri(value)