<s> import copy <EOL> import inspect <EOL> import os <EOL> from six . moves import http_client as httplib <EOL> import pytest <EOL> import yaml <EOL> from vcr . compat import mock , contextlib <EOL> from vcr . cassette import Cassette <EOL> from vcr . errors import UnhandledHTTPRequestError <EOL> from vcr . patch import force_reset <EOL> from vcr . stubs import VCRHTTPSConnection <EOL> def test_cassette_load ( tmpdir ) : <EOL> a_file = tmpdir . join ( '<STR_LIT>' ) <EOL> a_file . write ( yaml . dump ( { '<STR_LIT>' : [ <EOL> { '<STR_LIT>' : { '<STR_LIT:body>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:foo>' , '<STR_LIT>' : '<STR_LIT:GET>' , '<STR_LIT>' : { } } , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' } <EOL> ] } ) ) <EOL> a_cassette = Cassette . load ( path = str ( a_file ) ) <EOL> assert len ( a_cassette ) == <NUM_LIT:1> <EOL> def test_cassette_not_played ( ) : <EOL> a = Cassette ( '<STR_LIT:test>' ) <EOL> assert not a . play_count <EOL> def test_cassette_append ( ) : <EOL> a = Cassette ( '<STR_LIT:test>' ) <EOL> a . append ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> assert a . requests == [ '<STR_LIT:foo>' ] <EOL> assert a . responses == [ '<STR_LIT:bar>' ] <EOL> def test_cassette_len ( ) : <EOL> a = Cassette ( '<STR_LIT:test>' ) <EOL> a . append ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> a . append ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> assert len ( a ) == <NUM_LIT:2> <EOL> def _mock_requests_match ( request1 , request2 , matchers ) : <EOL> return request1 == request2 <EOL> @ mock . patch ( '<STR_LIT>' , _mock_requests_match ) <EOL> def test_cassette_contains ( ) : <EOL> a = Cassette ( '<STR_LIT:test>' ) <EOL> a . append ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> assert '<STR_LIT:foo>' in a <EOL> @ mock . patch ( '<STR_LIT>' , _mock_requests_match ) <EOL> def test_cassette_responses_of ( ) : <EOL> a = Cassette ( '<STR_LIT:test>' ) <EOL> a . append ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> assert a . responses_of ( '<STR_LIT:foo>' ) == [ '<STR_LIT:bar>' ] <EOL> @ mock . patch ( '<STR_LIT>' , _mock_requests_match ) <EOL> def test_cassette_get_missing_response ( ) : <EOL> a = Cassette ( '<STR_LIT:test>' ) <EOL> with pytest . raises ( UnhandledHTTPRequestError ) : <EOL> a . responses_of ( '<STR_LIT:foo>' ) <EOL> @ mock . patch ( '<STR_LIT>' , _mock_requests_match ) <EOL> def test_cassette_cant_read_same_request_twice ( ) : <EOL> a = Cassette ( '<STR_LIT:test>' ) <EOL> a . append ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> a . play_response ( '<STR_LIT:foo>' ) <EOL> with pytest . raises ( UnhandledHTTPRequestError ) : <EOL> a . play_response ( '<STR_LIT:foo>' ) <EOL> def make_get_request ( ) : <EOL> conn = httplib . HTTPConnection ( "<STR_LIT>" ) <EOL> conn . request ( "<STR_LIT:GET>" , "<STR_LIT>" ) <EOL> return conn . getresponse ( ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , lambda * args , ** kwargs : ( ( '<STR_LIT:foo>' , ) , ( mock . MagicMock ( ) , ) ) ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_function_decorated_with_use_cassette_can_be_invoked_multiple_times ( * args ) : <EOL> decorated_function = Cassette . use ( path = '<STR_LIT:test>' ) ( make_get_request ) <EOL> for i in range ( <NUM_LIT:4> ) : <EOL> decorated_function ( ) <EOL> def test_arg_getter_functionality ( ) : <EOL> arg_getter = mock . Mock ( return_value = { '<STR_LIT:path>' : '<STR_LIT:test>' } ) <EOL> context_decorator = Cassette . use_arg_getter ( arg_getter ) <EOL> with context_decorator as cassette : <EOL> assert cassette . _path == '<STR_LIT:test>' <EOL> arg_getter . return_value = { '<STR_LIT:path>' : '<STR_LIT>' } <EOL> with context_decorator as cassette : <EOL> assert cassette . _path == '<STR_LIT>' <EOL> arg_getter . return_value = { '<STR_LIT:path>' : '<STR_LIT>' , '<STR_LIT>' : ( '<STR_LIT>' , ) } <EOL> @ context_decorator <EOL> def function ( ) : <EOL> pass <EOL> with mock . patch . object ( <EOL> Cassette , '<STR_LIT>' , <EOL> return_value = mock . MagicMock ( inject = False ) <EOL> ) as cassette_load : <EOL> function ( ) <EOL> cassette_load . assert_called_once_with ( ** arg_getter . return_value ) <EOL> def test_cassette_not_all_played ( ) : <EOL> a = Cassette ( '<STR_LIT:test>' ) <EOL> a . append ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> assert not a . all_played <EOL> @ mock . patch ( '<STR_LIT>' , _mock_requests_match ) <EOL> def test_cassette_all_played ( ) : <EOL> a = Cassette ( '<STR_LIT:test>' ) <EOL> a . append ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) <EOL> a . play_response ( '<STR_LIT:foo>' ) <EOL> assert a . all_played <EOL> def test_before_record_response ( ) : <EOL> before_record_response = mock . Mock ( return_value = '<STR_LIT>' ) <EOL> cassette = Cassette ( '<STR_LIT:test>' , before_record_response = before_record_response ) <EOL> cassette . append ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> before_record_response . assert_called_once_with ( '<STR_LIT>' ) <EOL> assert cassette . responses [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> def assert_get_response_body_is ( value ) : <EOL> conn = httplib . HTTPConnection ( "<STR_LIT>" ) <EOL> conn . request ( "<STR_LIT:GET>" , "<STR_LIT>" ) <EOL> assert conn . getresponse ( ) . read ( ) . decode ( '<STR_LIT:utf8>' ) == value <EOL> @ mock . patch ( '<STR_LIT>' , _mock_requests_match ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = True ) <EOL> @ mock . patch ( '<STR_LIT>' , return_value = True ) <EOL> def test_nesting_cassette_context_managers ( * args ) : <EOL> first_response = { '<STR_LIT:body>' : { '<STR_LIT:string>' : b'<STR_LIT>' } , '<STR_LIT>' : { } , <EOL> '<STR_LIT:status>' : { '<STR_LIT:message>' : '<STR_LIT:m>' , '<STR_LIT:code>' : <NUM_LIT:200> } } <EOL> second_response = copy . deepcopy ( first_response ) <EOL> second_response [ '<STR_LIT:body>' ] [ '<STR_LIT:string>' ] = b'<STR_LIT>' <EOL> with contextlib . ExitStack ( ) as exit_stack : <EOL> first_cassette = exit_stack . enter_context ( Cassette . use ( path = '<STR_LIT:test>' ) ) <EOL> exit_stack . enter_context ( mock . patch . object ( first_cassette , '<STR_LIT>' , <EOL> return_value = first_response ) ) <EOL> assert_get_response_body_is ( '<STR_LIT>' ) <EOL> with Cassette . use ( path = '<STR_LIT:test>' ) as second_cassette : <EOL> with mock . patch . object ( second_cassette , '<STR_LIT>' , return_value = second_response ) : <EOL> assert_get_response_body_is ( '<STR_LIT>' ) <EOL> assert_get_response_body_is ( '<STR_LIT>' ) <EOL> def test_nesting_context_managers_by_checking_references_of_http_connection ( ) : <EOL> original = httplib . HTTPConnection <EOL> with Cassette . use ( path = '<STR_LIT:test>' ) : <EOL> first_cassette_HTTPConnection = httplib . HTTPConnection <EOL> with Cassette . use ( path = '<STR_LIT:test>' ) : <EOL> second_cassette_HTTPConnection = httplib . HTTPConnection <EOL> assert second_cassette_HTTPConnection is not first_cassette_HTTPConnection <EOL> with Cassette . use ( path = '<STR_LIT:test>' ) : <EOL> assert httplib . HTTPConnection is not second_cassette_HTTPConnection <EOL> with force_reset ( ) : <EOL> assert httplib . HTTPConnection is original <EOL> assert httplib . HTTPConnection is second_cassette_HTTPConnection <EOL> assert httplib . HTTPConnection is first_cassette_HTTPConnection <EOL> def test_custom_patchers ( ) : <EOL> class Test ( object ) : <EOL> attribute = None <EOL> with Cassette . use ( path = '<STR_LIT>' , <EOL> custom_patches = ( ( Test , '<STR_LIT>' , VCRHTTPSConnection ) , ) ) : <EOL> assert issubclass ( Test . attribute , VCRHTTPSConnection ) <EOL> assert VCRHTTPSConnection is not Test . attribute <EOL> old_attribute = Test . attribute <EOL> with Cassette . use ( path = '<STR_LIT>' , <EOL> custom_patches = ( ( Test , '<STR_LIT>' , VCRHTTPSConnection ) , ) ) : <EOL> assert issubclass ( Test . attribute , VCRHTTPSConnection ) <EOL> assert VCRHTTPSConnection is not Test . attribute <EOL> assert Test . attribute is not old_attribute <EOL> assert issubclass ( Test . attribute , VCRHTTPSConnection ) <EOL> assert VCRHTTPSConnection is not Test . attribute <EOL> assert Test . attribute is old_attribute <EOL> def test_decorated_functions_are_reentrant ( ) : <EOL> info = { "<STR_LIT>" : False } <EOL> original_conn = httplib . HTTPConnection <EOL> @ Cassette . use ( path = '<STR_LIT>' , inject = True ) <EOL> def test_function ( cassette ) : <EOL> if info [ '<STR_LIT>' ] : <EOL> assert httplib . HTTPConnection is not info [ '<STR_LIT>' ] <EOL> else : <EOL> info [ '<STR_LIT>' ] = httplib . HTTPConnection <EOL> info [ '<STR_LIT>' ] = True <EOL> test_function ( ) <EOL> assert httplib . HTTPConnection is info [ '<STR_LIT>' ] <EOL> test_function ( ) <EOL> assert httplib . HTTPConnection is original_conn <EOL> def test_cassette_use_called_without_path_uses_function_to_generate_path ( ) : <EOL> @ Cassette . use ( inject = True ) <EOL> def function_name ( cassette ) : <EOL> assert cassette . _path == '<STR_LIT>' <EOL> function_name ( ) <EOL> def test_path_transformer_with_function_path ( ) : <EOL> def path_transformer ( path ) : <EOL> return os . path . join ( '<STR_LIT:a>' , path ) <EOL> @ Cassette . use ( inject = True , path_transformer = path_transformer ) <EOL> def function_name ( cassette ) : <EOL> assert cassette . _path == os . path . join ( '<STR_LIT:a>' , '<STR_LIT>' ) <EOL> function_name ( ) <EOL> def test_path_transformer_with_context_manager ( ) : <EOL> with Cassette . use ( <EOL> path = '<STR_LIT:b>' , path_transformer = lambda * args : '<STR_LIT:a>' <EOL> ) as cassette : <EOL> assert cassette . _path == '<STR_LIT:a>' <EOL> def test_path_transformer_None ( ) : <EOL> with Cassette . use ( <EOL> path = '<STR_LIT:a>' , path_transformer = None , <EOL> ) as cassette : <EOL> assert cassette . _path == '<STR_LIT:a>' <EOL> def test_func_path_generator ( ) : <EOL> def generator ( function ) : <EOL> return os . path . join ( os . path . dirname ( inspect . getfile ( function ) ) , <EOL> function . __name__ ) <EOL> @ Cassette . use ( inject = True , func_path_generator = generator ) <EOL> def function_name ( cassette ) : <EOL> assert cassette . _path == os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> function_name ( ) <EOL> def test_use_as_decorator_on_coroutine ( ) : <EOL> original_http_connetion = httplib . HTTPConnection <EOL> @ Cassette . use ( inject = True ) <EOL> def test_function ( cassette ) : <EOL> assert httplib . HTTPConnection . cassette is cassette <EOL> assert httplib . HTTPConnection is not original_http_connetion <EOL> value = yield <NUM_LIT:1> <EOL> assert value == <NUM_LIT:1> <EOL> assert httplib . HTTPConnection . cassette is cassette <EOL> assert httplib . HTTPConnection is not original_http_connetion <EOL> value = yield <NUM_LIT:2> <EOL> assert value == <NUM_LIT:2> <EOL> coroutine = test_function ( ) <EOL> value = next ( coroutine ) <EOL> while True : <EOL> try : <EOL> value = coroutine . send ( value ) <EOL> except StopIteration : <EOL> break <EOL> def test_use_as_decorator_on_generator ( ) : <EOL> original_http_connetion = httplib . HTTPConnection <EOL> @ Cassette . use ( inject = True ) <EOL> def test_function ( cassette ) : <EOL> assert httplib . HTTPConnection . cassette is cassette <EOL> assert httplib . HTTPConnection is not original_http_connetion <EOL> yield <NUM_LIT:1> <EOL> assert httplib . HTTPConnection . cassette is cassette <EOL> assert httplib . HTTPConnection is not original_http_connetion <EOL> yield <NUM_LIT:2> <EOL> assert list ( test_function ( ) ) == [ <NUM_LIT:1> , <NUM_LIT:2> ] </s>
