FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Accept file:// URLs per RFC 8089 by itzzdev09 · Pull Request #483 · python-validators/validators · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
28 changes: 18 additions & 10 deletions src/validators/url.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def _validate_scheme(value: str):
value
# fmt: off
in {
"file",
"ftp",
"ftps",
"git",
Expand Down Expand Up @@ -240,18 +241,25 @@ def url(
except ValueError:
return False

# RFC 8089: a "file" URL's authority is commonly empty or "localhost",
# neither of which is a hostname `_validate_netloc` would accept.
is_local_file = scheme == "file" and netloc in ("", "localhost")

return (
validate_scheme(scheme)
and _validate_netloc(
netloc,
skip_ipv6_addr,
skip_ipv4_addr,
may_have_port,
simple_host,
consider_tld,
private,
rfc_1034,
rfc_2782,
and (
is_local_file
or _validate_netloc(
netloc,
skip_ipv6_addr,
skip_ipv4_addr,
may_have_port,
simple_host,
consider_tld,
private,
rfc_1034,
rfc_2782,
)
)
and _validate_optionals(path, query, fragment, strict_query)
)
13 changes: 13 additions & 0 deletions tests/test_url.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,16 @@ def test_returns_failed_validation_on_invalid_url(value: str):
def test_returns_failed_validation_on_invalid_private_url(value: str, private: Optional[bool]):
"""Test returns failed validation on invalid private url."""
assert isinstance(url(value, private=private), ValidationError)


@pytest.mark.parametrize(
"value",
[
"file:///var/www",
"file:///C:/Windows",
"file://localhost/var/www",
],
)
def test_returns_string_for_valid_file_url(value: str):
"""Test returns true on valid file url. Ref: RFC 8089."""
assert url(value)

Back | FazBrowse Home | New Git URL