예제 #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
예제 #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
예제 #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
예제 #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
예제 #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
예제 #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')
예제 #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))
예제 #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 == []
예제 #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'
                       })
예제 #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'])
예제 #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'])
예제 #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'])
예제 #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')
예제 #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')
예제 #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'})
예제 #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')