gabrielfalcao / HTTPretty

Intercept HTTP requests at the Python socket level. Fakes the whole socket module
https://httpretty.readthedocs.org
MIT License
2.11k stars 275 forks source link

Connection refused for Twisted web client HTTP agent #179

Open prat0318 opened 10 years ago

prat0318 commented 10 years ago

I am trying to mock my HTTP requests using httpretty for Twisted web client agent but every time i am getting a Connection refused error. Is Twisted Agent not supported or am i missing something? Has it worked for anyone else?

prat0318 commented 10 years ago

Code showcasing the difference:

import httpretty 
from twisted.internet import reactor
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://exampleqwert.com")

    agent = Agent(reactor)

    d = agent.request(
        'GET',
        'http://exampleqwert.com/',
        Headers({}),
        None)

    def cbResponse(ignored):
        print 'Response received from Async: %s' % ignored.code 
    d.addCallback(cbResponse)

    def cbShutdown(ignored): reactor.stop()
    d.addBoth(cbShutdown)

    reactor.run()

    print 'Response received from Sync: %s' % \
            requests.get('http://exampleqwert.com').status_code

main()

gives output as:

Response received from Async: 403
Response received from Sync: 200

For Async, it tries to read from the localhost, with my apache localhost server giving 403. But why is it reading localhost?