Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Allow support for paths without a leading slash #215

Open
jaraco opened this issue Oct 26, 2022 · 0 comments
Open

Feature: Allow support for paths without a leading slash #215

jaraco opened this issue Oct 26, 2022 · 0 comments

Comments

@jaraco
Copy link
Contributor

jaraco commented Oct 26, 2022

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:

# foo.txt
>>> from requests_toolbelt import sessions
>>> import requests_mock

>>> session = sessions.BaseUrlSession('http://test.com')
>>> adapter = requests_mock.Adapter()
>>> session.mount('http://', adapter)

>>> val = adapter.register_uri('GET', 'path', text='data')
>>> session.get('path').status_code

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/[email protected]/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
        return self.request("GET", url, **kwargs)
      File "/var/folders/sx/n5gkrgfx6zd91ymxr2sr9wvw00n8zm/T/pip-run-_cd4ze9b/requests_toolbelt/sessions.py", line 76, in request
        return super(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.

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) or mocker.install_url_adapter(adapter)), that could be used to more elegantly alter the default behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant