Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better url validation regex #220

Merged
merged 1 commit into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Check warning on line 14 in oda_api/misc_helpers.py

View check run for this annotation

Codecov / codecov/patch

oda_api/misc_helpers.py#L14

Added line #L14 was not covered by tests


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 @@
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

Check warning on line 252 in tests/test_basic.py

View check run for this annotation

Codecov / codecov/patch

tests/test_basic.py#L250-L252

Added lines #L250 - L252 were not covered by tests
else:
assert validate_url(url) is False

Check warning on line 254 in tests/test_basic.py

View check run for this annotation

Codecov / codecov/patch

tests/test_basic.py#L254

Added line #L254 was not covered by tests



@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
Loading