Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from rfc3986.exceptions import InvalidAuthority, ResolutionError
from rfc3986.misc import URI_MATCHER
from rfc3986.uri import URIReference
from . import base
@pytest.fixture
def scheme_and_path_uri():
return "mailto:user@example.com"
class TestURIReferenceParsesURIs(base.BaseTestParsesURIs):
"""Tests for URIReference handling of URIs."""
test_class = URIReference
def test_authority_info_raises_InvalidAuthority(self, invalid_uri):
"""Test that an invalid IPv6 is caught by authority_info()."""
uri = URIReference.from_string(invalid_uri)
with pytest.raises(InvalidAuthority):
uri.authority_info()
def test_attributes_catch_InvalidAuthority(self, invalid_uri):
"""Test that an invalid IPv6 is caught by authority_info()."""
uri = URIReference.from_string(invalid_uri)
assert uri.host is None
assert uri.userinfo is None
assert uri.port is None
def test_handles_absolute_path_uri(self, absolute_path_uri):
"""Test that URIReference can handle a path-only URI."""
def test_hostname_normalization():
assert URIReference(
None, "EXAMPLE.COM", None, None, None
) == URIReference(None, "example.com", None, None, None)
def test_invalid_query_component(self):
uri = URIReference(None, None, None, "foo#bar", None)
assert uri.is_valid() is False
def test_scheme_and_path_uri(self, scheme_and_path_uri):
uri = URIReference.from_string(scheme_and_path_uri)
assert uri == self.to_tuple(scheme_and_path_uri)
def test_wide_domain_bypass_check():
"""Verify we properly parse/handle the authority.
See also:
https://bugs.xdavidhu.me/google/2020/03/08/the-unexpected-google-wide-domain-check-bypass/
"""
url = "https://user:pass@xdavidhu.me\\test.corp.google.com:8080/path/to/something?param=value#hash"
ref = URIReference.from_string(url)
assert ref.scheme == "https"
assert ref.host == "xdavidhu.me"
def test_handles_absolute_path_uri(self, absolute_path_uri):
"""Test that URIReference can handle a path-only URI."""
uri = URIReference.from_string(absolute_path_uri)
assert uri.path == absolute_path_uri
assert uri.authority_info() == {
"userinfo": None,
"host": None,
"port": None,
}
def uris(request):
to_norm, normalized = request.param
return (
URIReference(None, None, to_norm, None, None),
URIReference(None, None, normalized, None, None),
)
def test_invalid_scheme(self):
uri = URIReference("123", None, None, None, None)
assert uri.is_valid() is False
def test_invalid_fragment_component(self):
uri = URIReference(None, None, None, None, "foo#bar")
assert uri.is_valid() is False
def test_merge_paths_with_base_authority_without_path():
"""Demonstrate merging with a base URI without an authority or path."""
base = URIReference(
scheme=None,
authority="authority",
path=None,
query=None,
fragment=None,
)
expected = "/relative"
assert merge_paths(base, "relative") == expected