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

Add assertion helpers for prepared requests #4

Closed
DevL opened this issue Dec 29, 2022 · 1 comment · Fixed by #12
Closed

Add assertion helpers for prepared requests #4

DevL opened this issue Dec 29, 2022 · 1 comment · Fixed by #12
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@DevL
Copy link
Contributor

DevL commented Dec 29, 2022

The assertions function/functions are invoked with a PreparedRequest object that can be unwieldy to work with. For example, a URL containing encoded query parameters aren't fun to work with.

Python's standard library urllib.parse to the rescue!
We can use urllib.parse.urlparse combined with urllib.parse.parse_qs to break the URL apart into its components and then provide nicer abstractions to work with when writing assertions functions.

>>> from urllib.parse import parse_qs, urlparse
>>> url = "https://www.example.com/some/endpoint?name=Lennart&beverages=tea%2Ctonic%2Cbeer"
>>> urlparse(url)
ParseResult(scheme='https', netloc='www.example.com', path='/some/endpoint', params='', query='name=Lennart&beverages=tea%2Ctonic%2Cbeer', fragment='')
>>> parsed = urlparse(url)
>>> parsed.query
'name=Lennart&beverages=tea%2Ctonic%2Cbeer'
>>> parse_qs(parsed.query)
{'name': ['Lennart'], 'beverages': ['tea,tonic,beer']}
@DevL DevL added the enhancement New feature or request label Dec 29, 2022
@DevL
Copy link
Contributor Author

DevL commented Dec 29, 2022

parse_qs returns a dict where the keys are lists as the same query parameter can be occur multiple times in the same string. This needs to be handled. At the same time, it'd be helpful to unpack single values so that assertions doesn't have to wrap the expected value in a list.

Params with missing values should be removed.

>>> from urllib.parse import parse_qs
>>> query_string = 'a=1&a=2&b=3&c='
>>> parse_qs(query_string)
{'a': ['1', '2'], 'b': ['3']}

The above should be accessible like

parsed_request = ParsedRequest(prepared_request)
assert parsed_request.query == {'a': ['1', '2'], 'b': '3'}

@DevL DevL added this to the 2.0 milestone Feb 28, 2024
@DevL DevL modified the milestones: 2.0, 1.2 Feb 29, 2024
@DevL DevL linked a pull request Mar 1, 2024 that will close this issue
@DevL DevL closed this as completed in #12 Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants