def make_vcr(pytestconfig, cassette_library_dir): mode = pytestconfig.getoption('--vcr-mode') api_key = pytestconfig.getoption('--whispir-api-key') username = pytestconfig.getoption('--whispir-username') gcm_api_key = pytestconfig.getoption('--whispir-gcm-api-key') scrubber = scrub_patterns( ((api_key, TEST_API_KEY), (username, TEST_USERNAME), (gcm_api_key, TEST_GCM_API_KEY))) options = { 'record_mode': mode, 'filter_headers': [('authorization', replace_auth), ('set-cookie', None), ('cookie', None), ('User-Agent', None), ('x-api-key', TEST_API_KEY)], 'before_record_response': scrubber, 'before_record_request': scrubber, 'path_transformer': VCR.ensure_suffix('.yaml'), 'decode_compressed_response': True, 'cassette_library_dir': cassette_library_dir, 'match_on': ('method', 'scheme', 'host', 'port', 'path', 'query', 'headers'), 'serializer': 'pretty-yaml' } vcr = VCR(**options) vcr.register_serializer('pretty-yaml', prettyserializer) return vcr
# Remove request headers i['request']['headers'] = {} # Filter some unimportant response headers response_headers = i['response']['headers'] response_headers.pop('connection', None) response_headers.pop('date', None) response_headers.pop('server', None) filter_x_headers(response_headers) return yaml.dump(cassette_dict, default_flow_style=None, Dumper=yaml.Dumper) @staticmethod def deserialize(cassette_str): return yaml.load(cassette_str, Loader=yaml.Loader) vcr = VCR( cassette_library_dir=FIXTURES_ROOT, record_mode=os.environ.get('VCR', 'once'), match_on=['method', 'scheme', 'host', 'path', 'query'], ) vcr.register_serializer('custom', CustomSerializer) def use_cassette(name): return vcr.use_cassette( '{}.yml'.format(name), serializer='custom', )
f"{json.dumps(serialize_dict(cassette_dict), sort_keys=True, indent=2)}\n" ) @staticmethod def deserialize(cassette_string): return json.loads(cassette_string) vcr = VCR( before_record_response=filter_access_token, cassette_library_dir="tests/integration/cassettes", match_on=["uri", "method"], path_transformer=VCR.ensure_suffix(".json"), serializer="custom_serializer", ) vcr.register_serializer("custom_serializer", CustomSerializer) vcr.register_persister(CustomPersister) def after_init(func, *args): func(*args) def add_init_hook(original_init): """Wrap an __init__ method to also call some hooks.""" @wraps(original_init) def wrapper(self, *args, **kwargs): original_init(self, *args, **kwargs) after_init(init_hook, self)
def serialize(cassette_dict): for i in cassette_dict['interactions']: # Remove request headers i['request']['headers'] = {} # Filter some unimportant response headers response_headers = i['response']['headers'] response_headers.pop('connection', None) response_headers.pop('date', None) response_headers.pop('server', None) filter_x_headers(response_headers) return yamlserializer.serialize(cassette_dict) @staticmethod def deserialize(cassette_str): return yamlserializer.deserialize(cassette_str) vcr = VCR( cassette_library_dir=FIXTURES_ROOT, record_mode='once', match_on=['url', 'method'], ) vcr.register_serializer('custom', CustomSerializer) def use_cassette(name): return vcr.use_cassette( '{}.yml'.format(name), serializer='custom', )
class CustomVCR(VCR): """Derived from VCR to make setting paths easier.""" def use_cassette(self, path="", **kwargs): """Use a cassette.""" path += ".json" return super().use_cassette(path, **kwargs) VCR = CustomVCR( serializer="custom_serializer", cassette_library_dir="tests/integration/cassettes", match_on=["uri", "method"], before_record_response=filter_access_token, ) VCR.register_serializer("custom_serializer", CustomSerializer) VCR.register_persister(CustomPersister) def after_init(func, *args): func(*args) def add_init_hook(original_init): """Wrap an __init__ method to also call some hooks.""" @wraps(original_init) def wrapper(self, *args, **kwargs): original_init(self, *args, **kwargs) after_init(init_hook, self) return wrapper
""" body = request.body if helpers.PY3: body = request.body.decode('utf-8') payload = json.loads(body) payload['SignRequest']['OptionalInputs']['ClaimedIdentity']['Name'] = 'X:Y' request.body = json.dumps(payload) return request my_vcr = VCR( serializer='json', record_mode='once', cassette_library_dir=join(dirname(__file__), 'cassettes'), path_transformer=VCR.ensure_suffix('.json'), before_record=before_record_callback ) if helpers.PY3: my_vcr.register_serializer('json', JSONSerializer) def fixture_path(filename): """Build the full path of a fixture file.""" return join(dirname(__file__), 'fixtures', filename) class BaseCase(unittest.TestCase): pass
def before_record_callback(request): """Replace confidential information in the recorded cassettes. - customer:key are replaced with 'X:Y' """ body = request.body.decode('utf-8') payload = json.loads(body) payload['SignRequest']['OptionalInputs']['ClaimedIdentity']['Name'] = 'X:Y' request.body = json.dumps(payload) return request my_vcr = VCR(serializer='json', record_mode='once', cassette_library_dir=join(dirname(__file__), 'cassettes'), path_transformer=VCR.ensure_suffix('.json'), before_record=before_record_callback) my_vcr.register_serializer('json', JSONSerializer) def fixture_path(filename): """Build the full path of a fixture file.""" return join(dirname(__file__), 'fixtures', filename) class BaseCase(unittest.TestCase): pass