2

Here's a test I've tried writing:

import httpretty
import requests


@httpretty.activate
def test():

    httpretty.register_uri(
        httpretty.GET,
        "http://localhost:8000",
        body='{"origin": "127.0.0.1"}',
        status=200
    )
    requests.get("http://localhost:8000")

When I run it with pytest, however, I get

requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionRefusedError(111, 'Connection refused'))

Looking at the documentation, I thought I'd followed it ad verbatim - any suggestions?

ignoring_gravity
  • 6,677
  • 4
  • 32
  • 65
  • 1
    Doesn't work for me either, nor with 3.7 neither with 3.8. I guess the lib is broken, [report it on Github](https://github.com/gabrielfalcao/HTTPretty/issues/new). There are other comparable libs if need a replacement in the meantime; I have used [`requests-mock`](https://pypi.org/project/requests-mock/) successfully. It only mocks `requests` calls though, not the low-level `socket` lib. – hoefling May 01 '20 at 15:52
  • Thanks! I've got around my problem using the pytest plugin `pytest-localserver` – ignoring_gravity May 01 '20 at 15:59

1 Answers1

1

The specific problem with the code block in the question is that when specifying a port, httpretty needs you to include a trailing slash in the url in the call to register_uri. There's actually a warning about this in the documentation for register_uri, but it still strikes me as something of a missing stair.

So, the code you would need for your basic example to work is:

httpretty.register_uri(
        httpretty.GET,
        "http://localhost:8000/",
        body='{"origin": "127.0.0.1"}',
        status=200
    )

I found this tip and also ran into my own problems which seem to indicate that maybe specifying a port isn't the only factor in play when it comes to trailing slashes.

I ended up switching to the responses library, which seems to get along better with pytest fixtures anyway.

AlanR
  • 131
  • 2
  • 7