Skip to content

Commit

Permalink
Fix and drop location tests
Browse files Browse the repository at this point in the history
  - Drop urlsplit() tests for https://kb.cert.org/vuls/id/127587, but
    assured that locations APIs in package strip leading and trailing
    spaces before processing URN/URL.
  • Loading branch information
brunato committed Sep 15, 2024
1 parent 3dc6388 commit 51a7e91
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
20 changes: 14 additions & 6 deletions tests/test_locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
Expand All @@ -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')
Expand Down Expand Up @@ -197,21 +202,20 @@ 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)

# 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'),
Expand Down Expand Up @@ -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')
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions xmlschema/locations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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):
Expand Down Expand Up @@ -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 \
Expand All @@ -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(
Expand Down

0 comments on commit 51a7e91

Please sign in to comment.