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

feat(uri): add telnet URI validation per RFC 4248 (#388) by tofetpuzo · Pull Request #446 · 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
49 changes: 48 additions & 1 deletion src/validators/uri.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 @@ -3,8 +3,12 @@
# Read: https://stackoverflow.com/questions/176264
# https://www.rfc-editor.org/rfc/rfc3986#section-3

# standard
from urllib.parse import urlsplit

# local
from .email import email
from .hostname import hostname
from .url import url
from .utils import validator

Expand All @@ -21,6 +25,46 @@ def _ipfs_url(value: str):
return True


def _telnet_url(value: str):
"""Validate a telnet URL per RFC 4248.

Ref: https://www.rfc-editor.org/rfc/rfc4248"
Examples:
>>> _telnet_url('telnet://example.com')
True
>>> _telnet_url('telnet://example.com:23')
True
>>> _telnet_url('telnet://example.com:23/')
True

Args:
value:
Telnet URL to validate.

Returns:
(Literal[True]): If `value` is a Telnet URI.
(ValidationError): If `value` is an invalid URI.
"""
try:
scheme, netloc, path, query, fragment = urlsplit(value)
except ValueError:
return False

if scheme != "telnet":
return False
if not netloc:
return False
if query or fragment:
return False
if path and path != "/":
return False

# handling any additional checks user/pass
if "@" in netloc:
netloc = netloc.rsplit("@", 1)[1]
return hostname(netloc, may_have_port=True)


@validator
def uri(value: str, /):
"""Return whether or not given value is a valid URI.
Expand Down Expand Up @@ -60,7 +104,6 @@ def uri(value: str, /):
"rtsp",
"sftp",
"ssh",
"telnet",
}
# fmt: on
):
Expand Down Expand Up @@ -98,4 +141,8 @@ def uri(value: str, /):
if value.startswith("urc:"):
return True

# telnet
if value.startswith("telnet:"):
return _telnet_url(value)

return False
39 changes: 39 additions & 0 deletions tests/test_uri.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
@@ -0,0 +1,39 @@
"""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",
],
)
def test_invalid_telnet_uri(value: str):
"""Test invalid telnet URI."""
assert not uri(value)
Loading

Back | FazBrowse Home | New Git URL