Skip to content

Commit f228adb

Browse files
committed
Remove incorrect test, throw error when base IRI is invalid and improve parsing code
1 parent f0dd9cb commit f228adb

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

lib/pyld/iri_resolver.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,14 @@ def unresolve(absolute_iri: str, base_iri: str = ""):
222222
"""
223223
# TODO: better sync with jsonld.js version
224224
# skip IRI processing
225-
if base_iri is None:
225+
if not base_iri:
226226
return absolute_iri
227227

228228
base = parse_url(base_iri)
229+
230+
if not base.scheme:
231+
raise ValueError(f"Found invalid baseIRI '{base_iri}' for value '{absolute_iri}'")
232+
229233
rel = parse_url(absolute_iri)
230234

231235
# schemes and network locations (authorities) don't match, don't alter IRI
@@ -264,7 +268,8 @@ def parse_url(url):
264268
m = re.match(p, url)
265269
# remove default http and https ports
266270
g = list(m.groups())
267-
if ((g[0] == 'https' and g[1].endswith(':443')) or
271+
272+
if g[1] is not None and ((g[0] == 'https' and g[1].endswith(':443')) or
268273
(g[0] == 'http' and g[1].endswith(':80'))):
269274
g[1] = g[1][:g[1].rfind(':')]
270275
return ParsedUrl(*g)

tests/test_iri_resolver.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,13 @@ def test_hash_absolute(self):
302302
def test_colon_in_value_ignores_base(self):
303303
assert unresolve('http:abc', 'http://base.org/') == 'http:abc'
304304

305-
def test_colon_in_value_removes_dots(self):
306-
assert unresolve('http://abc/../../', 'http://base.org/') == 'http://abc/'
307-
308-
# def test_non_absolute_base_error(self):
309-
# with pytest.raises(ValueError, match=r"Found invalid baseIRI 'def' for value 'abc'"):
310-
# unresolve('abc', 'def')
305+
def test_non_absolute_base_error(self):
306+
with pytest.raises(ValueError, match=r"Found invalid baseIRI 'def' for value 'http://base.org/abc'"):
307+
unresolve('http://base.org/abc', 'def')
311308

312-
# def test_non_absolute_base_empty_value_error(self):
313-
# with pytest.raises(ValueError, match=r"Found invalid baseIRI 'def' for value ''"):
314-
# unresolve('', 'def')
309+
def test_non_absolute_base_empty_value_error(self):
310+
with pytest.raises(ValueError, match=r"Found invalid baseIRI 'def' for value ''"):
311+
unresolve('', 'def')
315312

316313
def test_base_without_path_slash(self):
317314
assert unresolve('http://base.org/abc', 'http://base.org') == 'abc'

0 commit comments

Comments
 (0)