def test_should_replay_only_once(service: BaseService, scenarios_dir):
    interaction = service.get_interactions_for_active_scenario()[0]
    interaction.max_replays = 1
    assert service.should_replay(interaction)

    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',
    )
    service.process_request(request_1)
    assert not service.should_replay(interaction)
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_get_interactions_for_active_scenario(service: BaseService,
                                              scenarios_dir, request):
    interaction_dir = request.fspath.join(
        '../fixtures/scenarios/test_scenario/TestService/')

    expected = [
        f'Interaction.from_file({interaction_dir.join("interaction_0.yaml")})',
        f'Interaction.from_file({interaction_dir.join("interaction_1.yaml")})',
    ]

    with patch(
            'inspire_mitmproxy.interaction.Interaction.from_file',
            side_effect=lambda interaction_file:
            f'Interaction.from_file({interaction_file})',
    ):
        result = service.get_interactions_for_active_scenario()

        assert expected == result
def test_should_not_replay(service: BaseService, scenarios_dir):
    interaction = service.get_interactions_for_active_scenario()[0]
    interaction.max_replays = 0
    assert not service.should_replay(interaction)
def test_get_interactions_for_active_scenario_raises(service: BaseService,
                                                     scenarios_dir):
    service.active_scenario = 'this_scenario_does_not_exist'

    with raises(ScenarioNotInService):
        service.get_interactions_for_active_scenario()