getsentry / responses

A utility for mocking out the Python Requests library.
Apache License 2.0
4.17k stars 355 forks source link

Using responses to mock a requests.get from a different object #29

Closed kura closed 10 years ago

kura commented 10 years ago

I have a class that sets up and handles HTTP requests for me

class Client(object):
    api_base = "https://api.digitalocean.com/v2/"

    def get(self, url):
        r = requests.get("{0}{1}".format(self.api_base, url))
        r.raise_for_status()
        return json.loads(r.text)

Which is called similar to this

cli = Client()
print cli.get("test")

When I try to use this from within a TestCase via nosetests I get a 404 error from requests and it actually does the HTTP request to get that error, instead of using the mocked content from responses.

class TestClient(unittest.TestCase):

    @responses.activate
    def test_get_json(self):
        url = "https://api.digitalocean.com/v2/kura"
        responses.add(responses.GET, url,
                      body='{"message": "something"}', status=200,
                      content_type="application/json")
        j = self.cli.get("kura")
        self.assertEquals(j['message'], "something")

This is the final HTTPError exception that is triggered.

Traceback (most recent call last):
  File "/home/kura/.virtualenvs/batfish-python2.7/local/lib/python2.7/site-packages/responses.py", line 118, in wrapped
    return func(*args, **kwargs)
  File "/home/kura/workspace/batfish/tests/test_client.py", line 50, in test_get_json
    j = self.cli.get(uri)
  File "/home/kura/workspace/batfish/batfish/client.py", line 81, in get
    r = requests.get("{0}{1}".format(self.api_base, url), headers=headers)
  File "/home/kura/.virtualenvs/batfish-python2.7/local/lib/python2.7/site-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/home/kura/.virtualenvs/batfish-python2.7/local/lib/python2.7/site-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/kura/.virtualenvs/batfish-python2.7/local/lib/python2.7/site-packages/requests/sessions.py", line 456, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/kura/.virtualenvs/batfish-python2.7/local/lib/python2.7/site-packages/responses.py", line 163, in _on_request
    raise response
ConnectionError: Connection refused: https://api.digitalocean.com/v2/kura/

As you can see, this is actually sending the request off to the server, rather than serving up the mocked response.

kura commented 10 years ago

Seems this was me being stupid...

victorevector commented 9 years ago

@kura Hi Kura, Ive been having the same problem. Could you please explain what you originally did wrong or overlooked?

kura commented 9 years ago

@victorevector this was over 6 months ago but, I believe it may had had something to do with the fact that I was defining the URL to mock a response for as https://api.digitalocean.com/v2/kura but the URL my client code actually requested was https://api.digitalocean.com/v2/kura/.

I believe my client code may have been appending the trailing slash, causing the mocked URL to be different.