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

URL match improvements #84

Closed

Conversation

SlavaSkvortsov
Copy link
Contributor

Refactored URL matching, moved URL-related logic to a separate class.
The main goal was to do a params-independent URL comparing. It was hard to use the lib for GET requests (had to use regex matching)

@lundberg
Copy link
Owner

lundberg commented Oct 3, 2020

Thanks for contributing to RESPX @SlavaSkvortsov !

I like the idea of breaking up the code for readability and easier (re)use.

Though, I think we need to take the discussion in #81, by @jocke-l, into account and start using httpx.URL.

Two reasons, less "duplicated" code from HTTPX to maintain, and to align with the HTTPX api, as discussed in #79, i.e. support params kwarg.

I'd like to see an api where one could do...

@respx.mock(base_url="https://example.org")
async def test_something(respx_mock):
    respx_mock.post("/foobar/", params={"foo": "bar"})

...but also, if possible, even...

@respx.mock(base_url="https://example.org")
async def test_something(respx_mock):
    respx_mock.post(r"/foobar/(?P<slug>\w+)/", params={"foo": "bar"})

Meaning, it would be nice to support both base_url, regular string url, regex url and optional params for merge.

@SlavaSkvortsov
Copy link
Contributor Author

SlavaSkvortsov commented Oct 3, 2020

Thank you for maintaining this lib, you are doing a great job!

I'll change the PR, but I don't think I can implement regular string URL support at this iteration. Maybe later :)

@lundberg
Copy link
Owner

lundberg commented Oct 5, 2020

Thanks for the update, but I think you mis-interpreted by last comment ;-)

What I meant was that it would be best if this PR extends/depends on #81.

It's better to keep each PR small and specific to make reviews easier and to minimize introduction of bugs. Also the commits and changelog gets clearer this way.

Let's merge #81 first, and then come back to this PR, sounds good @SlavaSkvortsov ?

@SlavaSkvortsov
Copy link
Contributor Author

Yeap, sounds great :)

Slava Skvortsov added 3 commits October 14, 2020 19:42
# Conflicts:
#	respx/api.py
#	respx/models.py
#	respx/transports.py
#	tests/test_api.py
@SlavaSkvortsov
Copy link
Contributor Author

This one looks ready as well.
The main idea here - you should never compare URL params as strings (or add them as a part of regex). Also, if your pattern is http://foo.bar the URL http://foo.bar?qwe=qwe should match.

Copy link
Owner

@lundberg lundberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice rebase and refactoring!

respx/models.py Outdated
url = httpx.URL(url_parts)
url, params = self._extract_params(url)

if self._params is not None and self._params != params:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

QueryParams can be bool evaluated.

Suggested change
if self._params is not None and self._params != params:
if self._params and self._params != params:

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, should we require full match on all params, or treat them as pattern matching and accept kwargs params only to be included in possibly "richer" request url params?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, should we require full match on all params, or treat them as pattern matching and accept kwargs params only to be included in possibly "richer" request url params?

It's a nice question. I thought about it today. I think the second option is better

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm also leaning to second option, i.e. accept pattern params to be subset of request params.

Major reason is that it aligns with your proposal of empty/missing pattern params matching requests with params.

self._params = httpx.QueryParams(params)

if isinstance(url, (str, tuple, httpx.URL)):
self._url = base_url.join(httpx.URL(url))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here will base_url be joined with it self, when url arg is None, due to line 388.

Maybe not a problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I'll take a look

SlavaSkvortsov and others added 2 commits October 15, 2020 13:12
Co-authored-by: Jonas Lundberg <jonas@5monkeys.se>
@lundberg
Copy link
Owner

Sorry, but this one is too outdated now as of #96.

Please check out the new patterns to see if any of your logic is missing.

@lundberg lundberg closed this Oct 24, 2020
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

Successfully merging this pull request may close these issues.

None yet

2 participants