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

Add feature to bypass responses per demand #39

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ A response can also throw an exception as follows.
# All calls to 'http://twitter.com/api/1/foobar' will throw exception.


Allow responses to perform real http-request
--------------------------------------------

.. code-block:: python

import responses
import requests


@responses.activate
def test_my_api():
responses.allow(responses.GET, 'https://twitter.com/api/1/foobar')
response = requests.get('https://twitter.com/api/1/foobar')
assert response.status_code == 404


Responses as a context manager
------------------------------

Expand Down
25 changes: 22 additions & 3 deletions responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,20 @@ def add(self, method, url, body='', match_querystring=False,
'stream': stream,
})

def allow(self, method, url, match_querystring=False):

# ensure the url has a default path set if the url is a string
if _is_string(url) and url.count('/') == 2:
url = url.replace('?', '/?', 1) if match_querystring \
else url + '/'

self._urls.append({
'url': url,
'method': method,
'match_querystring': match_querystring,
'allow': True,
})

def add_callback(self, method, url, callback, match_querystring=False,
content_type='text/plain'):
# ensure the url has a default path set if the url is a string
Expand Down Expand Up @@ -188,11 +202,11 @@ def _find_match(self, request):

break
else:
return None
return None, False
if self.assert_all_requests_are_fired:
# for each found match remove the url from the stack
self._urls.remove(match)
return match
return match, match.get('allow', False)

def _has_url_match(self, match, request_url):
url = match['url']
Expand Down Expand Up @@ -220,7 +234,8 @@ def _has_strict_url_match(self, url, other):
return url_qsl == other_qsl

def _on_request(self, adapter, request, **kwargs):
match = self._find_match(request)
match, allow = self._find_match(request)

# TODO(dcramer): find the correct class for this
if match is None:
error_msg = 'Connection refused: {0} {1}'.format(request.method,
Expand All @@ -230,6 +245,10 @@ def _on_request(self, adapter, request, **kwargs):
self._calls.add(request, response)
raise response

if allow:
original = self._patcher.temp_original
return original(adapter, request, **kwargs)

if 'body' in match and isinstance(match['body'], Exception):
self._calls.add(request, match['body'])
raise match['body']
Expand Down
21 changes: 21 additions & 0 deletions test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
absolute_import, print_function, division, unicode_literals
)

import random
import re
import requests
import responses
import pytest
import string

from inspect import getargspec
from requests.exceptions import ConnectionError, HTTPError
Expand Down Expand Up @@ -367,5 +369,24 @@ def run():
assert status_codes == [301, 301, 200]
assert_reset()


def test_allow_feature(monkeypatch):

@responses.activate
def run():
called = []

def capture_all(self, request, *args, **kw):
called.append(request)

monkeypatch.setattr(requests.Session, 'send', capture_all)

# hopefully this domain name doesn't exists
fake_url = 'http://{0}.com/'.format(''.join(
random.choice(string.digits) for i in range(60)))
responses.allow(responses.GET, fake_url)
requests.get(fake_url)
assert called[0].url == fake_url

run()
assert_reset()