]> khamidou — GitHub mirrors - vcrpy.git/commitdiff
fixes before_record_response mutates response
authorkg <kyle@easypost.com>
Wed, 9 May 2018 20:44:28 +0000 (20:44 +0000)
committerkg <kyle@easypost.com>
Thu, 24 May 2018 01:12:13 +0000 (01:12 +0000)
When no cassette exists, it's expected that the response returned, should be
the original, unchanged response. The response recorded in the cassette should be
that which is returned by the before_record_response callback.

But on subsequent requests/responses (when a cassette exists), the responses
returned should be exactly what is in the cassette.

resolves #355

tests/integration/test_stubs.py
vcr/cassette.py

index 23bf8ecae887070d195df2c967dcc50a40d60f8a..cbbd0451740ea87eb6121c52740e65954d0fb522 100644 (file)
@@ -1,5 +1,6 @@
 import vcr
 import zlib
+import json
 import six.moves.http_client as httplib
 
 from assertions import assert_is_json
@@ -83,3 +84,50 @@ def test_original_decoded_response_is_not_modified(tmpdir, httpbin):
 
         assert 'content-encoding' not in inside.headers
         assert_is_json(inside.read())
+
+
+def _make_before_record_response(fields, replacement='[REDACTED]'):
+    def before_record_response(response):
+        string_body = response['body']['string'].decode('utf8')
+        body = json.loads(string_body)
+
+        for field in fields:
+            if field in body:
+                body[field] = replacement
+
+        response['body']['string'] = json.dumps(body).encode()
+        return response
+    return before_record_response
+
+
+def test_original_response_is_not_modified_by_before_filter(tmpdir, httpbin):
+    testfile = str(tmpdir.join('sensitive_data_scrubbed_response.yml'))
+    host, port = httpbin.host, httpbin.port
+    field_to_scrub = 'url'
+    replacement = '[YOU_CANT_HAVE_THE_MANGO]'
+
+    conn = httplib.HTTPConnection(host, port)
+    conn.request('GET', '/get')
+    outside = conn.getresponse()
+
+    callback = _make_before_record_response([field_to_scrub], replacement)
+    with vcr.use_cassette(testfile, before_record_response=callback):
+        conn = httplib.HTTPConnection(host, port)
+        conn.request('GET', '/get')
+        inside = conn.getresponse()
+
+        # The scrubbed field should be the same, because no cassette existed.
+        # Furthermore, the responses should be identical.
+        inside_body = json.loads(inside.read().decode('utf-8'))
+        outside_body = json.loads(outside.read().decode('utf-8'))
+        assert not inside_body[field_to_scrub] == replacement
+        assert inside_body[field_to_scrub] == outside_body[field_to_scrub]
+
+    # Ensure that when a cassette exists, the scrubbed response is returned.
+    with vcr.use_cassette(testfile, before_record_response=callback):
+        conn = httplib.HTTPConnection(host, port)
+        conn.request('GET', '/get')
+        inside = conn.getresponse()
+
+        inside_body = json.loads(inside.read().decode('utf-8'))
+        assert inside_body[field_to_scrub] == replacement
index e22bbea605e2ae9e414c6193f1c02099501a09d8..d64dec67a55e578b968567f566176ede2b633e9c 100644 (file)
@@ -1,4 +1,5 @@
 import collections
+import copy
 import sys
 import inspect
 import logging
@@ -222,6 +223,9 @@ class Cassette(object):
         request = self._before_record_request(request)
         if not request:
             return
+        # Deepcopy is here because mutation of `response` will corrupt the
+        # real response.
+        response = copy.deepcopy(response)
         response = self._before_record_response(response)
         if response is None:
             return