Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow local urls #303

Open
sebslomski opened this issue Jul 26, 2016 · 5 comments
Open

Allow local urls #303

sebslomski opened this issue Jul 26, 2016 · 5 comments

Comments

@sebslomski
Copy link

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()
@timc13
Copy link

timc13 commented Jun 13, 2017

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

@jondeandres
Copy link

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("''",))

@sloria
Copy link

sloria commented Jun 21, 2018

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.

@dapicester
Copy link

@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 pushed a commit to dapicester/HTTPretty that referenced this issue Nov 30, 2021
Allow a whitelist of addresses to access the network.
Pass a list of tuples `(host, port)` as `whitelist` argument.
@dapicester
Copy link

dapicester commented Nov 30, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants