HTTPretty
HTTPretty copied to clipboard
Allow local urls
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()
allowing a whitelist of hostnames would be nice (so it could work in a containerized environment)
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("''",))
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.
@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.
I figured out how to fix the test and I have submitted a PR #448 .