3

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?

Jan Wrobel
  • 6,969
  • 3
  • 37
  • 53

1 Answers1

4

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)

SingleNegationElimination
  • 151,563
  • 33
  • 264
  • 304
  • 2
    See also https://twistedmatrix.com/trac/ticket/4024 and https://github.com/iffy/agentforhire – Jean-Paul Calderone Aug 06 '13 at 12:11
  • Thank you, I'll try to go the mock route. Are you aware of an existing IResponse implementation that could be used for tests? It seems fairly straightforward to roll my own, but maybe there is no such need? – Jan Wrobel Aug 06 '13 at 14:52
  • 1
    You can use the same implementation returned by Agent, namely `twisted.web.client.Response`, it works fine even when not connected to a real transport. Alternatively, a trivial implementation is also sufficient: `collections.NamedTuple("MyResponse", "version code phrase headers length deliverBody")` – SingleNegationElimination Aug 06 '13 at 16:18