Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal Server Error when receiving invalid JSON. #81

Merged
merged 10 commits into from Oct 28, 2019
7 changes: 6 additions & 1 deletion tartiflette_asgi/endpoints.py
@@ -1,4 +1,6 @@
import typing
import json


from starlette.background import BackgroundTasks
from starlette.datastructures import QueryParams
Expand Down Expand Up @@ -37,7 +39,10 @@ async def post(self, request: Request) -> Response:
content_type = request.headers.get("Content-Type", "")

if "application/json" in content_type:
data = await request.json()
try:
data = await request.json()
except json.JSONDecodeError:
return JSONResponse({"error": "Invalid JSON."}, 400)
elif "application/graphql" in content_type:
body = await request.body()
data = {"query": body.decode()}
Expand Down
8 changes: 8 additions & 0 deletions tests/test_graphql_api.py
Expand Up @@ -27,6 +27,14 @@ def test_post_json(client: TestClient):
assert response.json() == {"data": {"hello": "Hello stranger"}}


def test_post_invalid_json(client: TestClient):
response = client.post(
"/", data="{test", headers={"content-type": "application/json"}
)
assert response.status_code == 400
assert response.json() == {"error": "Invalid JSON."}


def test_post_graphql(client: TestClient):
response = client.post(
"/", data="{ hello }", headers={"content-type": "application/graphql"}
Expand Down