Skip to content

Commit

Permalink
added unit tests for is_url and url_exists Open-EO#571
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentVerelst committed Jun 17, 2024
1 parent be88f07 commit cc97ff0
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import pyproj
import pytest
import requests
import requests_mock
import shapely.geometry

from openeo.capabilities import ComparableVersion
Expand All @@ -29,11 +31,13 @@
ensure_list,
first_not_none,
guess_format,
is_url,
normalize_crs,
repr_truncate,
rfc3339,
str_truncate,
to_bbox_dict,
url_exists,
url_join,
)

Expand Down Expand Up @@ -1089,3 +1093,38 @@ def test_normalize_crs_succeeds_with_correct_projstring(self, epsg_input, expect
def test_normalize_crs_handles_incorrect_crs(self, epsg_input, use_pyproj):
with pytest.raises(ValueError):
normalize_crs(epsg_input, use_pyproj=use_pyproj)


@pytest.mark.parametrize(
"input_url, expected",
[
("http://example.com", True),
("https://example.com", True),
("http://example.com/path", True),
("http://example.com/path?query=1", True),
("example.com", False),
("http:///path", False),
("", False),
("/some/path/", False),
],
)
def test_is_url(input_url, expected):
assert is_url(input_url) == expected


@pytest.mark.parametrize(
"status_code, expected",
[
(200, True),
(404, False),
],
)
def test_url_exists(status_code, expected):
with requests_mock.Mocker() as mock:
mock.head("http://example.com", status_code=status_code)
assert url_exists("http://example.com") == expected

# Mocking a request that raises an exception
with requests_mock.Mocker() as mock:
mock.head("http://example.com", exc=requests.exceptions.RequestException)
assert url_exists("http://example.com") == False

0 comments on commit cc97ff0

Please sign in to comment.