Remove routing decorators in `middleware/test_base.py` (#1488)

* Remove routing decorators in

* Update test_base.py middleware
This commit is contained in:
Amin Alaee 2022-02-09 14:59:10 +01:00 committed by GitHub
parent b79e59f52d
commit aa38ce4032
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 10 deletions

View File

@ -4,7 +4,7 @@ from starlette.applications import Starlette
from starlette.middleware import Middleware from starlette.middleware import Middleware
from starlette.middleware.base import BaseHTTPMiddleware from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import PlainTextResponse, StreamingResponse from starlette.responses import PlainTextResponse, StreamingResponse
from starlette.routing import Route from starlette.routing import Route, WebSocketRoute
class CustomMiddleware(BaseHTTPMiddleware): class CustomMiddleware(BaseHTTPMiddleware):
@ -14,21 +14,14 @@ class CustomMiddleware(BaseHTTPMiddleware):
return response return response
app = Starlette()
app.add_middleware(CustomMiddleware)
@app.route("/")
def homepage(request): def homepage(request):
return PlainTextResponse("Homepage") return PlainTextResponse("Homepage")
@app.route("/exc")
def exc(request): def exc(request):
raise Exception("Exc") raise Exception("Exc")
@app.route("/exc-stream")
def exc_stream(request): def exc_stream(request):
return StreamingResponse(_generate_faulty_stream()) return StreamingResponse(_generate_faulty_stream())
@ -38,7 +31,6 @@ def _generate_faulty_stream():
raise Exception("Faulty Stream") raise Exception("Faulty Stream")
@app.route("/no-response")
class NoResponse: class NoResponse:
def __init__(self, scope, receive, send): def __init__(self, scope, receive, send):
pass pass
@ -50,13 +42,24 @@ class NoResponse:
pass pass
@app.websocket_route("/ws")
async def websocket_endpoint(session): async def websocket_endpoint(session):
await session.accept() await session.accept()
await session.send_text("Hello, world!") await session.send_text("Hello, world!")
await session.close() await session.close()
app = Starlette(
routes=[
Route("/", endpoint=homepage),
Route("/exc", endpoint=exc),
Route("/exc-stream", endpoint=exc_stream),
Route("/no-response", endpoint=NoResponse),
WebSocketRoute("/ws", endpoint=websocket_endpoint),
],
middleware=[Middleware(CustomMiddleware)],
)
def test_custom_middleware(test_client_factory): def test_custom_middleware(test_client_factory):
client = test_client_factory(app) client = test_client_factory(app)
response = client.get("/") response = client.get("/")