Mocking Twisted web client HTTP requests using HTTPretty

779 Views Asked by At

As Httpretty works on the Python socket layer, even Twisted web requests should be mocked out. But i am seeing some weird behavior on using httpretty. It tries to connect to localhost somehow. Below example shows the difference:

import httpretty
from twisted.internet import reactor
from twisted.internet.defer import Deferred
from twisted.web.client import Agent
from 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 from Sync: %s' % \
            requests.get('http://example.com').status_code

main()

And the response is :

Async Failed : [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.ConnectionRefusedError'>: Connection was refused by other side: 111: Connection refused.
]
Response received from Sync: 200

How can i use httpretty with Twisted web client?

1

There are 1 best solutions below

2
On BEST ANSWER

You can't. HTTPretty is blocking HTTP client libraries (like requests). It doesn't mock non-blocking sockets.