Reputation: 8244
When testing Python software that uses the requests library, it is advised to mock requests.sessions.Session.request
.
Unfortunately that doesn't seem to work very well when having to deal with HTTP Authentication, I feel like I am mocking a too high abstraction layer in requests. How can I intercept/mock requests such that I don't have to deal with authentication myself (and just get the appropriate headers)?
Upvotes: 3
Views: 1341
Reputation: 15538
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.
Upvotes: 4