From f217731066312513ae860dfc663d47591bd05a56 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Mon, 20 Feb 2017 01:08:42 -0800 Subject: [PATCH 1/2] Improved GraphQL batch view errors. --- graphene_django/tests/test_views.py | 9 +++++++++ graphene_django/views.py | 13 +++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/graphene_django/tests/test_views.py b/graphene_django/tests/test_views.py index e7ec187..fa3aaed 100644 --- a/graphene_django/tests/test_views.py +++ b/graphene_django/tests/test_views.py @@ -183,6 +183,15 @@ def test_batch_allows_post_with_json_encoding(client): }] +def test_batch_fails_if_is_empty(client): + response = client.post(batch_url_string(), j([]), 'application/json') + + assert response.status_code == 200 + assert response_json(response) == { + 'errors': [{'message': 'Received an empty list in the batch request.'}] + } + + def test_allows_sending_a_mutation_via_post(client): response = client.post(url_string(), j(query='mutation TestMutation { writeTest { test } }'), 'application/json') diff --git a/graphene_django/views.py b/graphene_django/views.py index a68fd53..df37931 100644 --- a/graphene_django/views.py +++ b/graphene_django/views.py @@ -193,10 +193,19 @@ class GraphQLView(View): try: request_json = json.loads(request.body.decode('utf-8')) if self.batch: - assert isinstance(request_json, list) + assert isinstance(request_json, list), ( + 'Batch requests should receive a list, but received {}.' + ).format(repr(request_json)) + assert len(request_json) > 0, ( + 'Received an empty list in the batch request.' + ) else: - assert isinstance(request_json, dict) + assert isinstance(request_json, dict), ( + 'The received data is not a valid JSON query.' + ) return request_json + except AssertionError as e: + raise HttpError(HttpResponseBadRequest(str(e))) except: raise HttpError(HttpResponseBadRequest('POST body sent invalid JSON.')) From 2660de969ffae0ec0558a0d41ad36c2827630969 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Mon, 20 Feb 2017 01:15:13 -0800 Subject: [PATCH 2/2] Improved batch view tests --- graphene_django/tests/test_views.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/graphene_django/tests/test_views.py b/graphene_django/tests/test_views.py index fa3aaed..da7ad04 100644 --- a/graphene_django/tests/test_views.py +++ b/graphene_django/tests/test_views.py @@ -184,9 +184,9 @@ def test_batch_allows_post_with_json_encoding(client): def test_batch_fails_if_is_empty(client): - response = client.post(batch_url_string(), j([]), 'application/json') + response = client.post(batch_url_string(), '[]', 'application/json') - assert response.status_code == 200 + assert response.status_code == 400 assert response_json(response) == { 'errors': [{'message': 'Received an empty list in the batch request.'}] } @@ -441,9 +441,18 @@ def test_handles_errors_caused_by_a_lack_of_query(client): } -def test_handles_invalid_json_bodies(client): +def test_handles_not_expected_json_bodies(client): response = client.post(url_string(), '[]', 'application/json') + assert response.status_code == 400 + assert response_json(response) == { + 'errors': [{'message': 'The received data is not a valid JSON query.'}] + } + + +def test_handles_invalid_json_bodies(client): + response = client.post(url_string(), '[oh}', 'application/json') + assert response.status_code == 400 assert response_json(response) == { 'errors': [{'message': 'POST body sent invalid JSON.'}]