Skip to content

Commit 328cd50

Browse files
committed
Merge branch 'master' of github.com:graphql-python/graphene-django
2 parents 488992b + c8179de commit 328cd50

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

graphene_django/tests/test_views.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ def test_batch_allows_post_with_json_encoding(client):
183183
}]
184184

185185

186+
def test_batch_fails_if_is_empty(client):
187+
response = client.post(batch_url_string(), '[]', 'application/json')
188+
189+
assert response.status_code == 400
190+
assert response_json(response) == {
191+
'errors': [{'message': 'Received an empty list in the batch request.'}]
192+
}
193+
194+
186195
def test_allows_sending_a_mutation_via_post(client):
187196
response = client.post(url_string(), j(query='mutation TestMutation { writeTest { test } }'), 'application/json')
188197

@@ -432,9 +441,18 @@ def test_handles_errors_caused_by_a_lack_of_query(client):
432441
}
433442

434443

435-
def test_handles_invalid_json_bodies(client):
444+
def test_handles_not_expected_json_bodies(client):
436445
response = client.post(url_string(), '[]', 'application/json')
437446

447+
assert response.status_code == 400
448+
assert response_json(response) == {
449+
'errors': [{'message': 'The received data is not a valid JSON query.'}]
450+
}
451+
452+
453+
def test_handles_invalid_json_bodies(client):
454+
response = client.post(url_string(), '[oh}', 'application/json')
455+
438456
assert response.status_code == 400
439457
assert response_json(response) == {
440458
'errors': [{'message': 'POST body sent invalid JSON.'}]

graphene_django/views.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,19 @@ def parse_body(self, request):
193193
try:
194194
request_json = json.loads(request.body.decode('utf-8'))
195195
if self.batch:
196-
assert isinstance(request_json, list)
196+
assert isinstance(request_json, list), (
197+
'Batch requests should receive a list, but received {}.'
198+
).format(repr(request_json))
199+
assert len(request_json) > 0, (
200+
'Received an empty list in the batch request.'
201+
)
197202
else:
198-
assert isinstance(request_json, dict)
203+
assert isinstance(request_json, dict), (
204+
'The received data is not a valid JSON query.'
205+
)
199206
return request_json
207+
except AssertionError as e:
208+
raise HttpError(HttpResponseBadRequest(str(e)))
200209
except:
201210
raise HttpError(HttpResponseBadRequest('POST body sent invalid JSON.'))
202211

0 commit comments

Comments
 (0)