I'm looking for hints or examples that illustrate how to unit test code that makes HTTP requests using twisted.web.client.Agent.
Is it possible to use the real Agent in tests and configure it to connect to a fake HTTP server using StringTransport (no real TCP connetion)?
Or is it better to mock out the Agent and inject a mock into classes that use the Agent?
it is certainly possible to use a real Agent
. You must construct the Agent
instance with a reactor as first argument; as such, you can provide a fake reactor, such as MemoryReactor
. Although that's a pretty handy way to get down in the guts of what Agent
is doing, it's really mostly useful for testing Agent
itself. Injecting a fake Agent
is many times an easier route, because its api is both shallow (almost everything passes through Agent.request
and simple (it returns an IResponse
)