From 1002deadba63bfcb61cff120e38e30f6eeaffbcc Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Sun, 25 Nov 2018 00:52:03 +0000 Subject: [PATCH 1/2] Handle having no scheme in URLs given to mock [skip ci] --- src/mock_vws/_decorators.py | 15 ++++++++++++++- tests/mock_vws/test_usage.py | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/mock_vws/_decorators.py b/src/mock_vws/_decorators.py index 5eaf39d09..62664d3fa 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 +from requests.exceptions import MissingSchema 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,14 @@ 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: + raise MissingSchema(missing_scheme_error.format(url=url)) 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: """ From 0a00db0ff680ad09630babaa9833b37ea67208ad Mon Sep 17 00:00:00 2001 From: Adam Dangoor Date: Sun, 25 Nov 2018 01:09:23 +0000 Subject: [PATCH 2/2] Fix lint issues [skip ci] --- spelling_private_dict.txt | 1 + src/mock_vws/_decorators.py | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) 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 62664d3fa..27818dacf 100644 --- a/src/mock_vws/_decorators.py +++ b/src/mock_vws/_decorators.py @@ -8,7 +8,7 @@ from typing import Tuple, Union from urllib.parse import urljoin, urlparse -from requests.exceptions import MissingSchema +import requests from requests_mock.mocker import Mocker from mock_vws.database import VuforiaDatabase @@ -48,8 +48,8 @@ def __init__( # pylint: disable=too-many-arguments 500 response for on a match. Raises: - requests.exceptions.MissingSchema: There is no schema in a given - URL. + ``requests.exceptions.MissingSchema``: There is no schema in a + given URL. """ super().__init__() self._real_http = real_http @@ -64,7 +64,8 @@ def __init__( # pylint: disable=too-many-arguments for url in (base_vwq_url, base_vws_url): result = urlparse(url) if not result.scheme: - raise MissingSchema(missing_scheme_error.format(url=url)) + error = missing_scheme_error.format(url=url) + raise requests.exceptions.MissingSchema(error) self._mock_vws_api = MockVuforiaWebServicesAPI( processing_time_seconds=processing_time_seconds,