How to unit test code that makes HTTP requests with twisted.web.client.Agent?

独自空忆成欢 提交于 2019-12-06 06:36:40

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/18078847/how-to-unit-test-code-that-makes-http-requests-with-twisted-web-client-agent

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!