def test_with_basic_and_relative_uris(self, basic_uri, relative_uri): R = URIReference.from_string(relative_uri) B = URIReference.from_string(basic_uri) T = R.resolve_with(basic_uri) assert T.scheme == B.scheme assert T.host == R.host assert T.path == R.path
def test_with_basic_uri_and_relative_path(self, basic_uri): R = URIReference.from_string('foo/bar/bogus') B = URIReference.from_string(basic_uri).normalize() T = R.resolve_with(B) assert T.scheme == B.scheme assert T.host == B.host assert T.path == '/' + R.path
def test_with_basic_and_absolute_path_uris(self, basic_uri, absolute_path_uri): R = URIReference.from_string(absolute_path_uri) B = URIReference.from_string(basic_uri).normalize() T = R.resolve_with(B) assert T.scheme == B.scheme assert T.host == B.host assert T.path == R.path
def test_basic_uri_with_path_and_relative_path(self, basic_uri_with_path): R = URIReference.from_string('foo/bar/bogus') B = URIReference.from_string(basic_uri_with_path).normalize() T = R.resolve_with(B) assert T.scheme == B.scheme assert T.host == B.host index = B.path.rfind('/') assert T.path == B.path[:index] + '/' + R.path
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 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
def test_merge_paths_with_base_path_without_base_authority(): """Demonstrate merging with a base URI without an authority.""" base = URIReference(scheme=None, authority=None, path='/foo/bar/bogus', query=None, fragment=None) expected = '/foo/bar/relative' assert merge_paths(base, 'relative') == expected
def test_handles_scheme_and_path_uri(self, scheme_and_path_uri): """Test that URIReference can handle a `scheme:path` URI.""" uri = URIReference.from_string(scheme_and_path_uri) assert uri.path == '*****@*****.**' assert uri.scheme == 'mailto' assert uri.query is None assert uri.host is None assert uri.port is None assert uri.userinfo is None assert uri.authority is None
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://*****:*****@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_merge_paths_with_base_authority_and_path(): """Demonstrate merging with a base URI with an authority and path.""" base = URIReference( scheme=None, authority="authority", path="/foo/bar/bogus", query=None, fragment=None, ) expected = "/foo/bar/relative" assert merge_paths(base, "relative") == expected
def test_handles_uri_with_everything(self, uri_with_everything): """ Test that URIReference can handle and with everything in it. """ uri = URIReference.from_string(uri_with_everything) assert uri.scheme == 'https' assert uri.path == '/path/to/resource' assert uri.query == 'key=value' assert uri.fragment == 'fragment' assert uri.userinfo == 'user:pass' assert uri.port == '443'
def test_handles_basic_uri(self, basic_uri): """Test that URIReference can handle a simple URI.""" uri = URIReference.from_string(basic_uri) assert uri.scheme == 'http' assert uri.authority == basic_uri[7:] # len('http://') assert uri.host == uri.authority assert uri.path is None assert uri.query is None assert uri.fragment is None assert uri.port is None assert uri.userinfo is None
def test_handles_basic_uri_with_port(self, basic_uri_with_port): """Test that URIReference can handle a simple URI with a port.""" uri = URIReference.from_string(basic_uri_with_port) assert uri.scheme == 'ftp' assert uri.authority == basic_uri_with_port[6:] # len('ftp://') assert uri.host != uri.authority assert uri.port == '21' assert uri.path is None assert uri.query is None assert uri.fragment is None assert uri.userinfo is None
def test_handles_uri_with_path_and_query(self, uri_with_path_and_query): """ Test that URIReference can handle a URI with a path and query. """ uri = URIReference.from_string(uri_with_path_and_query) assert uri.scheme == 'http' assert uri.host == uri.authority assert uri.path == '/path/to/resource' assert uri.query == 'key=value' assert uri.fragment is None assert uri.userinfo is None assert uri.port is None
def test_handles_basic_uri_with_path(self, basic_uri_with_path): """Test that URIReference can handle a URI with a path.""" uri = URIReference.from_string(basic_uri_with_path) assert uri.scheme == 'http' assert basic_uri_with_path == (uri.scheme + '://' + uri.authority + uri.path) assert uri.host == uri.authority assert uri.path == '/path/to/resource' assert uri.query is None assert uri.fragment is None assert uri.userinfo is None assert uri.port is None
def test_handles_uri_with_port_and_userinfo( self, uri_with_port_and_userinfo): """ Test that URIReference can handle a URI with a port and userinfo. """ uri = URIReference.from_string(uri_with_port_and_userinfo) assert uri.scheme == 'ssh' # 6 == len('ftp://') assert uri.authority == uri_with_port_and_userinfo[6:] assert uri.host != uri.authority assert uri.port == '22' assert uri.path is None assert uri.query is None assert uri.fragment is None assert uri.userinfo == 'user:pass'
def test_resolve_pathless_fragment(self, basic_uri): R = URIReference.from_string('#fragment') B = URIReference.from_string(basic_uri) T = R.resolve_with(B) assert T.path is None assert T.fragment == 'fragment'
def test_basic_uri_requiring_scheme(self, basic_uri): uri = URIReference.from_string(basic_uri) assert uri.is_valid(require_scheme=True) is True
def test_uri_with_everything_raises_exception(self, uri_with_everything): R = URIReference.from_string('foo/bar/bogus') B = URIReference.from_string(uri_with_everything) with pytest.raises(ResolutionError): R.resolve_with(B)
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_different_basic_uris(self, basic_uri, basic_uri_with_port): uri = URIReference.from_string(basic_uri) assert (uri == URIReference.from_string(basic_uri_with_port)) is False
def test_basic_uri_with_port_is_valid(self, basic_uri_with_port): uri = URIReference.from_string(basic_uri_with_port) assert uri.is_valid() is True
def uris(request): to_norm, normalized = request.param return ( URIReference(None, None, to_norm, None, None), URIReference(None, None, normalized, None, None), )
def test_basic_uri_requiring_authority(self, basic_uri): uri = URIReference.from_string(basic_uri) assert uri.is_valid(require_authority=True) is True
def test_uri_with_everything_requiring_fragment(self, uri_with_everything): uri = URIReference.from_string(uri_with_everything) assert uri.is_valid(require_fragment=True) is True
def test_empty_querystrings_persist(): url = 'https://httpbin.org/get?' ref = URIReference.from_string(url) assert ref.query == '' assert ref.unsplit() == url
def test_resolve_pathless_query(self, basic_uri): R = URIReference.from_string('?query') B = URIReference.from_string(basic_uri) T = R.resolve_with(B) assert T.path is None assert T.query == 'query'
def test_uri_with_port_and_userinfo_is_valid(self, uri_with_port_and_userinfo): uri = URIReference.from_string(uri_with_port_and_userinfo) assert uri.is_valid() is True
def test_basic_uri_with_path_is_valid(self, basic_uri_with_path): uri = URIReference.from_string(basic_uri_with_path) assert uri.is_valid() is True
def test_basic_uri_resolves_itself(self, basic_uri): R = URIReference.from_string(basic_uri) B = URIReference.from_string(basic_uri) T = R.resolve_with(B) assert T == B
def test_authority_normalization(authority, expected_authority): uri = URIReference(None, authority, None, None, None).normalize() assert uri.authority == expected_authority
def test_basic_uris_with_paths_are_absolute(self, basic_uri_with_path): uri = URIReference.from_string(basic_uri_with_path) assert uri.is_absolute() is True
def test_absolute_paths_are_not_absolute_uris(self, absolute_path_uri): uri = URIReference.from_string(absolute_path_uri) assert uri.is_absolute() is False
def test_same_basic_uri(self, basic_uri): uri = URIReference.from_string(basic_uri) assert uri == uri
def test_uri_comparison_raises_TypeError(basic_uri): uri = URIReference.from_string(basic_uri) with pytest.raises(TypeError): uri == 1
def test_differing_schemes(self, basic_uri): R = URIReference.from_string('https://example.com/path') B = URIReference.from_string(basic_uri) T = R.resolve_with(B) assert T.scheme == R.scheme
def test_uri_with_everything_are_not_absolute(self, uri_with_everything): uri = URIReference.from_string(uri_with_everything) assert uri.is_absolute() is False
def test_uri_with_path_and_query_is_valid(self, uri_with_path_and_query): uri = URIReference.from_string(uri_with_path_and_query) assert uri.is_valid() is True
def test_hostname_normalization(): assert URIReference(None, "EXAMPLE.COM", None, None, None) == URIReference(None, "example.com", None, None, None)
def test_fragment_normalization(): uri = URIReference(None, "example.com", None, None, "fiz%DF").normalize() assert uri.fragment == "fiz%DF"