def test_base_service_get_interactions_for_active_scenario(service: BaseService, scenarios_dir): expected_request_1 = MITMRequest( headers=MITMHeaders({ 'Accept': ['application/json'], 'Accept-Encoding': ['gzip, deflate'], 'Connection': ['keep-alive'], 'User-Agent': ['python-requests/2.18.4'] }), method='GET', url='https://host_a.local/api', ) expected_response_1 = MITMResponse( body='{"value": "response1"}', headers=MITMHeaders({ 'content-type': ['application/json; charset=UTF-8'] }), status_code=200, ) expected_request_2 = MITMRequest( body='{"value": "response2"}', headers=MITMHeaders({ 'Accept': ['application/json'], 'Accept-Encoding': ['gzip, deflate'], 'Connection': ['keep-alive'], 'User-Agent': ['python-requests/2.18.4'] }), method='POST', url='https://host_a.local/api', ) expected_response_2 = MITMResponse( headers=MITMHeaders({ 'content-type': ['application/json; charset=UTF-8'] }), status_code=201, ) interactions = service.get_interactions_for_active_scenario() assert len(interactions) == 2 assert interactions[0].request == expected_request_1 assert interactions[0].response == expected_response_1 assert interactions[1].request == expected_request_2 assert interactions[1].response == expected_response_2
def test_interaction_next_in_dir( tmpdir, interaction_dir_files: List[str], expected_next_interaction_name: str, ): interaction_dir = tmpdir.mkdir('interactions') for file_name in interaction_dir_files: interaction_dir.join(file_name).ensure() interaction_dir_path = Path(interaction_dir.strpath) result = Interaction.next_in_dir(directory=interaction_dir_path, request=MITMRequest( url='http://test.local/', method='GET'), response=MITMResponse(status_code=204)) expected_request = MITMRequest(url='http://test.local/', method='GET') expected_response = MITMResponse(status_code=204) assert expected_next_interaction_name == result.name assert expected_request == result.request assert expected_response == result.response
def test_management_service_build_response_empty_object_body(management_service): with patch( 'inspire_mitmproxy.services.management_service.get_current_version', return_value='0.0.1', ): result = management_service.build_response(200, json_message={}) expected = MITMResponse( body='{}', headers=MITMHeaders({ 'Content-Type': ['application/json; encoding=UTF-8'], 'Server': ['inspire-mitmproxy/0.0.1'], }), status_code=200, ) assert expected == result
def process_request(self, request: dict): return MITMResponse(body=message, headers=MITMHeaders({ 'Content-Type': ['text/plain'], }))
@mark.parametrize( 'request_, response', [ ( MITMRequest( headers=MITMHeaders({ 'Accept': ['application/json'], }), method='GET', url='https://host_a.local/api', ), MITMResponse( body='{"value": "response1"}', headers=MITMHeaders({ 'content-type': ['application/json; charset=UTF-8'], }), status_code=200, ), ), ( MITMRequest( body='{"value": "response2"}', headers=MITMHeaders({ 'Accept': ['application/json'], }), method='POST', url='https://host_a.local/api', ), MITMResponse( headers=MITMHeaders({
def test_base_service_process_response_record_when_dir_exists_and_has_some_interactions_already( dispatcher: Dispatcher, temporary_scenarios_dir, ): expected_replayed_response = b'test_scenario_record_not_empty/TestServiceA/0' expected_recording = { 'request': { 'url': 'https://host_a.local/recordme', 'method': 'GET', 'body': '', 'headers': { 'Accept': ['text/plain'], 'Host': ['host_a.local'], }, }, 'response': { 'body': 'Hello, world!', 'headers': {}, 'status': { 'code': 200, 'message': 'OK', }, }, 'match': {}, 'callbacks': [], 'max_replays': -1, } request_set_scenario = MITMRequest( method='POST', url='http://mitm-manager.local/config', body='{"active_scenario": "test_scenario_record_not_empty"}', headers=MITMHeaders({ 'Host': ['mitm-manager.local'], 'Accept': ['application/json'], })) request_enable_recording = MITMRequest( method='POST', url='http://mitm-manager.local/record', body='{"enable": true}', headers=MITMHeaders({ 'Host': ['mitm-manager.local'], 'Accept': ['application/json'], })) request_to_be_replayed = MITMRequest(method='GET', url='https://host_a.local/replayme', headers=MITMHeaders({ 'Host': ['host_a.local'], 'Accept': ['text/plain'], })) request_to_be_recorded = MITMRequest(method='GET', url='https://host_a.local/recordme', headers=MITMHeaders({ 'Host': ['host_a.local'], 'Accept': ['text/plain'], })) response_to_be_recorded = MITMResponse(status_code=200, body='Hello, world!') response_set_scenario = dispatcher.process_request(request_set_scenario) assert response_set_scenario.status_code == 201 response_enable_recording = dispatcher.process_request( request_enable_recording) assert response_enable_recording.status_code == 201 response_to_be_replayed = dispatcher.process_request( request_to_be_replayed) assert expected_replayed_response == response_to_be_replayed.body with raises(DoNotIntercept): dispatcher.process_request(request_to_be_recorded) dispatcher.process_response(request_to_be_recorded, response_to_be_recorded) service_interactions_dir = temporary_scenarios_dir.join( 'test_scenario_record_not_empty') assert service_interactions_dir.exists() assert len(service_interactions_dir.listdir()) == 1 assert len(service_interactions_dir.join('TestServiceA').listdir()) == 2 out_file = service_interactions_dir.join('TestServiceA').join( 'interaction_1.yaml').read() result_recording = yaml_load(out_file) assert expected_recording == result_recording
from pytest import fixture, mark from inspire_mitmproxy.http import MITMHeaders, MITMRequest, MITMResponse from inspire_mitmproxy.interaction import Interaction TEST_REQUEST = MITMRequest( body=None, headers=MITMHeaders({'Host': ['test.local']}), method='GET', url='https://test.local/path', ) TEST_RESPONSE = MITMResponse( body='{"key": "value"}', headers=MITMHeaders({'Content-Type': ['application/json; charset=UTF-8']}), status_code=200, ) def _get_headers(good=True): if good: return MITMHeaders({}) else: return MITMHeaders({'X-Wrong-Header': ["Headers don't matter"]}) def _test_match_request_data_generate_requests(test_data): params = [] ids = []
def test_dict_responses_from_bytes_and_str_equal(): result_from_string = MITMResponse.from_dict(TEST_DICT_RESPONSE) result_from_bytes = MITMResponse.from_dict( TEST_DICT_RESPONSE_WITH_BYTES_BODY) assert result_from_string == result_from_bytes
def test_response_from_dict_gzip(): result = MITMResponse.from_dict(TEST_DICT_RESPONSE_GZIP) expected = TEST_RESPONSE_GZIP assert result == expected
def test_response_from_dict(): result = MITMResponse.from_dict(TEST_DICT_RESPONSE) expected = TEST_RESPONSE assert result == expected
def test_response_from_mitmproxy_gzip(): result = MITMResponse.from_mitmproxy(TEST_MITM_RESPONSE_GZIP) expected = TEST_RESPONSE_GZIP assert result == expected
def test_response_from_mitmproxy(): result = MITMResponse.from_mitmproxy(TEST_MITM_RESPONSE) expected = TEST_RESPONSE assert result == expected
headers=Headers(fields=[ (b'Content-Type', b'text/plain; charset=UTF-8'), (b'Date', b'Wed, 21 Mar 2018 12:47:18 GMT'), (b'Server', b'nginx/1.12.2'), (b'Content-Encoding', b'gzip'), ]), # gzipped utf-8-encoded "Hello, world!" content=b'x\x9c\xf3H\xcd\xc9\xc9\xd7Q(\xcf/\xcaIQ\x04\x00 ^\x04\x8a', ) TEST_RESPONSE = MITMResponse( status_code=200, status_message='OK', body="Witaj, świecie!", headers=MITMHeaders({ 'Content-Type': ['text/plain; charset=ISO-8859-2'], 'Date': ['Wed, 21 Mar 2018 12:47:18 GMT'], 'Server': ['nginx/1.12.2'], }), original_encoding='ISO-8859-2', http_version='HTTP/1.1', ) TEST_RESPONSE_WITH_BYTES_BODY = MITMResponse( status_code=200, status_message='OK', body=b"Witaj, \xb6wiecie!", headers=MITMHeaders({ 'Content-Type': ['text/plain; charset=ISO-8859-2'], 'Date': ['Wed, 21 Mar 2018 12:47:18 GMT'], 'Server': ['nginx/1.12.2'], }),
headers=Headers( fields=[ (b'Content-Type', b'text/plain; charset=ISO-8859-2'), (b'Date', b'Wed, 21 Mar 2018 12:47:18 GMT'), (b'Server', b'nginx/1.12.2'), ] ), content=b'Witaj, \xb6wiecie!', ) TEST_RESPONSE = MITMResponse( status_code=200, status_message='OK', body="Witaj, świecie!", headers=MITMHeaders({ 'Content-Type': ['text/plain; charset=ISO-8859-2'], 'Date': ['Wed, 21 Mar 2018 12:47:18 GMT'], 'Server': ['nginx/1.12.2'], }), original_encoding='ISO-8859-2', http_version='HTTP/1.1', ) TEST_RESPONSE_WITH_BYTES_BODY = MITMResponse( status_code=200, status_message='OK', body=b"Witaj, \xb6wiecie!", headers=MITMHeaders({ 'Content-Type': ['text/plain; charset=ISO-8859-2'], 'Date': ['Wed, 21 Mar 2018 12:47:18 GMT'], 'Server': ['nginx/1.12.2'], }),