def vcr(request): def auth_matcher(r1, r2): return (r1.headers.get('authorization') == r2.headers.get('authorization')) def uri_with_query_matcher(r1, r2): "URI matcher that allows query params to appear in any order" p1, p2 = urlparse(r1.uri), urlparse(r2.uri) return (p1[:3] == p2[:3] and parse_qs(p1.query, True) == parse_qs(p2.query, True)) # Use `none` to use the recorded requests, and `once` to delete existing # cassettes and re-record. record_mode = request.config.option.record_mode assert record_mode in ('once', 'none') cassette_dir = os.path.join(os.path.dirname(__file__), 'cassettes') if not os.path.exists(cassette_dir): os.makedirs(cassette_dir) # https://github.com/kevin1024/vcrpy/pull/196 vcr = VCR( record_mode=request.config.option.record_mode, filter_headers=[('Authorization', '**********')], filter_post_data_parameters=[('refresh_token', '**********')], match_on=['method', 'uri_with_query', 'auth', 'body'], cassette_library_dir=cassette_dir) vcr.register_matcher('auth', auth_matcher) vcr.register_matcher('uri_with_query', uri_with_query_matcher) return vcr
def initialize_vcr(): def auth_matcher(r1, r2): return (r1.headers.get('authorization') == r2.headers.get('authorization')) def uri_with_query_matcher(r1, r2): p1, p2 = urlparse(r1.uri), urlparse(r2.uri) return (p1[:3] == p2[:3] and parse_qs(p1.query, True) == parse_qs(p2.query, True)) cassette_dir = os.path.join(os.path.dirname(__file__), 'cassettes') if not os.path.exists(cassette_dir): os.makedirs(cassette_dir) filename = os.path.join(cassette_dir, 'demo_theme.yaml') if os.path.exists(filename): record_mode = 'none' else: record_mode = 'once' vcr = VCR( record_mode=record_mode, filter_headers=[('Authorization', '**********')], filter_post_data_parameters=[('refresh_token', '**********')], match_on=['method', 'uri_with_query', 'auth', 'body'], cassette_library_dir=cassette_dir) vcr.register_matcher('auth', auth_matcher) vcr.register_matcher('uri_with_query', uri_with_query_matcher) return vcr
def vcr(request): def auth_matcher(r1, r2): return ( r1.headers.get('authorization') == r2.headers.get('authorization')) def uri_with_query_matcher(r1, r2): "URI matcher that allows query params to appear in any order" p1, p2 = urlparse(r1.uri), urlparse(r2.uri) return (p1[:3] == p2[:3] and parse_qs(p1.query, True) == parse_qs(p2.query, True)) # Use `none` to use the recorded requests, and `once` to delete existing # cassettes and re-record. record_mode = request.config.option.record_mode assert record_mode in ('once', 'none') cassette_dir = os.path.join(os.path.dirname(__file__), 'cassettes') if not os.path.exists(cassette_dir): os.makedirs(cassette_dir) # https://github.com/kevin1024/vcrpy/pull/196 vcr = VCR(record_mode=request.config.option.record_mode, filter_headers=[('Authorization', '**********')], filter_post_data_parameters=[('refresh_token', '**********')], match_on=['method', 'uri_with_query', 'auth', 'body'], cassette_library_dir=cassette_dir) vcr.register_matcher('auth', auth_matcher) vcr.register_matcher('uri_with_query', uri_with_query_matcher) return vcr
def http_recorder(vcr_mode, cassette_dir): """Creates a VCR object in vcr_mode mode. Args: vcr_mode (string): the parameter for record_mode. cassette_dir (string): path to the cassettes. Returns: VCR: a VCR object. """ my_vcr = VCR(cassette_library_dir=cassette_dir, record_mode=vcr_mode, match_on=[ 'method', 'scheme', 'host', 'port', 'path', 'unordered_query' ], filter_headers=['x-qx-client-application', 'User-Agent'], filter_query_parameters=[('access_token', 'dummyapiusersloginWithTokenid01')], filter_post_data_parameters=[('apiToken', 'apiToken_dummy')], decode_compressed_response=True, before_record_response=_purge_headers_cb([ 'Date', ('Set-Cookie', 'dummy_cookie'), 'X-Global-Transaction-ID', 'Etag', 'Content-Security-Policy', 'X-Content-Security-Policy', 'X-Webkit-Csp', 'content-length' ])) my_vcr.register_matcher('unordered_query', _unordered_query_matcher) my_vcr.register_persister(IdRemoverPersister) return my_vcr
def vcr(request): def auth_matcher(r1, r2): return (r1.headers.get('authorization') == \ r2.headers.get('authorization')) def uri_with_query_matcher(r1, r2): "URI matcher that allows query params to appear in any order" p1, p2 = urlparse(r1.uri), urlparse(r2.uri) return (p1[:3] == p2[:3] and \ parse_qs(p1.query, True) == parse_qs(p2.query, True)) # Use `none` to use the recorded requests, and `once` to delete existing # cassettes and re-record. record_mode = request.config.option.record_mode assert record_mode in ('once', 'none') cassette_dir = os.path.join(os.path.dirname(__file__), 'cassettes') if not os.path.exists(cassette_dir): os.makedirs(cassette_dir) def scrub(tokens, replacement=''): def before_record_response(response): dikt = json.loads(response['body']['string']) for token in tokens: dikt[token] = replacement response['body']['string'] = json.dumps(dikt) return response return before_record_response # https://github.com/kevin1024/vcrpy/pull/196 vcr = VCR(record_mode=request.config.option.record_mode, filter_headers=[('Authorization', '**********')], filter_post_data_parameters=[('refresh_token', '**********'), ('code', '**********')], match_on=['method', 'uri_with_query', 'auth', 'body'], before_record_response=scrub(['access_token', 'refresh_token'], '**********'), cassette_library_dir=cassette_dir) vcr.register_matcher('auth', auth_matcher) vcr.register_matcher('uri_with_query', uri_with_query_matcher) return vcr