diff --git a/oda_api/misc_helpers.py b/oda_api/misc_helpers.py index 970ea73b..65a3ba71 100644 --- a/oda_api/misc_helpers.py +++ b/oda_api/misc_helpers.py @@ -5,15 +5,13 @@ regex_url = re.compile( r'^https?://' # http:// or https:// - r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain... - r'localhost|' # localhost... - r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip + r'[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*' # domain... r'(?::\d+)?' # optional port - r'(?:/?|[/?]\S+)$', re.IGNORECASE) + r'(?:/[a-z-/]*)?$', re.IGNORECASE) def validate_url(url): - return re.match(regex_url, url) is not None + return regex_url.match(url) is not None def clean_var_name(s): diff --git a/tests/test_basic.py b/tests/test_basic.py index c5f5f7c8..c8840174 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -8,6 +8,7 @@ import requests import oda_api.api import oda_api.token +from oda_api.misc_helpers import validate_url from cdci_data_analysis.pytest_fixtures import DispatcherJobState from conftest import remove_old_token_files, remove_scratch_folders @@ -229,6 +230,31 @@ def test_default_url_init(): assert disp.url == "https://www.astro.unige.ch/mmoda/dispatch-data" +@pytest.mark.parametrize("protocol", [('http://', True), ('https://', True), ('', False)]) +@pytest.mark.parametrize("hostname", [("localhost", True), + ("oda-dispatcher", True), + ("foo.bar.baz", True), + ("1.2.3.4", True), + ("", False), + ("*", False), + ("foo-", False,), + ("bar.", False)]) +@pytest.mark.parametrize("port", [("", True), (":8000", True), (':ab', False)]) +@pytest.mark.parametrize("path", [("", True), + ("/", True), + ("/foo", True), + ("/foo/", True), + ("/foo/b-ar", True), + ("/foo/b^ar", False)]) +def test_validate_url(protocol, hostname, port, path): + url = f"{protocol[0]}{hostname[0]}{port[0]}{path[0]}" + if protocol[1] and hostname[1] and port[1] and path[1]: + assert validate_url(url) is True + else: + assert validate_url(url) is False + + + @pytest.mark.parametrize("protocol_url", ['http://', 'https://', '']) @pytest.mark.parametrize("init_parameter", ['host', 'url']) @pytest.mark.parametrize("protocol_parameter_value", ['http', 'https', '', None, 'not_included'])