From 6f2ee9c9be3c21797aa2128218fade1e2b84833b Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 13 Jul 2018 14:31:05 +0100 Subject: [PATCH] Test for ClientDisconnect while reading body --- tests/test_request.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/test_request.py b/tests/test_request.py index ac904120..3c848023 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -1,4 +1,7 @@ from starlette import Request, JSONResponse, TestClient +from starlette.request import ClientDisconnect +import asyncio +import pytest def test_request_url(): @@ -8,7 +11,6 @@ def test_request_url(): data = {"method": request.method, "url": request.url} response = JSONResponse(data) await response(receive, send) - return asgi client = TestClient(app) @@ -209,3 +211,25 @@ def test_request_without_setting_receive(): client = TestClient(app) response = client.post("/", json={"a": "123"}) assert response.json() == {"json": "Receive channel not available"} + + +def test_request_disconnect(): + """ + If a client disconnect occurs while reading request body + then ClientDisconnect should be raised. + """ + + def app(scope): + async def asgi(receive, send): + request = Request(scope, receive) + await request.body() + return asgi + + async def receiver(): + return {'type': 'http.disconnect'} + + scope = {'method': 'POST', 'path': '/'} + asgi_callable = app(scope) + loop = asyncio.get_event_loop() + with pytest.raises(ClientDisconnect): + loop.run_until_complete(asgi_callable(receiver, None))