diff --git a/comictalker/talker_utils.py b/comictalker/talker_utils.py index b1b94c9..c1140b1 100644 --- a/comictalker/talker_utils.py +++ b/comictalker/talker_utils.py @@ -27,9 +27,14 @@ logger = logging.getLogger(__name__) def fix_url(url: str) -> str: + if not url: + return "" tmp_url = urlsplit(url) + new_path = posixpath.normpath(tmp_url.path) + if new_path in (".", "/"): + new_path = "" # joinurl only works properly if there is a trailing slash - tmp_url = tmp_url._replace(path=posixpath.normpath(tmp_url.path) + "/") + tmp_url = tmp_url._replace(path=new_path + "/") return tmp_url.geturl() diff --git a/tests/utils_test.py b/tests/utils_test.py index 43ebc94..ee02b6a 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -5,6 +5,7 @@ import os import pytest import comicapi.utils +import comictalker.talker_utils def test_os_sorted(): @@ -200,3 +201,20 @@ titles_2 = [ @pytest.mark.parametrize("value, result", titles_2) def test_sanitize_title(value, result): assert comicapi.utils.sanitize_title(value) == result.casefold() + + +urls = [ + ("", ""), + ("http://test.test", "http://test.test/"), + ("http://test.test/", "http://test.test/"), + ("http://test.test/..", "http://test.test/"), + ("http://test.test/../hello", "http://test.test/hello/"), + ("http://test.test/../hello/", "http://test.test/hello/"), + ("http://test.test/../hello/..", "http://test.test/"), + ("http://test.test/../hello/../", "http://test.test/"), +] + + +@pytest.mark.parametrize("value, result", urls) +def test_fix_url(value, result): + assert comictalker.talker_utils.fix_url(value) == result