Exemplo n.º 1
0
    def test_raw_request_returns_response_object(self, transport):
        response = Response(201, {}, 'hello, world!')
        when(transport).request(url='/foo',
                                method='GET',
                                headers=m.any(),
                                data=m.any()).thenReturn(response)

        client = Client(transport)
        assert client.raw_request('/foo') == response
Exemplo n.º 2
0
    def test_request_returns_None_if_response_status_code_is_404(
            self, transport):
        when(transport).request(
            url=m.any(),
            method=m.any(),
            headers=m.any(),
            data=m.any(),
        ).thenReturn(Response(404, {}, ''))

        client = Client(transport)
        assert client.request('/foo') is None
Exemplo n.º 3
0
    def test_request_returns_None_if_body_is_empty(self, transport):
        data = {'foo': 'hello', 'bar': 123}

        when(transport).request(
            url='/foo',
            method='GET',
            headers=m.any(),
            data=m.any(),
        ).thenReturn(Response(200, {}, ''))

        client = Client(transport)
        assert client.request('/foo') is None
Exemplo n.º 4
0
    def test_request_returns_parsed_json(self, transport):
        data = {'foo': 'hello', 'bar': 123}

        when(transport).request(
            url='/foo',
            method='GET',
            headers=m.any(),
            data=m.any(),
        ).thenReturn(Response(200, {}, json.dumps(data)))

        client = Client(transport)
        assert client.request('/foo') == data
Exemplo n.º 5
0
    def test_request_raises_ClientError_if_response_status_code_is_not_2xx(
            self, transport):
        when(transport).request(
            url=m.any(),
            method=m.any(),
            headers=m.any(),
            data=m.any(),
        ).thenReturn(Response(400, {}, ''))

        client = Client(transport)

        with pytest.raises(ClientError) as exc:
            client.request('/foo')
        assert exc.value.status_code == 400
Exemplo n.º 6
0
    def test_request_method_defaults_to_post_if_data(self, transport,
                                                     request_method):
        client = Client(transport)

        request_method(client, '/foo', data=[1, 2, 3])

        verify_request(transport, method='POST')
Exemplo n.º 7
0
    def test_request_data_automatically_json_serialized(
            self, transport, request_method):
        client = Client(transport)

        data = {'foo': 'hello', 'bar': 123}
        request_method(client, '/foo', data=data)

        verify_request(transport, data=json.dumps(data))
Exemplo n.º 8
0
    def test_removal_of_multiple_cleanups_for_same_url(self, transport):
        callbacks = ClenaupCallbacks()

        client = Client(transport)
        client.add_cleanup('/foo', callbacks.cleanup1)
        client.add_cleanup('/foo', callbacks.cleanup2)
        client.remove_cleanup('/foo')

        client.cleanup()

        assert callbacks.calls == []
Exemplo n.º 9
0
    def test_request_custom_headers(self, transport, request_method):
        client = Client(transport)

        request_method(client, '/foo', headers={'AuthToken': '123'})

        verify_request(transport,
                       headers={
                           'AuthToken': '123',
                           'Content-Type': 'application/json'
                       })
Exemplo n.º 10
0
    def test_multiple_cleanups_for_same_url(self, transport):
        callbacks = ClenaupCallbacks()

        client = Client(transport)
        client.add_cleanup('/foo', callbacks.cleanup1)
        client.add_cleanup('/foo', callbacks.cleanup2)

        client.cleanup()

        assert sorted(callbacks.calls) == sorted(['cleanup1', 'cleanup2'])
Exemplo n.º 11
0
    def test_removing_cleanup_callback(self, transport):
        class Callbacks:
            def __init__(self):
                self.calls = []

            def cleanup1(self):
                self.calls.append('cleanup1')

            def cleanup2(self):
                self.calls.append('cleanup2')

        callbacks = Callbacks()

        client = Client(transport)
        client.add_cleanup('/foo', callbacks.cleanup1)
        client.add_cleanup('/bar', callbacks.cleanup2)
        client.remove_cleanup('/foo')

        client.cleanup()

        assert sorted(callbacks.calls) == sorted(['cleanup2'])
Exemplo n.º 12
0
    def test_cleanup_executes_all_registered_cleanup_callbacks(
            self, transport):
        class Callbacks:
            def __init__(self):
                self.calls = []

            def cleanup1(self):
                self.calls.append('cleanup1')

            def cleanup2(self):
                self.calls.append('cleanup2')

        callbacks = Callbacks()

        client = Client(transport)
        client.add_cleanup('/foo', callbacks.cleanup1)
        client.add_cleanup('/bar', callbacks.cleanup2)

        client.cleanup()

        assert sorted(callbacks.calls) == sorted(['cleanup1', 'cleanup2'])
Exemplo n.º 13
0
    def test_request_method_defaults_to_get(self, transport, request_method):
        client = Client(transport)

        request_method(client, '/foo')

        verify_request(transport, method='GET')
Exemplo n.º 14
0
    def test_request_base_url(self, transport, request_method):
        client = Client(transport, '/api')

        request_method(client, '/foo/bar/baz')

        verify_request(transport, url='/api/foo/bar/baz')
Exemplo n.º 15
0
    def test_request_default_headers(self, transport, request_method):
        client = Client(transport)

        request_method(client, '/foo')

        verify_request(transport, headers={'Content-Type': 'application/json'})
Exemplo n.º 16
0
    def test_request_custom_method(self, transport, request_method):
        client = Client(transport)

        request_method(client, '/foo', method='PUT')

        verify_request(transport, method='PUT')