Ejemplo n.º 1
0
    def test_compatibility_of_multiple_requests(self):
        '''
            Make multiple requests without the batch and in the batch and
            assert that both gives the same results.
        '''

        data = json.dumps({"text": "Batch"})
        # Make GET, POST and PUT requests individually.

        # Get the response for an individual GET request.
        inv_req = ensure_text_content(self.client.get("/views/"))
        inv_get = self.prepare_response(inv_req.status_code, inv_req.text,
                                        response_headers(inv_req))

        # Get the response for an individual POST request.
        inv_req = ensure_text_content(
            self.client.post("/views/", data, content_type="text/plain"))
        inv_post = self.prepare_response(inv_req.status_code, inv_req.text,
                                         response_headers(inv_req))

        # Get the response for an individual PUT request.
        inv_req = ensure_text_content(
            self.client.patch("/views/", data, content_type="text/plain"))
        inv_put = self.prepare_response(inv_req.status_code, inv_req.text,
                                        response_headers(inv_req))

        # Consolidate all the responses.
        indv_responses = [inv_get, inv_post, inv_put]

        # Make a batch call for GET, POST and PUT request.
        get_req = ("get", "/views/", '', {})
        post_req = ("post", "/views/", data, {"content_type": "text/plain"})
        put_req = ("put", "/views/", data, {"content_type": "text/plain"})

        # Get the response for a batch request.
        batch_requests = self.make_multiple_batch_request(
            [get_req, post_req, put_req])
        batch_responses = json.loads(batch_requests.text)

        # Assert all the responses are compatible.
        for indv_resp, batch_resp in zip(indv_responses, batch_responses):
            del batch_resp["reason_phrase"]
            self.assert_reponse_compatible(indv_resp, batch_resp)
    def test_invalid_http_method(self):
        '''
            Make a batch request with invalid HTTP method.
        '''
        resp = ensure_text_content(
            self.client.post(
                "/api/v1/batch/",
                json.dumps([self._batch_request("select", "/views", "", {})]),
                content_type="application/json"))

        self.assertEqual(resp.status_code, 400, "Method validation is broken!")
        self.assertEqual(resp.text.lower(), "invalid request method.",
                         "Method validation is broken!")
Ejemplo n.º 3
0
    def test_missing_http_method(self):
        '''
            Make a batch request without HTTP method.
        '''
        resp = ensure_text_content(
            self.client.post(
                "/api/v1/batch/",
                json.dumps([{"body": "/views"}]),
                content_type="application/json"
            )
        )

        self.assertEqual(resp.status_code, 400, "Method & URL validation is broken!")
        self.assertEqual(resp.text.lower(), "request definition should have url, method defined.", "Method validation is broken!")
Ejemplo n.º 4
0
    def test_view_that_raises_exception(self):
        '''
            Make a batch request to a view that raises exception.
        '''
        resp = ensure_text_content(
            self.client.post(
                "/api/v1/batch/",
                json.dumps([{"method": "get", "url": "/exception/"}]),
                content_type="application/json"
            )
        )

        resp = json.loads(resp.text)[0]
        self.assertEqual(resp['status_code'], 500, "Exceptions should return 500.")
        self.assertEqual(resp['body'].lower(), "exception", "Exception handling is broken!")
Ejemplo n.º 5
0
    def test_invalid_batch_request(self):
        '''
            Make a batch request without wrapping in the list.
        '''
        resp = ensure_text_content(
            self.client.post(
                "/api/v1/batch/",
                json.dumps({"method": "get", "url": "/views/"}),
                content_type="application/json"
            )
        )

        self.assertEqual(resp.status_code, 400, "Batch requests should always be in list.")
        self.assertEqual(resp.text.lower(), "the body of batch request should always be list!",
                         "List validation is broken!")
    def test_exception_view_with_deserialization(self):
        """Make batch request to exception endpoint AND try to deserialize the response"""
        with mock.patch('batch_requests.views._settings.DESERIALIZE_RESPONSES'
                        ) as mock_des:
            mock_des.return_value = True
            resp = ensure_text_content(
                self.client.post("/api/v1/batch/",
                                 json.dumps([{
                                     "method": "get",
                                     "url": "/exception/"
                                 }]),
                                 content_type="application/json"))

        assert resp.status_code == 200
        responses = json.loads(resp.text)
        assert responses[0]['status_code'] == 500
    def test_compatibility_of_delete_request(self):
        '''
            Make a DELETE request without the batch and in the batch and assert
            that both gives the same results.
        '''
        # Get the response for an individual request.
        inv_req = ensure_text_content(self.client.delete("/views/"))
        inv_resp = self.prepare_response(inv_req.status_code, inv_req.text, inv_req._headers)

        # Get the response for a batch request.
        batch_request = self.make_a_batch_request("delete", "/views/", "")
        batch_resp = json.loads(batch_request.text)[0]
        del batch_resp["reason_phrase"]

        # Assert both individual request response and batch response are equal.
        self.assert_reponse_compatible(inv_resp, batch_resp)
    def test_compatibility_of_patch_request(self):
        '''
            Make a POST request without the batch and in the batch and assert
            that both gives the same results.
        '''
        data = json.dumps({"text": "hello"})

        # Get the response for an individual request.
        inv_req = ensure_text_content(self.client.post("/views/", data, content_type="text/plain"))
        inv_resp = self.prepare_response(inv_req.status_code, inv_req.text, inv_req._headers)

        # Get the response for a batch request.
        batch_request = self.make_a_batch_request("POST", "/views/", data, {"CONTENT_TYPE": "text/plain"})
        batch_resp = json.loads(batch_request.text)[0]
        del batch_resp["reason_phrase"]

        # Assert both individual request response and batch response are equal.
        self.assert_reponse_compatible(inv_resp, batch_resp)
    def test_view_that_raises_exception(self):
        '''
            Make a batch request to a view that raises exception.
        '''
        with self.settings(DEBUG=True):
            resp = ensure_text_content(
                self.client.post("/api/v1/batch/",
                                 json.dumps([
                                     {
                                         "method": "get",
                                         "url": "/exception/"
                                     },
                                     {
                                         "method": "get",
                                         "url": "/rate-limited/"
                                     },
                                     {
                                         "method": "get",
                                         "url": "/views/"
                                     },
                                 ]),
                                 content_type="application/json"))

        assert resp.status_code == 200
        responses = json.loads(resp.text)

        expected = {
            0: (500, 'exception'),
            1: (429, 'rate limited'),
            2: (200, 'success!'),
        }

        for index, expects in expected.items():
            resp = responses[index]
            exp_status, exp_explanation = expects
            assert resp['status_code'] == exp_status
            assert resp['body'].lower() == exp_explanation