From d5c798e6b8f33b43409320eb9c8b5b91a4786a3a Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 25 Jan 2019 14:40:33 +0000 Subject: [PATCH] Example ASGI apps now ensure scope['type'] is expected message type. (#348) --- docs/index.md | 1 + docs/requests.md | 2 ++ docs/responses.md | 9 +++++++++ docs/testclient.md | 2 ++ docs/websockets.md | 1 + 5 files changed, 15 insertions(+) diff --git a/docs/index.md b/docs/index.md index 7b5a344a..d6f814f5 100644 --- a/docs/index.md +++ b/docs/index.md @@ -100,6 +100,7 @@ from starlette.responses import PlainTextResponse class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): diff --git a/docs/requests.md b/docs/requests.md index 93724668..c71932e8 100644 --- a/docs/requests.md +++ b/docs/requests.md @@ -13,6 +13,7 @@ from starlette.response import Response class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): @@ -95,6 +96,7 @@ from starlette.responses import Response class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): diff --git a/docs/responses.md b/docs/responses.md index be23a3dc..60f89b72 100644 --- a/docs/responses.md +++ b/docs/responses.md @@ -24,6 +24,7 @@ from starlette.responses import Response class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): @@ -62,6 +63,7 @@ from starlette.responses import HTMLResponse class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): @@ -79,6 +81,7 @@ from starlette.responses import PlainTextResponse class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): @@ -107,6 +110,7 @@ env = Environment(loader=FileSystemLoader('templates')) class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): @@ -141,6 +145,7 @@ from starlette.responses import JSONResponse class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): @@ -164,6 +169,7 @@ from starlette.responses import UJSONResponse class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): @@ -181,6 +187,7 @@ from starlette.responses import PlainTextResponse, RedirectResponse class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): @@ -210,6 +217,7 @@ async def slow_numbers(minimum, maximum): class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): @@ -237,6 +245,7 @@ from starlette.responses import FileResponse class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): diff --git a/docs/testclient.md b/docs/testclient.md index d8749a39..bc3e8d59 100644 --- a/docs/testclient.md +++ b/docs/testclient.md @@ -9,6 +9,7 @@ from starlette.testclient import TestClient class App: def __init__(self, scope): + assert scope['type'] == 'http' self.scope = scope async def __call__(self, receive, send): @@ -49,6 +50,7 @@ from starlette.websockets import WebSocket class App: def __init__(self, scope): + assert scope['type'] == 'websocket' self.scope = scope async def __call__(self, receive, send): diff --git a/docs/websockets.md b/docs/websockets.md index 07f3efb6..7a8150c6 100644 --- a/docs/websockets.md +++ b/docs/websockets.md @@ -12,6 +12,7 @@ from starlette.websockets import WebSocket class App: def __init__(self, scope): + assert scope['type'] == 'websocket' self.scope = scope async def __call__(self, receive, send):