def _scrub_sensitive_response_info(self, response): if not TestMode.is_playback(self.test_mode): # We need to make a copy because vcr doesn't make one for us. # Without this, changing the contents of the dicts would change # the contents returned to the caller - not just the contents # getting saved to disk. That would be a problem with headers # such as 'location', often used in the request uri of a # subsequent service call. response = copy.deepcopy(response) # decode_response is supposed to do a copy, but do it bad # https://github.com/kevin1024/vcrpy/issues/264 response = decode_response(response) headers = response.get('headers') if headers: for name, val in headers.items(): for i, e in enumerate(val): val[i] = self._scrub(e) if name.lower() == 'retry-after': val[:] = ['0'] body = response.get('body') if body: body_str = body.get('string') if body_str: response['body']['string'] = self._scrub(body_str) return response
def test_decode_response_gzip(): body = b'gzip message' buf = BytesIO() f = gzip.GzipFile('a', fileobj=buf, mode='wb') f.write(body) f.close() compressed_body = buf.getvalue() buf.close() gzip_response = { 'body': {'string': compressed_body}, 'headers': { 'access-control-allow-credentials': ['true'], 'access-control-allow-origin': ['*'], 'connection': ['keep-alive'], 'content-encoding': ['gzip'], 'content-length': ['177'], 'content-type': ['application/json'], 'date': ['Wed, 02 Dec 2015 19:44:32 GMT'], 'server': ['nginx'] }, 'status': {'code': 200, 'message': 'OK'} } decoded_response = decode_response(gzip_response) assert decoded_response['body']['string'] == body assert decoded_response['headers']['content-length'] == [str(len(body))]
def test_decode_response_gzip(): body = b"gzip message" buf = BytesIO() f = gzip.GzipFile("a", fileobj=buf, mode="wb") f.write(body) f.close() compressed_body = buf.getvalue() buf.close() gzip_response = { "body": { "string": compressed_body }, "headers": { "access-control-allow-credentials": ["true"], "access-control-allow-origin": ["*"], "connection": ["keep-alive"], "content-encoding": ["gzip"], "content-length": ["177"], "content-type": ["application/json"], "date": ["Wed, 02 Dec 2015 19:44:32 GMT"], "server": ["nginx"], }, "status": { "code": 200, "message": "OK" }, } decoded_response = decode_response(gzip_response) assert decoded_response["body"]["string"] == body assert decoded_response["headers"]["content-length"] == [str(len(body))]
def _scrub_sensitive_response_info(self, response): if not TestMode.is_playback(self.test_mode): # We need to make a copy because vcr doesn't make one for us. # Without this, changing the contents of the dicts would change # the contents returned to the caller - not just the contents # getting saved to disk. That would be a problem with headers # such as 'location', often used in the request uri of a # subsequent service call. response = copy.deepcopy(response) # decode_response is supposed to do a copy, but do it bad # https://github.com/kevin1024/vcrpy/issues/264 response = decode_response(response) headers = response.get("headers") if headers: def internal_scrub(key, val): if key.lower() == "retry-after": return "0" return self._scrub(val) for name, val in headers.items(): if isinstance(val, list): for i, e in enumerate(val): val[i] = internal_scrub(name, e) else: headers[name] = internal_scrub(name, val) body = response.get("body") if body: body_str = body.get("string") if body_str: response["body"]["string"] = self._scrub(body_str) return response
def test_decode_response_gzip(): body = b'gzip message' buf = BytesIO() f = gzip.GzipFile('a', fileobj=buf, mode='wb') f.write(body) f.close() compressed_body = buf.getvalue() buf.close() gzip_response = { 'body': { 'string': compressed_body }, 'headers': { 'access-control-allow-credentials': ['true'], 'access-control-allow-origin': ['*'], 'connection': ['keep-alive'], 'content-encoding': ['gzip'], 'content-length': ['177'], 'content-type': ['application/json'], 'date': ['Wed, 02 Dec 2015 19:44:32 GMT'], 'server': ['nginx'] }, 'status': { 'code': 200, 'message': 'OK' } } decoded_response = decode_response(gzip_response) assert decoded_response['body']['string'] == body assert decoded_response['headers']['content-length'] == [str(len(body))]
def test_decode_response_uncompressed(): recorded_response = { "status": { "message": "OK", "code": 200 }, "headers": { "content-length": ["10806"], "date": ["Fri, 24 Oct 2014 18:35:37 GMT"], "content-type": ["text/html; charset=utf-8"], }, "body": { "string": b"" }, } assert decode_response(recorded_response) == recorded_response
def test_decode_response_uncompressed(): recorded_response = { "status": { "message": "OK", "code": 200 }, "headers": { "content-length": ["10806"], "date": ["Fri, 24 Oct 2014 18:35:37 GMT"], "content-type": ["text/html; charset=utf-8"], }, "body": { "string": b"" } } assert decode_response(recorded_response) == recorded_response
def test_decode_response_deflate(): body = b'deflate message' deflate_response = { 'body': {'string': zlib.compress(body)}, 'headers': { 'access-control-allow-credentials': ['true'], 'access-control-allow-origin': ['*'], 'connection': ['keep-alive'], 'content-encoding': ['deflate'], 'content-length': ['177'], 'content-type': ['application/json'], 'date': ['Wed, 02 Dec 2015 19:44:32 GMT'], 'server': ['nginx'] }, 'status': {'code': 200, 'message': 'OK'} } decoded_response = decode_response(deflate_response) assert decoded_response['body']['string'] == body assert decoded_response['headers']['content-length'] == [str(len(body))]
def test_decode_response_deflate(): body = b"deflate message" deflate_response = { "body": { "string": zlib.compress(body) }, "headers": { "access-control-allow-credentials": ["true"], "access-control-allow-origin": ["*"], "connection": ["keep-alive"], "content-encoding": ["deflate"], "content-length": ["177"], "content-type": ["application/json"], "date": ["Wed, 02 Dec 2015 19:44:32 GMT"], "server": ["nginx"], }, "status": { "code": 200, "message": "OK" }, } decoded_response = decode_response(deflate_response) assert decoded_response["body"]["string"] == body assert decoded_response["headers"]["content-length"] == [str(len(body))]
def test_decode_response_deflate(): body = b'deflate message' deflate_response = { 'body': { 'string': zlib.compress(body) }, 'headers': { 'access-control-allow-credentials': ['true'], 'access-control-allow-origin': ['*'], 'connection': ['keep-alive'], 'content-encoding': ['deflate'], 'content-length': ['177'], 'content-type': ['application/json'], 'date': ['Wed, 02 Dec 2015 19:44:32 GMT'], 'server': ['nginx'] }, 'status': { 'code': 200, 'message': 'OK' } } decoded_response = decode_response(deflate_response) assert decoded_response['body']['string'] == body assert decoded_response['headers']['content-length'] == [str(len(body))]
# https://github.com/kevin1024/vcrpy/issues/249 # http://stackoverflow.com/questions/36366234 # Thanks to Reti43 on StackOverflow import sys import yaml # decode_response doesn't exist in vcrpy 1.7.4 from pypi! # pip install from source instead from vcr.filters import decode_response if len(sys.argv) not in [2, 3]: print "Usage: decode_vcrpy.py path/to/fixture.yaml [response number, ex '0']" sys.exit(1) with open(sys.argv[1], 'r') as f: doc = yaml.load(f) if len(sys.argv) == 3: response = doc['interactions'][int(sys.argv[2])]['response'] print(decode_response(response)['body']['string']) sys.exit(0) for index, interaction in enumerate(doc['interactions']): print('################') print('response %d' % index) print('uri: %s' % interaction['request']['uri']) print('response:') decoded = decode_response(interaction['response']) print(decoded['body']['string'])