示例#1
0
def test_interaction_execute_callbacks(interaction_callback: Interaction):
    with patch('requests.request') as request:
        interaction_callback.execute_callbacks()

        sleep(1)

        first_callback, second_callback = request.mock_calls

        first_expected = call(
            method='GET',
            url='http://callback.local',
            data=b'1',
            headers={'Host': 'callback.local'},
            timeout=10,
        )

        second_expected = call(
            method='GET',
            url='http://interpolated.local',
            data=b'2',
            headers={'Host': 'interpolated.local'},
            timeout=10,
        )

        assert first_callback == first_expected
        assert second_callback == second_expected
示例#2
0
def interaction_callback(request):
    with patch.dict('os.environ',
                    {'TEST_ENVAR_INTERPOLATION': 'interpolated'}):
        yield Interaction.from_file(
            Path(
                request.fspath.join(
                    '../fixtures/test_interaction_callback.yaml')))
def test_interaction_get_next_sequence_number_in_dir(
    tmpdir,
    interaction_dir_files: List[str],
    expected_next_sequence_number: int,
):
    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.get_next_sequence_number_in_dir(interaction_dir_path)

    assert expected_next_sequence_number == result
def test_interaction_to_dict(interaction_all_fields: Interaction):
    expected = {
        'request': {
            'body': 'very nice body that does match\n',
            'headers': {
                'Host': ['test.local'],
            },
            'url': 'https://test.local/path',
            'method': 'POST',
        },
        'response': {
            'body': '{"key": "value"}',
            'headers': {
                'Content-Type': ['application/json; charset=UTF-8'],
            },
            'status': {
                'message': 'OK',
                'code': 200,
            },
        },
        'match': {
            'regex': {
                'body': '.*does match.*',
                'url': 'https://test\.local/\d+/details',
            },
            'exact': ['method'],
        },
        'callbacks': [
            {
                'delay': 2,
                'request': {
                    'body': None,
                    'headers': {
                        'Host': ['callback.local']
                    },
                    'url': 'http://callback.local',
                    'method': 'GET',
                },
            },
        ]
    }

    result = interaction_all_fields.to_dict()

    assert expected == result
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_interaction_matches_request_negative(
        interaction_all_fields: Interaction, _request):
    assert not interaction_all_fields.matches_request(_request)
def test_interaction_matches_request_positive(
        interaction_all_fields: Interaction, _request: MITMRequest):
    assert interaction_all_fields.matches_request(_request)
def interaction_regex_but_no_exact(request):
    return Interaction.from_file(
        Path(
            request.fspath.join(
                '../fixtures/test_interaction_partial_matches.yaml')))
def interaction_only_required_fields(request):
    return Interaction.from_file(
        Path(request.fspath.join('../fixtures/test_interaction_simple.yaml')))
def interaction_all_fields(request):
    return Interaction.from_file(
        Path(
            request.fspath.join('../fixtures/test_interaction_detailed.yaml')))
def interaction_all_fields_regex_in_body(request):
    ('POST', '', 'https://test.local/bad_url/details',
     _get_headers(good=False)),
    return Interaction.from_file(
        Path(
            request.fspath.join('../fixtures/test_interaction_detailed.yaml')))