Mocking Twisted web client HTTP requests using HTTPretty -
as httpretty works on python socket layer, twisted web requests should mocked out. seeing weird behavior on using httpretty. tries connect localhost somehow. below illustration shows difference:
import httpretty twisted.internet import reactor twisted.internet.defer import deferred twisted.web.client import agent twisted.web.http_headers import headers import requests @httpretty.activate def main(): httpretty.register_uri( httpretty.get, "http://example.com", body='[{"title": "test deal"}]', content_type="application/json") agent = agent(reactor) d = agent.request( 'get', 'http://example.com', headers({'user-agent': ['twisted web client example']}), none) def cberror(message): print 'async failed : %s' % message d.adderrback(cberror) def cbshutdown(ignored): reactor.stop() d.addboth(cbshutdown) reactor.run() print 'response received sync: %s' % \ requests.get('http://example.com').status_code main()
and response :
async failed : [failure instance: traceback (failure no frames): <class 'twisted.internet.error.connectionrefusederror'>: connection refused other side: 111: connection refused. ] response received sync: 200
how can utilize httpretty twisted web client?
you can't. httpretty blocking http client libraries (like requests). doesn't mock non-blocking sockets.
client twisted twisted.web httpretty
No comments:
Post a Comment