diff --git a/tests/test_locations.py b/tests/test_locations.py index aa1edcdc..80b1cbde 100644 --- a/tests/test_locations.py +++ b/tests/test_locations.py @@ -154,6 +154,11 @@ def test_urlsplit(self): ) def test_path_from_uri(self): + if platform.system() == 'Windows': + default_class = LocationWindowsPath + else: + default_class = LocationPosixPath + with self.assertRaises(ValueError) as ec: LocationPath.from_uri('') self.assertEqual(str(ec.exception), 'Empty URI provided!') @@ -163,11 +168,11 @@ def test_path_from_uri(self): self.assertEqual(str(path), '/names') path = LocationPosixPath.from_uri('file:///home/foo/names/?name=foo') - self.assertIsInstance(path, LocationPosixPath) + self.assertIsInstance(path, default_class) self.assertEqual(str(path), '/home/foo/names') path = LocationPosixPath.from_uri('file:///home/foo/names#foo') - self.assertIsInstance(path, LocationPosixPath) + self.assertIsInstance(path, default_class) self.assertEqual(str(path), '/home/foo/names') path = LocationPath.from_uri('file:///home\\foo\\names#foo') @@ -197,7 +202,6 @@ def test_path_from_uri(self): def test_get_uri(self): for url in URL_CASES: self.assertEqual(get_uri(*urlsplit(url)), url) - self.assertEqual(get_uri(*urlsplit(f' {url}')), url) url = 'D:/a/xmlschema/xmlschema/tests/test_cases/examples/' self.assertNotEqual(get_uri(*urlsplit(url)), url) @@ -205,13 +209,13 @@ def test_get_uri(self): # Test urlsplit() roundtrip with urlunsplit() for url in URL_CASES: if url == 'file:other.xsd': + # https://datatracker.ietf.org/doc/html/rfc8089#appendix-E.2.1 if sys.version_info < (3, 13): self.assertNotEqual(urlsplit(url).geturl(), url) elif url.startswith(('////', 'file:////')) and not is_unc_path('////'): self.assertNotEqual(urlsplit(url).geturl(), url) else: self.assertEqual(urlsplit(url).geturl(), url) - self.assertEqual(urlsplit(f' {url}').geturl(), url) def test_get_uri_path(self): self.assertEqual(get_uri_path('https', 'host', 'path', 'id=7', 'types'), @@ -370,7 +374,9 @@ def test_normalize_url_with_base_unc_path(self,): if is_unc_path('////filer01/MY_HOME/'): self.assertEqual(url, 'file://////filer01/MY_HOME/dev/XMLSCHEMA/test.xsd') else: - self.assertEqual(url, 'file:///filer01/MY_HOME/dev/XMLSCHEMA/test.xsd') + self.assertEqual( + url, f'file://{DRIVE_REGEX}/filer01/MY_HOME/dev/XMLSCHEMA/test.xsd' + ) with patch.object(os, 'name', 'posix'): self.assertEqual(os.name, 'posix') @@ -387,7 +393,9 @@ def test_normalize_url_with_base_unc_path(self,): if is_unc_path('////'): self.assertEqual(url, 'file://////filer01/MY_HOME/dev/XMLSCHEMA/test.xsd') else: - self.assertEqual(url, 'file:///filer01/MY_HOME/dev/XMLSCHEMA/test.xsd') + self.assertEqual( + url, f'file://{DRIVE_REGEX}/filer01/MY_HOME/dev/XMLSCHEMA/test.xsd' + ) def test_normalize_url_slashes(self): # Issue #116 diff --git a/xmlschema/locations.py b/xmlschema/locations.py index 002012f6..17551bcc 100644 --- a/xmlschema/locations.py +++ b/xmlschema/locations.py @@ -198,7 +198,7 @@ def normalize_url(url: str, base_url: Optional[str] = None, the whitespaces are replaced with `+` characters. :return: a normalized URL string. """ - url_parts = urlsplit(url) + url_parts = urlsplit(url.lstrip()) if not is_local_scheme(url_parts.scheme): return encode_url(url_parts.geturl(), method) @@ -207,7 +207,7 @@ def normalize_url(url: str, base_url: Optional[str] = None, return path.normalize().as_uri() if base_url is not None: - base_url_parts = urlsplit(base_url) + base_url_parts = urlsplit(base_url.lstrip()) base_path = LocationPath.from_uri(base_url) if is_local_scheme(base_url_parts.scheme): @@ -328,7 +328,7 @@ def is_safe_url(url: str, method: str = 'xml') -> bool: query_quote = quote_plus if method == 'html' else quote query_unquote = unquote_plus if method == 'html' else unquote - parts = urlsplit(url) + parts = urlsplit(url.lstrip()) path_safe = ':/\\' if is_local_scheme(parts.scheme) else '/' return parts.netloc == quote(unquote(parts.netloc), safe='@:') and \ @@ -345,7 +345,7 @@ def encode_url(url: str, method: str = 'xml') -> str: url = decode_url(url, method) query_quote = quote_plus if method == 'html' else quote - parts = urlsplit(url) + parts = urlsplit(url.lstrip()) path_safe = ':/\\' if is_local_scheme(parts.scheme) else '/' return get_uri(