From 08bb3bd187862923b97ca9a9ea0d2dc9a0999752 Mon Sep 17 00:00:00 2001 From: Aron Griffis Date: Sun, 23 Aug 2015 10:22:09 -0400 Subject: [PATCH 1/6] Remove an extra space --- vcr/cassette.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vcr/cassette.py b/vcr/cassette.py index ff63492..e393e40 100644 --- a/vcr/cassette.py +++ b/vcr/cassette.py @@ -164,7 +164,7 @@ class Cassette(object): return CassetteContextDecorator.from_args(cls, **kwargs) def __init__(self, path, serializer=yamlserializer, record_mode='once', - match_on=(uri, method), before_record_request=None, + match_on=(uri, method), before_record_request=None, before_record_response=None, custom_patches=(), inject=False): From eb1cdad03a711816250aec6a198b13140c93cbc8 Mon Sep 17 00:00:00 2001 From: Aron Griffis Date: Sun, 23 Aug 2015 10:23:21 -0400 Subject: [PATCH 2/6] self._before_record_response can never be falsy in Cassette (just like self._before_record_request above it) --- vcr/cassette.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vcr/cassette.py b/vcr/cassette.py index e393e40..bc7410f 100644 --- a/vcr/cassette.py +++ b/vcr/cassette.py @@ -210,8 +210,7 @@ class Cassette(object): request = self._before_record_request(request) if not request: return - if self._before_record_response: - response = self._before_record_response(response) + response = self._before_record_response(response) self.data.append((request, response)) self.dirty = True From a569dd4dc83795aedba310711086bbbe443bc249 Mon Sep 17 00:00:00 2001 From: Aron Griffis Date: Sun, 23 Aug 2015 10:23:50 -0400 Subject: [PATCH 3/6] Raise KeyError with message instead of print, just like in get_matchers below --- vcr/config.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/vcr/config.py b/vcr/config.py index 0c45844..b5319c2 100644 --- a/vcr/config.py +++ b/vcr/config.py @@ -67,10 +67,11 @@ class VCR(object): try: serializer = self.serializers[serializer_name] except KeyError: - print("Serializer {0} doesn't exist or isn't registered".format( - serializer_name - )) - raise KeyError + raise KeyError( + "Serializer {0} doesn't exist or isn't registered".format( + serializer_name + ) + ) return serializer def _get_matchers(self, matcher_names): From 3e5553c56a61cb4c02112f7a3ade5c9d86209e70 Mon Sep 17 00:00:00 2001 From: Aron Griffis Date: Sun, 23 Aug 2015 10:25:23 -0400 Subject: [PATCH 4/6] Don't drop passed-in before_record_response This is an actual bugfix. If before_record_response is passed into VCR as an iterable then it won't be included in filter_functions. This commit repairs the logic to separate the tests (just as it's already done for before_record_request). Also use .extend() rather than looping on .append() --- tests/unit/test_vcr.py | 26 ++++++++++++++++++++++++++ vcr/config.py | 15 +++++---------- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/tests/unit/test_vcr.py b/tests/unit/test_vcr.py index 1029de6..cbdcde4 100644 --- a/tests/unit/test_vcr.py +++ b/tests/unit/test_vcr.py @@ -72,6 +72,32 @@ def test_vcr_before_record_request_params(): assert cassette.filter_request(Request('GET', base_path + 'get', '', {})) is not None +def test_vcr_before_record_response_iterable(): + # Regression test for #191 + + request = Request('GET', '/', '', {}) + response = object() # just can't be None + + # Prevent actually saving the cassette + with mock.patch('vcr.cassette.save_cassette'): + + # Baseline: non-iterable before_record_response should work + mock_filter = mock.Mock() + vcr = VCR(before_record_response=mock_filter) + with vcr.use_cassette('test') as cassette: + assert mock_filter.call_count == 0 + cassette.append(request, response) + assert mock_filter.call_count == 1 + + # Regression test: iterable before_record_response should work too + mock_filter = mock.Mock() + vcr = VCR(before_record_response=(mock_filter,)) + with vcr.use_cassette('test') as cassette: + assert mock_filter.call_count == 0 + cassette.append(request, response) + assert mock_filter.call_count == 1 + + @pytest.fixture def random_fixture(): return 1 diff --git a/vcr/config.py b/vcr/config.py index b5319c2..030462a 100644 --- a/vcr/config.py +++ b/vcr/config.py @@ -158,12 +158,10 @@ class VCR(object): 'before_record_response', self.before_record_response ) filter_functions = [] - if before_record_response and not isinstance(before_record_response, - collections.Iterable): - before_record_response = (before_record_response,) - for function in before_record_response: - filter_functions.append(function) - + if before_record_response: + if not isinstance(before_record_response, collections.Iterable): + before_record_response = (before_record_response,) + filter_functions.extend(before_record_response) def before_record_response(response): for function in filter_functions: if response is None: @@ -224,9 +222,7 @@ class VCR(object): if before_record_request: if not isinstance(before_record_request, collections.Iterable): before_record_request = (before_record_request,) - for function in before_record_request: - filter_functions.append(function) - + filter_functions.extend(before_record_request) def before_record_request(request): request = copy.copy(request) for function in filter_functions: @@ -234,7 +230,6 @@ class VCR(object): break request = function(request) return request - return before_record_request @staticmethod From 7f02d65dd91ecba195ad55d03b32089e81d3342a Mon Sep 17 00:00:00 2001 From: Aron Griffis Date: Sun, 23 Aug 2015 10:29:02 -0400 Subject: [PATCH 5/6] Make hosts_to_ignore a set() earlier for clarity. --- vcr/config.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/vcr/config.py b/vcr/config.py index 030462a..a94520f 100644 --- a/vcr/config.py +++ b/vcr/config.py @@ -211,12 +211,10 @@ class VCR(object): ) ) - hosts_to_ignore = list(ignore_hosts) + hosts_to_ignore = set(ignore_hosts) if ignore_localhost: - hosts_to_ignore.extend(('localhost', '0.0.0.0', '127.0.0.1')) - + hosts_to_ignore.update(('localhost', '0.0.0.0', '127.0.0.1')) if hosts_to_ignore: - hosts_to_ignore = set(hosts_to_ignore) filter_functions.append(self._build_ignore_hosts(hosts_to_ignore)) if before_record_request: From 3305f0ca7df9fe125b217962b9e3a0614ef1bd10 Mon Sep 17 00:00:00 2001 From: Aron Griffis Date: Sun, 23 Aug 2015 11:45:10 -0400 Subject: [PATCH 6/6] Repair a docstring --- vcr/errors.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/vcr/errors.py b/vcr/errors.py index 1b40f91..bdc9701 100644 --- a/vcr/errors.py +++ b/vcr/errors.py @@ -3,8 +3,5 @@ class CannotOverwriteExistingCassetteException(Exception): class UnhandledHTTPRequestError(KeyError): - ''' - Raised when a cassette does not c - ontain the request we want - ''' + """Raised when a cassette does not contain the request we want.""" pass