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
Exemple #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"
Exemple #3
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'
    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'
    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()
    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'
Exemple #7
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"
Exemple #8
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'
Exemple #9
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"]
Exemple #10
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()
Exemple #11
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'}}
            }]}
        )
Exemple #12
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'}}
            }]}
        )
Exemple #13
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}
            }]}
        )
Exemple #14
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': {}
            }]}
        )
Exemple #15
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"]
Exemple #16
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': {}
            }]}
        )
Exemple #17
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}
            }]}
        )
Exemple #18
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
Exemple #19
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()
Exemple #20
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()
Exemple #21
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()
Exemple #22
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": {}
                }]
            },
        )
Exemple #23
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"
                        }
                    }
                }]
            },
        )
Exemple #24
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
                    },
                }]
            },
        )