Skip to content

Commit 4ece827

Browse files
Merge pull request #202 from adamtheturtle/no-scheme-46
Test URLs with no scheme
2 parents 0e6b6b3 + 0a00db0 commit 4ece827

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

spelling_private_dict.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
KiB
22
MPixel
33
MiB
4+
MissingSchema
45
Ubuntu
56
admin
67
api

src/mock_vws/_decorators.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import re
77
from contextlib import ContextDecorator
88
from typing import Tuple, Union
9-
from urllib.parse import urljoin
9+
from urllib.parse import urljoin, urlparse
1010

11+
import requests
1112
from requests_mock.mocker import Mocker
1213

1314
from mock_vws.database import VuforiaDatabase
@@ -45,13 +46,26 @@ def __init__( # pylint: disable=too-many-arguments
4546
query_recognizes_deletion_seconds: The number of seconds after a
4647
target has been deleted that the query endpoint will return a
4748
500 response for on a match.
49+
50+
Raises:
51+
``requests.exceptions.MissingSchema``: There is no schema in a
52+
given URL.
4853
"""
4954
super().__init__()
5055
self._real_http = real_http
5156
self._mock: Mocker
5257

5358
self._base_vws_url = base_vws_url
5459
self._base_vwq_url = base_vwq_url
60+
missing_scheme_error = (
61+
'Invalid URL "{url}": No scheme supplied. '
62+
'Perhaps you meant "https://{url}".'
63+
)
64+
for url in (base_vwq_url, base_vws_url):
65+
result = urlparse(url)
66+
if not result.scheme:
67+
error = missing_scheme_error.format(url=url)
68+
raise requests.exceptions.MissingSchema(error)
5569

5670
self._mock_vws_api = MockVuforiaWebServicesAPI(
5771
processing_time_seconds=processing_time_seconds,

tests/mock_vws/test_usage.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import pytest
1212
import requests
13+
from requests.exceptions import MissingSchema
1314
from requests_mock.exceptions import NoMockAddress
1415

1516
from mock_vws import MockVWS
@@ -233,6 +234,26 @@ def test_custom_base_vwq_url(self) -> None:
233234
requests.post(url='https://vuforia.vwq.example.com/v1/query')
234235
requests.get('https://vws.vuforia.com/summary')
235236

237+
def test_no_scheme(self) -> None:
238+
"""
239+
An error if raised if a URL is given with no scheme.
240+
"""
241+
with pytest.raises(MissingSchema) as exc:
242+
MockVWS(base_vws_url='vuforia.vws.example.com')
243+
244+
expected = (
245+
'Invalid URL "vuforia.vws.example.com": No scheme supplied. '
246+
'Perhaps you meant "https://vuforia.vws.example.com".'
247+
)
248+
assert str(exc.value) == expected
249+
with pytest.raises(MissingSchema) as exc:
250+
MockVWS(base_vwq_url='vuforia.vwq.example.com')
251+
expected = (
252+
'Invalid URL "vuforia.vwq.example.com": No scheme supplied. '
253+
'Perhaps you meant "https://vuforia.vwq.example.com".'
254+
)
255+
assert str(exc.value) == expected
256+
236257

237258
class TestCustomQueryRecognizesDeletionSeconds:
238259
"""

0 commit comments

Comments
 (0)