Ejemplo n.º 1
0
def test_vcr_use_cassette():
    record_mode = mock.Mock()
    test_vcr = VCR(record_mode=record_mode)
    with mock.patch(
            'vcr.cassette.Cassette.load',
            return_value=mock.MagicMock(inject=False)) as mock_cassette_load:

        @test_vcr.use_cassette('test')
        def function():
            pass

        assert mock_cassette_load.call_count == 0
        function()
        assert mock_cassette_load.call_args[1]['record_mode'] is record_mode

        # Make sure that calls to function now use cassettes with the
        # new filter_header_settings
        test_vcr.record_mode = mock.Mock()
        function()
        assert mock_cassette_load.call_args[1][
            'record_mode'] == test_vcr.record_mode

        # Ensure that explicitly provided arguments still supercede
        # those on the vcr.
        new_record_mode = mock.Mock()

    with test_vcr.use_cassette('test',
                               record_mode=new_record_mode) as cassette:
        assert cassette.record_mode == new_record_mode
Ejemplo n.º 2
0
def test_before_record_response():
    before_record_response = mock.Mock(return_value='mutated')
    cassette = Cassette('test', before_record_response=before_record_response)
    cassette.append('req', 'res')

    before_record_response.assert_called_once_with('res')
    assert cassette.responses[0] == 'mutated'
Ejemplo n.º 3
0
def test_arg_getter_functionality():
    arg_getter = mock.Mock(return_value={'path': 'test'})
    context_decorator = Cassette.use_arg_getter(arg_getter)

    with context_decorator as cassette:
        assert cassette._path == 'test'

    arg_getter.return_value = {'path': 'other'}

    with context_decorator as cassette:
        assert cassette._path == 'other'

    arg_getter.return_value = {
        'path': 'other',
        'filter_headers': ('header_name', )
    }

    @context_decorator
    def function():
        pass

    with mock.patch.object(
            Cassette, 'load',
            return_value=mock.MagicMock(inject=False)) as cassette_load:
        function()
        cassette_load.assert_called_once_with(**arg_getter.return_value)
Ejemplo n.º 4
0
def test_before_record_response():
    before_record_response = mock.Mock(return_value="mutated")
    cassette = Cassette("test", before_record_response=before_record_response)
    cassette.append("req", "res")

    before_record_response.assert_called_once_with("res")
    assert cassette.responses[0] == "mutated"
Ejemplo n.º 5
0
def test_arg_getter_functionality():
    arg_getter = mock.Mock(return_value={"path": "test"})
    context_decorator = Cassette.use_arg_getter(arg_getter)

    with context_decorator as cassette:
        assert cassette._path == "test"

    arg_getter.return_value = {"path": "other"}

    with context_decorator as cassette:
        assert cassette._path == "other"

    arg_getter.return_value = {
        "path": "other",
        "filter_headers": ("header_name", )
    }

    @context_decorator
    def function():
        pass

    with mock.patch.object(
            Cassette, "load",
            return_value=mock.MagicMock(inject=False)) as cassette_load:
        function()
        cassette_load.assert_called_once_with(**arg_getter.return_value)
Ejemplo n.º 6
0
def test_replace_query_parameters_callable():
    # This goes beyond test_replace_query_parameters() to ensure that the
    # callable receives the expected arguments.
    uri = 'http://g.com/?hey=there'
    request = Request('GET', uri, '', {})
    callme = mock.Mock(return_value='ho')
    replace_query_parameters(request, [('hey', callme)])
    assert request.uri == 'http://g.com/?hey=ho'
    assert callme.call_args == ((), {'request': request,
                                     'key': 'hey',
                                     'value': 'there'})
Ejemplo n.º 7
0
def test_replace_headers_callable():
    # This goes beyond test_replace_headers() to ensure that the callable
    # receives the expected arguments.
    headers = {'hey': 'there'}
    request = Request('GET', 'http://google.com', '', headers)
    callme = mock.Mock(return_value='ho')
    replace_headers(request, [('hey', callme)])
    assert request.headers == {'hey': 'ho'}
    assert callme.call_args == ((), {'request': request,
                                     'key': 'hey',
                                     'value': 'there'})
Ejemplo n.º 8
0
def test_before_record_response_as_filter():
    request = Request('GET', '/', '', {})
    response = object()  # just can't be None

    # Prevent actually saving the cassette
    with mock.patch('vcr.cassette.FilesystemPersister.save_cassette'):

        filter_all = mock.Mock(return_value=None)
        vcr = VCR(before_record_response=filter_all)
        with vcr.use_cassette('test') as cassette:
            cassette.append(request, response)
            assert cassette.data == []
            assert not cassette.dirty
Ejemplo n.º 9
0
def test_replace_headers_callable():
    # This goes beyond test_replace_headers() to ensure that the callable
    # receives the expected arguments.
    headers = {"hey": "there"}
    request = Request("GET", "http://google.com", "", headers)
    callme = mock.Mock(return_value="ho")
    replace_headers(request, [("hey", callme)])
    assert request.headers == {"hey": "ho"}
    assert callme.call_args == ((), {
        "request": request,
        "key": "hey",
        "value": "there"
    })
Ejemplo n.º 10
0
def test_replace_query_parameters_callable():
    # This goes beyond test_replace_query_parameters() to ensure that the
    # callable receives the expected arguments.
    uri = "http://g.com/?hey=there"
    request = Request("GET", uri, "", {})
    callme = mock.Mock(return_value="ho")
    replace_query_parameters(request, [("hey", callme)])
    assert request.uri == "http://g.com/?hey=ho"
    assert callme.call_args == ((), {
        "request": request,
        "key": "hey",
        "value": "there"
    })
Ejemplo n.º 11
0
def test_vcr_before_record_response_iterable():
    # Regression test for #191

    request = Request('GET', '/', '', {})
    response = object()  # just can't be None

    # Prevent actually saving the cassette
    with mock.patch('vcr.cassette.save_cassette'):

        # Baseline: non-iterable before_record_response should work
        mock_filter = mock.Mock()
        vcr = VCR(before_record_response=mock_filter)
        with vcr.use_cassette('test') as cassette:
            assert mock_filter.call_count == 0
            cassette.append(request, response)
            assert mock_filter.call_count == 1

        # Regression test: iterable before_record_response should work too
        mock_filter = mock.Mock()
        vcr = VCR(before_record_response=(mock_filter, ))
        with vcr.use_cassette('test') as cassette:
            assert mock_filter.call_count == 0
            cassette.append(request, response)
            assert mock_filter.call_count == 1