Skip to content

Commit 651dbd1

Browse files
committed
Accept case-insensitive URI schemes
1 parent 22786cc commit 651dbd1

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

‎src/validators/uri.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ def uri(value: str, /):
9292
if not value or re.search(r"[\x00-\x20\x7f]", value):
9393
return False
9494

95+
# RFC 3986 section 3.1: schemes are case-insensitive; other components are not.
96+
scheme, separator, rest = value.partition(":")
97+
value = scheme.lower() + separator + rest
98+
9599
# TODO: work on various validations
96100

97101
# url

‎tests/test_uri.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,37 @@
77
from validators.uri import uri
88

99

10+
@pytest.mark.parametrize(
11+
"value",
12+
[
13+
"http://example.com/CaseSensitive?key=Value",
14+
"https://example.com",
15+
"ftp://example.com/file.txt",
16+
"mailto:User@example.com",
17+
"file:///tmp/Readme.txt",
18+
"ipfs://QmYwAPJzv5CZsnAzt8auVZRnGiUncKEy6PBDoDeZ7hA9Rx",
19+
"magnet:?xt=urn:btih:0123456789012345678901234567890123456789",
20+
"tel:+1234567890",
21+
"data:text/plain;base64,SGVsbG8=",
22+
"urn:isbn:9780140328721",
23+
"urc:example",
24+
"telnet://example.com:23/",
25+
],
26+
)
27+
@pytest.mark.parametrize("uppercase", [True, False])
28+
def test_accepts_case_insensitive_schemes(value: str, uppercase: bool):
29+
"""Accept uppercase and mixed-case spellings of supported URI schemes."""
30+
scheme, separator, rest = value.partition(":")
31+
scheme = scheme.upper() if uppercase else scheme.capitalize()
32+
assert uri(scheme + separator + rest) is True
33+
34+
35+
@pytest.mark.parametrize("value", ["HTTP://", "FILE:path.txt", "TELNET://example.com/path"])
36+
def test_uppercase_schemes_still_validate_the_rest(value: str):
37+
"""Apply scheme-specific validation after normalizing the scheme."""
38+
assert not uri(value)
39+
40+
1041
@pytest.mark.parametrize(
1142
"value",
1243
[

0 commit comments

Comments
 (0)