Don't mock the Session
, use a Transport Adapter
. =)
The Session
object does substantial processing on the request, and patching it at that level won't get you the request.
Instead, write a Transport Adapter
whose send()
method stores off the PreparedRequest
object.
For examples, see the official docs and an article I wrote. However, you'll want something like this:
from requests.adapters import HTTPAdapter
class TestAdapter(HTTPAdapter):
"""
A Transport Adapter that stores all requests sent, and provides pre-canned responses.
"""
def __init__(self, responses):
self.responses = responses
self.requests = requests
def send(self, request, *args, **kwargs):
self.requests.append(request)
return self.responses.pop(0)
To get this to work responses
will need to be a list of urllib3.Response
objects or something similar that Requests can work with. TestAdapter.requests
will be a list of requests.PreparedRequest
objects.
If you don't want to do all this work, you can try something like betamax.