Ejemplo n.º 1
0
 def test_reset_empties_the_requests_cache(self):
     batch = BatchSession(self.client)
     batch.request('GET', '/foobar/baz',
                   permissions=mock.sentinel.permissions)
     assert len(batch.requests) == 1
     batch.reset()
     assert len(batch.requests) == 0
Ejemplo n.º 2
0
    def test_prefix_is_removed_from_batch_requests(self):
        batch = BatchSession(self.client)
        batch.request('GET', '/v1/foobar')
        batch.send()

        calls = self.client.session.request.call_args_list
        _, kwargs1 = calls[0]
        assert kwargs1['payload']['requests'][0]['path'] == '/foobar'
Ejemplo n.º 3
0
    def test_batch_send_multiple_requests_if_too_many_requests(self):
        batch = BatchSession(self.client, batch_max_requests=3)
        for i in range(5):
            batch.request('GET', '/foobar/%s' % i)
        batch.send()

        calls = self.client.session.request.call_args_list
        assert len(calls) == 2
        _, kwargs1 = calls[0]
        assert kwargs1['payload']['requests'][-1]['path'] == '/foobar/2'
        _, kwargs2 = calls[1]
        assert kwargs2['payload']['requests'][0]['path'] == '/foobar/3'
Ejemplo n.º 4
0
    def batch(self, **kwargs):
        if self._server_settings is None:
            resp, _ = self.session.request("GET", self.get_endpoint('root'))
            self._server_settings = resp['settings']

        batch_max_requests = self._server_settings['batch_max_requests']
        batch_session = BatchSession(self,
                                     batch_max_requests=batch_max_requests)
        batch_client = self.clone(session=batch_session, **kwargs)
        yield batch_client
        batch_session.send()
        batch_session.reset()
Ejemplo n.º 5
0
    def test_send_adds_data_attribute(self):
        batch = BatchSession(self.client)
        batch.request('GET', '/foobar/baz', data={'foo': 'bar'})
        batch.send()

        self.client.session.request.assert_called_with(
            method='POST',
            endpoint=self.client.endpoints.get('batch'),
            payload={'requests': [{
                'method': 'GET',
                'path': '/foobar/baz',
                'body': {'data': {'foo': 'bar'}}
            }]}
        )
Ejemplo n.º 6
0
    def test_send_adds_permissions_attribute(self):
        batch = BatchSession(self.client)
        batch.request('GET', '/foobar/baz',
                      permissions=mock.sentinel.permissions)
        batch.send()

        self.client.session.request.assert_called_with(
            method='POST',
            endpoint=self.client.endpoints.get('batch'),
            payload={'requests': [{
                'method': 'GET',
                'path': '/foobar/baz',
                'body': {'permissions': mock.sentinel.permissions}
            }]}
        )
Ejemplo n.º 7
0
    def test_send_adds_headers_if_specified(self):
        batch = BatchSession(self.client)
        batch.request('GET', '/foobar/baz', headers={'Foo': 'Bar'})
        batch.send()

        self.client.session.request.assert_called_with(
            method='POST',
            endpoint=self.client.endpoints.get('batch'),
            payload={'requests': [{
                'method': 'GET',
                'path': '/foobar/baz',
                'headers': {'Foo': 'Bar'},
                'body': {}
            }]}
        )
Ejemplo n.º 8
0
    def batch(self, **kwargs):
        if self._server_settings is None:
            resp, _ = self.session.request("GET", self.get_endpoint('root'))
            self._server_settings = resp['settings']

        batch_max_requests = self._server_settings['batch_max_requests']
        batch_session = BatchSession(self,
                                     batch_max_requests=batch_max_requests)
        batch_client = self.clone(session=batch_session, **kwargs)

        # Set a reference for reading results from the context.
        batch_client.results = batch_session.results

        yield batch_client
        batch_session.send()
        batch_session.reset()
Ejemplo n.º 9
0
    def test_batch_raises_exception_as_soon_as_subrequest_fails(self):
        self.client.session.request.side_effect = [
            ({"responses": [
                {"status": 404, "path": "/url2", "body": {}, "headers": {}}
            ]}, mock.sentinel.headers),
            ({"responses": [
                {"status": 200, "path": "/url1", "body": {}, "headers": {}},
            ]}, mock.sentinel.headers)]

        batch = BatchSession(self.client, batch_max_requests=1)
        batch.request('GET', '/v1/foobar')
        batch.request('GET', '/v1/foobar')

        with self.assertRaises(KintoException):
            batch.send()
        assert self.client.session.request.call_count == 1
Ejemplo n.º 10
0
    def test_batch_raises_exception_as_soon_as_subrequest_fails_with_status_code_5xx(
            self):
        self.client.session.request.side_effect = [({
            "responses": [{
                "status": 502,
                "path": "/url2",
                "body": {
                    "message": "Host not reachable"
                },
                "headers": {}
            }]
        }, mock.sentinel.headers),
                                                   ({
                                                       "responses": [
                                                           {
                                                               "status": 200,
                                                               "path": "/url1",
                                                               "body": {},
                                                               "headers": {}
                                                           },
                                                       ]
                                                   }, mock.sentinel.headers)]

        batch = BatchSession(self.client, batch_max_requests=1)
        batch.request('GET', '/v1/foobar')
        batch.request('GET', '/v1/foobar')

        with self.assertRaises(KintoException):
            batch.send()
        assert self.client.session.request.call_count == 1
Ejemplo n.º 11
0
 def test_reset_empties_the_requests_cache(self):
     batch = BatchSession(self.client)
     batch.request('GET', '/foobar/baz',
                   permissions=mock.sentinel.permissions)
     assert len(batch.requests) == 1
     batch.reset()
     assert len(batch.requests) == 0
Ejemplo n.º 12
0
    def test_prefix_is_removed_from_batch_requests(self):
        batch = BatchSession(self.client)
        batch.request('GET', '/v1/foobar')
        batch.send()

        calls = self.client.session.request.call_args_list
        _, kwargs1 = calls[0]
        assert kwargs1['payload']['requests'][0]['path'] == '/foobar'
Ejemplo n.º 13
0
    def test_prefix_is_removed_from_batch_requests(self):
        batch = BatchSession(self.client)
        batch.request("GET", "/v1/foobar")
        batch.send()

        calls = self.client.session.request.call_args_list
        _, kwargs1 = calls[0]
        assert kwargs1["payload"]["requests"][0]["path"] == "/foobar"
Ejemplo n.º 14
0
    def test_parse_results_retrieves_response_data(self):
        batch_response = {"body": {"data": {"id": "hey"}}, "status": 200}
        mock.sentinel.resp = {"responses": [batch_response]}
        self.client.session.request.return_value = (mock.sentinel.resp,
                                                    mock.sentinel.headers)
        batch = BatchSession(self.client)
        batch.request("GET", "/v1/foobar")
        batch.send()

        results = batch.results()
        assert len(results) == 1
        assert results[0] == batch_response["body"]
Ejemplo n.º 15
0
    def batch(self, **kwargs):
        if self._server_settings is None:
            resp, _ = self.session.request("GET", self.get_endpoint('root'))
            self._server_settings = resp['settings']

        batch_max_requests = self._server_settings['batch_max_requests']
        batch_session = BatchSession(self,
                                     batch_max_requests=batch_max_requests)
        batch_client = self.clone(session=batch_session, **kwargs)
        yield batch_client
        batch_session.send()
        batch_session.reset()
Ejemplo n.º 16
0
    def test_batch_send_multiple_requests_if_too_many_requests(self):
        batch = BatchSession(self.client, batch_max_requests=3)
        for i in range(5):
            batch.request("GET", "/foobar/%s" % i)
        batch.send()

        calls = self.client.session.request.call_args_list
        assert len(calls) == 2
        _, kwargs1 = calls[0]
        assert kwargs1["payload"]["requests"][-1]["path"] == "/foobar/2"
        _, kwargs2 = calls[1]
        assert kwargs2["payload"]["requests"][0]["path"] == "/foobar/3"
Ejemplo n.º 17
0
    def test_batch_send_multiple_requests_if_too_many_requests(self):
        batch = BatchSession(self.client, batch_max_requests=3)
        for i in range(5):
            batch.request('GET', '/foobar/%s' % i)
        batch.send()

        calls = self.client.session.request.call_args_list
        assert len(calls) == 2
        _, kwargs1 = calls[0]
        assert kwargs1['payload']['requests'][-1]['path'] == '/foobar/2'
        _, kwargs2 = calls[1]
        assert kwargs2['payload']['requests'][0]['path'] == '/foobar/3'
Ejemplo n.º 18
0
    def test_send_adds_data_attribute(self):
        batch = BatchSession(self.client)
        batch.request('GET', '/foobar/baz', data={'foo': 'bar'})
        batch.send()

        self.client.session.request.assert_called_with(
            method='POST',
            endpoint=self.client.endpoints.get('batch'),
            payload={'requests': [{
                'method': 'GET',
                'path': '/foobar/baz',
                'body': {'data': {'foo': 'bar'}}
            }]}
        )
Ejemplo n.º 19
0
    def test_send_adds_permissions_attribute(self):
        batch = BatchSession(self.client)
        batch.request('GET', '/foobar/baz',
                      permissions=mock.sentinel.permissions)
        batch.send()

        self.client.session.request.assert_called_with(
            method='POST',
            endpoint=self.client.endpoints.get('batch'),
            payload={'requests': [{
                'method': 'GET',
                'path': '/foobar/baz',
                'body': {'permissions': mock.sentinel.permissions}
            }]}
        )
Ejemplo n.º 20
0
    def test_send_adds_headers_if_specified(self):
        batch = BatchSession(self.client)
        batch.request('GET', '/foobar/baz', headers={'Foo': 'Bar'})
        batch.send()

        self.client.session.request.assert_called_with(
            method='POST',
            endpoint=self.client.endpoints.get('batch'),
            payload={'requests': [{
                'method': 'GET',
                'path': '/foobar/baz',
                'headers': {'Foo': 'Bar'},
                'body': {}
            }]}
        )
Ejemplo n.º 21
0
    def test_parse_results_retrieves_response_data(self):
        batch_response = {
            "body": {"data": {"id": "hey"}},
            "status": 200,
        }
        mock.sentinel.resp = {"responses": [batch_response]}
        self.client.session.request.return_value = (mock.sentinel.resp,
                                                    mock.sentinel.headers)
        batch = BatchSession(self.client)
        batch.request('GET', '/v1/foobar')
        batch.send()

        results = batch.results()
        assert len(results) == 1
        assert results[0] == batch_response["body"]
Ejemplo n.º 22
0
    def batch(self, **kwargs):
        if self._server_settings is None:
            resp, _ = self.session.request("GET", self.get_endpoint('root'))
            self._server_settings = resp['settings']

        batch_max_requests = self._server_settings['batch_max_requests']
        batch_session = BatchSession(self,
                                     batch_max_requests=batch_max_requests)
        batch_client = self.clone(session=batch_session, **kwargs)

        # Set a reference for reading results from the context.
        batch_client.results = batch_session.results

        yield batch_client
        batch_session.send()
        batch_session.reset()
Ejemplo n.º 23
0
    def batch(self, **kwargs):
        if self._server_settings is None:
            resp, _ = self.session.request("GET", self.get_endpoint("root"))
            self._server_settings = resp["settings"]

        batch_max_requests = self._server_settings["batch_max_requests"]
        batch_session = BatchSession(self,
                                     batch_max_requests=batch_max_requests,
                                     ignore_4xx_errors=self._ignore_batch_4xx)
        batch_client = self.clone(session=batch_session, **kwargs)

        # Set a reference for reading results from the context.
        batch_client.results = batch_session.results

        yield batch_client
        batch_session.send()
        batch_session.reset()
Ejemplo n.º 24
0
    def test_send_adds_headers_if_specified(self):
        batch = BatchSession(self.client)
        batch.request("GET", "/foobar/baz", headers={"Foo": "Bar"})
        batch.send()

        self.client.session.request.assert_called_with(
            method="POST",
            endpoint=self.client.endpoints.get("batch"),
            payload={
                "requests": [{
                    "method": "GET",
                    "path": "/foobar/baz",
                    "headers": {
                        "Foo": "Bar"
                    },
                    "body": {}
                }]
            },
        )
Ejemplo n.º 25
0
    def test_send_adds_data_attribute(self):
        batch = BatchSession(self.client)
        batch.request("GET", "/foobar/baz", data={"foo": "bar"})
        batch.send()

        self.client.session.request.assert_called_with(
            method="POST",
            endpoint=self.client.endpoints.get("batch"),
            payload={
                "requests": [{
                    "method": "GET",
                    "path": "/foobar/baz",
                    "body": {
                        "data": {
                            "foo": "bar"
                        }
                    }
                }]
            },
        )
Ejemplo n.º 26
0
    def test_send_adds_permissions_attribute(self):
        batch = BatchSession(self.client)
        batch.request("GET",
                      "/foobar/baz",
                      permissions=mock.sentinel.permissions)
        batch.send()

        self.client.session.request.assert_called_with(
            method="POST",
            endpoint=self.client.endpoints.get("batch"),
            payload={
                "requests": [{
                    "method": "GET",
                    "path": "/foobar/baz",
                    "body": {
                        "permissions": mock.sentinel.permissions
                    },
                }]
            },
        )
Ejemplo n.º 27
0
 def test_requests_are_stacked(self):
     batch = BatchSession(self.client)
     batch.request('GET', '/foobar/baz',
                   mock.sentinel.data,
                   mock.sentinel.permissions)
     assert len(batch.requests) == 1
Ejemplo n.º 28
0
 def test_requests_are_stacked(self):
     batch = BatchSession(self.client)
     batch.request('GET', '/foobar/baz', mock.sentinel.data,
                   mock.sentinel.permissions)
     assert len(batch.requests) == 1