def test_whitelist_service_defaults(service_url):
    dispatcher = Dispatcher()
    with pytest.raises(DoNotIntercept):
        dispatcher.process_request(MITMRequest(
            method='GET',
            url=service_url,
        ))
def test_get_service_interactions_raises(dispatcher: Dispatcher):
    request_set_config = MITMRequest(
        method='POST',
        url='http://mitm-manager.local/config',
        body='{"active_scenario": "test_scenario_replays_ok"}',
        headers=MITMHeaders({
            'Host': ['mitm-manager.local'],
            'Accept': ['application/json'],
        }))

    request_inexistent_service_interactions = MITMRequest(
        method='GET',
        url='http://mitm-manager.local/service/InexistentService/interactions',
        headers=MITMHeaders({
            'Host': ['mitm-manager.local'],
            'Accept': ['application/json'],
        }))

    response_set_config = dispatcher.process_request(request_set_config)
    assert response_set_config.status_code == 201

    with raises(ServiceNotFound):
        dispatcher.process_request(request_inexistent_service_interactions)
Beispiel #3
0
def test_dispatcher_default_services():
    dispatcher = Dispatcher()
    result = dispatcher.process_request(
        MITMRequest(url='http://mitm-manager.local/services'))

    expected = {
        'services': [{
            "type": "ManagementService",
            "name": "ManagementService",
            "hosts_list": ["mitm-manager.local"]
        }, {
            "type": "BaseService",
            "name": "ArxivService",
            "hosts_list": ["arxiv.org", "export.arxiv.org"]
        }, {
            "type": "BaseService",
            "name": "LegacyService",
            "hosts_list": ["inspirehep.net"]
        }, {
            "type": "BaseService",
            "name": "RTService",
            "hosts_list": ["inspirevm13.cern.ch", "rt.inspirehep.net"]
        }, {
            "type":
            "WhitelistService",
            "name":
            "WhitelistService",
            "hosts_list": [
                "test-indexer",
                "test-scrapyd",
                "test-web-e2e.local",
            ]
        }]
    }
    json_response = json.loads(result.body)

    assert json_response == expected
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
def test_get_service_interactions(dispatcher: Dispatcher):
    request_set_config = MITMRequest(
        method='POST',
        url='http://mitm-manager.local/config',
        body='{"active_scenario": "test_scenario_replays_ok"}',
        headers=MITMHeaders({
            'Host': ['mitm-manager.local'],
            'Accept': ['application/json'],
        }))

    request_service_a = MITMRequest(method='GET',
                                    url='https://host_a.local/api',
                                    headers=MITMHeaders({
                                        'Host': ['host_a.local'],
                                        'Accept': ['application/json'],
                                    }))

    request_service_b = MITMRequest(method='GET',
                                    url='https://host_b.local/api',
                                    headers=MITMHeaders({
                                        'Host': ['host_b.local'],
                                        'Accept': ['application/json'],
                                    }))

    request_service_a_interactions = MITMRequest(
        method='GET',
        url='http://mitm-manager.local/service/TestServiceA/interactions',
        headers=MITMHeaders({
            'Host': ['mitm-manager.local'],
            'Accept': ['application/json'],
        }))

    request_service_b_interactions = MITMRequest(
        method='GET',
        url='http://mitm-manager.local/service/TestServiceB/interactions',
        headers=MITMHeaders({
            'Host': ['mitm-manager.local'],
            'Accept': ['application/json'],
        }))

    response_set_config = dispatcher.process_request(request_set_config)
    assert response_set_config.status_code == 201

    with patch('requests.request') as request:
        response_service_a_1 = dispatcher.process_request(request_service_a)
        assert response_service_a_1.status_code == 200

        response_service_a_2 = dispatcher.process_request(request_service_a)
        assert response_service_a_2.status_code == 200

        sleep(1)  # Wait for the callback
        assert request.call_count == 2

    response_service_b = dispatcher.process_request(request_service_b)
    assert response_service_b.status_code == 200

    response_service_a_interactions = dispatcher.process_request(
        request_service_a_interactions)
    response_service_b_interactions = dispatcher.process_request(
        request_service_b_interactions)

    assert json_loads(response_service_a_interactions.body) == {
        'interaction_0': {
            'num_calls': 2
        }
    }
    assert json_loads(response_service_b_interactions.body) == {
        'interaction_0': {
            'num_calls': 1
        }
    }