Skip to content

Commit

Permalink
better url validation regex (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsavchenko authored Oct 19, 2023
1 parent 99a7ffc commit 62f14c3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
8 changes: 3 additions & 5 deletions oda_api/misc_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'])
Expand Down

0 comments on commit 62f14c3

Please sign in to comment.