Closed
Description
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']}