HTTPretty icon indicating copy to clipboard operation
HTTPretty copied to clipboard

Allow local urls

Open sebslomski opened this issue 8 years ago • 5 comments

Hi, I'd like to use httpretty to speed up my tests as we have some lookups to googleapis and others. Is it possible set allow_net_connect = False, mock the desired requests with fixtures and still allow some urls, such as a local Elastic Search server?

This is what my TestCase looks like:

class MockedHTTPTestsMixin(object):

    def run(self, result=None):
        httpretty.enable()
        httpretty.HTTPretty.allow_net_connect = False

        httpretty.register_uri(
            httpretty.GET,
            r'http://maps.googleapis.com/maps/api/geocode/json?.*',
            body=google_json,
        )

        try:
            super(MockedHTTPTestsMixin, self).run(result)
        finally:
            httpretty.disable()
            httpretty.reset()

sebslomski avatar Jul 26 '16 12:07 sebslomski

allowing a whitelist of hostnames would be nice (so it could work in a containerized environment)

timc13 avatar Jun 13 '17 19:06 timc13

There is some progress on this? I am trying to mock a request to JIRA API but I need to allow local requests to an internal server. I am having a BadStatusLine exception.

  File "/home/vagrant/www/mox/mox/lib/requests/api.py", line 58, in post
    response = Response(requests0.post(url, data=data, json=json, **kwargs))
  File "/home/vagrant/.virtualenvs/moxenv/local/lib/python2.7/site-packages/requests/api.py", line 111, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/home/vagrant/.virtualenvs/moxenv/local/lib/python2.7/site-packages/requests/api.py", line 57, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/vagrant/.virtualenvs/moxenv/local/lib/python2.7/site-packages/requests/sessions.py", line 475, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/vagrant/.virtualenvs/moxenv/local/lib/python2.7/site-packages/requests/sessions.py", line 585, in send
    r = adapter.send(request, **kwargs)
  File "/home/vagrant/.virtualenvs/moxenv/local/lib/python2.7/site-packages/requests/adapters.py", line 454, in send
    raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', BadStatusLine("''",))

jondeandres avatar Jul 26 '17 15:07 jondeandres

I am also interested in this. We need to block all outgoing requests to 3rd-party APIs whilst allowing requests to Elasticsearch. A whitelist of hostnames as @timc13 suggested, would solve this.

sloria avatar Jun 21 '18 16:06 sloria

@gabrielfalcao I am working on a MR for this. So far it works for my need, but my test case hangs. I feel I am missing something. Could you point me to the right direction? Here is the feature branch on my fork.

dapicester avatar Nov 26 '21 09:11 dapicester

I figured out how to fix the test and I have submitted a PR #448 .

dapicester avatar Nov 30 '21 03:11 dapicester