diff --git a/spelling_private_dict.txt b/spelling_private_dict.txt index d6a10c1e9..dce824580 100644 --- a/spelling_private_dict.txt +++ b/spelling_private_dict.txt @@ -1,6 +1,7 @@ KiB MPixel MiB +MissingSchema Ubuntu admin api diff --git a/src/mock_vws/_decorators.py b/src/mock_vws/_decorators.py index 5eaf39d09..27818dacf 100644 --- a/src/mock_vws/_decorators.py +++ b/src/mock_vws/_decorators.py @@ -6,8 +6,9 @@ import re from contextlib import ContextDecorator from typing import Tuple, Union -from urllib.parse import urljoin +from urllib.parse import urljoin, urlparse +import requests from requests_mock.mocker import Mocker from mock_vws.database import VuforiaDatabase @@ -45,6 +46,10 @@ def __init__( # pylint: disable=too-many-arguments query_recognizes_deletion_seconds: The number of seconds after a target has been deleted that the query endpoint will return a 500 response for on a match. + + Raises: + ``requests.exceptions.MissingSchema``: There is no schema in a + given URL. """ super().__init__() self._real_http = real_http @@ -52,6 +57,15 @@ def __init__( # pylint: disable=too-many-arguments self._base_vws_url = base_vws_url self._base_vwq_url = base_vwq_url + missing_scheme_error = ( + 'Invalid URL "{url}": No scheme supplied. ' + 'Perhaps you meant "https://{url}".' + ) + for url in (base_vwq_url, base_vws_url): + result = urlparse(url) + if not result.scheme: + error = missing_scheme_error.format(url=url) + raise requests.exceptions.MissingSchema(error) self._mock_vws_api = MockVuforiaWebServicesAPI( processing_time_seconds=processing_time_seconds, diff --git a/tests/mock_vws/test_usage.py b/tests/mock_vws/test_usage.py index 90a1cd8e5..7fef3ec2b 100644 --- a/tests/mock_vws/test_usage.py +++ b/tests/mock_vws/test_usage.py @@ -10,6 +10,7 @@ import pytest import requests +from requests.exceptions import MissingSchema from requests_mock.exceptions import NoMockAddress from mock_vws import MockVWS @@ -233,6 +234,26 @@ def test_custom_base_vwq_url(self) -> None: requests.post(url='https://vuforia.vwq.example.com/v1/query') requests.get('https://vws.vuforia.com/summary') + def test_no_scheme(self) -> None: + """ + An error if raised if a URL is given with no scheme. + """ + with pytest.raises(MissingSchema) as exc: + MockVWS(base_vws_url='vuforia.vws.example.com') + + expected = ( + 'Invalid URL "vuforia.vws.example.com": No scheme supplied. ' + 'Perhaps you meant "https://vuforia.vws.example.com".' + ) + assert str(exc.value) == expected + with pytest.raises(MissingSchema) as exc: + MockVWS(base_vwq_url='vuforia.vwq.example.com') + expected = ( + 'Invalid URL "vuforia.vwq.example.com": No scheme supplied. ' + 'Perhaps you meant "https://vuforia.vwq.example.com".' + ) + assert str(exc.value) == expected + class TestCustomQueryRecognizesDeletionSeconds: """