You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the jaraco.abode project, the project makes extensive use of mocks for the test suite.
Historically, the project used full URLs to register the mocked URLs, but more recently, the project is using requests_toolbox and relative URLs (just the path).
And requests-mock does support passing just a path for the URL parameter. Unfortunately, it only accepts paths that contain a leading slash (paths without a leading slash such as this will fail to match).
However, much of the API, including paths passed from the system under test, will supply paths without a leading slash, and it's expected that these paths will join to the URL to produce a suitable request URL. It would be nice if requests-mock could honor this expectation as well.
Run with pip-run -q requests-toolbelt requests-mock -- -m doctest foo.txt results in:
**********************************************************************
File "foo", line 9, in foo
Failed example:
session.get('path').status_code
Exception raised:
Traceback (most recent call last):
File "/opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/doctest.py", line 1350, in __run
exec(compile(example.source, filename, "single",
File "<doctest foo[6]>", line 1, in<module>
session.get('path').status_code
File "/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-_cd4ze9b/requests/sessions.py", line 600, in get
returnself.request("GET", url, **kwargs)
File "/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-_cd4ze9b/requests_toolbelt/sessions.py", line 76, in request
returnsuper(BaseUrlSession, self).request(
File "/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-_cd4ze9b/requests/sessions.py", line 587, in request
resp =self.send(prep, **send_kwargs)
File "/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-_cd4ze9b/requests/sessions.py", line 701, in send
r = adapter.send(request, **kwargs)
File "/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-_cd4ze9b/requests_mock/adapter.py", line 261, in send
raise exceptions.NoMockAddress(request)
requests_mock.exceptions.NoMockAddress: No mock address: GET http://test.com/path
**********************************************************************
1 items had failures:
1 of 7 in foo
***Test Failed*** 1 failures.
I've been able to work around the limitation with this patch, but that's far from elegant.
Alternatively, if there were a hook to alter the matcher behavior from the mocker (e.g. mocker.set_matcher_option(infer_leading_slash=True) or mocker.install_url_adapter(adapter)), that could be used to more elegantly alter the default behavior.
The text was updated successfully, but these errors were encountered:
In the jaraco.abode project, the project makes extensive use of mocks for the test suite.
Historically, the project used full URLs to register the mocked URLs, but more recently, the project is using requests_toolbox and relative URLs (just the path).
And requests-mock does support passing just a path for the URL parameter. Unfortunately, it only accepts paths that contain a leading slash (paths without a leading slash such as this will fail to match).
However, much of the API, including paths passed from the system under test, will supply paths without a leading slash, and it's expected that these paths will join to the URL to produce a suitable request URL. It would be nice if requests-mock could honor this expectation as well.
For example:
Run with
pip-run -q requests-toolbelt requests-mock -- -m doctest foo.txt
results in:I've been able to work around the limitation with this patch, but that's far from elegant.
The mock logic already automatically appends a
/
to an empty path in the request. I wonder if it couldn't also always prepend a slash to the expected path.Alternatively, if there were a hook to alter the matcher behavior from the mocker (e.g.
mocker.set_matcher_option(infer_leading_slash=True)
ormocker.install_url_adapter(adapter)
), that could be used to more elegantly alter the default behavior.The text was updated successfully, but these errors were encountered: