Test for ClientDisconnect while reading body

This commit is contained in:
Tom Christie 2018-07-13 14:31:05 +01:00
parent 23a4b19505
commit 6f2ee9c9be
1 changed files with 25 additions and 1 deletions

View File

@ -1,4 +1,7 @@
from starlette import Request, JSONResponse, TestClient from starlette import Request, JSONResponse, TestClient
from starlette.request import ClientDisconnect
import asyncio
import pytest
def test_request_url(): def test_request_url():
@ -8,7 +11,6 @@ def test_request_url():
data = {"method": request.method, "url": request.url} data = {"method": request.method, "url": request.url}
response = JSONResponse(data) response = JSONResponse(data)
await response(receive, send) await response(receive, send)
return asgi return asgi
client = TestClient(app) client = TestClient(app)
@ -209,3 +211,25 @@ def test_request_without_setting_receive():
client = TestClient(app) client = TestClient(app)
response = client.post("/", json={"a": "123"}) response = client.post("/", json={"a": "123"})
assert response.json() == {"json": "Receive channel not available"} 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))