middleware: assert message["type"] == "http.response.start"
See original GitHub issueDescribe the bug
I’ve tried to add a middleware to my app and then I started hitting a assertation error with the test client.
To Reproduce
Smallest reproducer I could find. It needs jinja2+middlware+testclient to hit the error.
import os
import tempfile
from typing import Callable
from fastapi import FastAPI
from fastapi import Request, Response
from fastapi.testclient import TestClient
from fastapi.templating import Jinja2Templates
dir_ = tempfile.mkdtemp()
with open(os.path.join(dir_, "index.html"), "w"):
pass
app = FastAPI()
templates = Jinja2Templates(directory=dir_)
@app.get("/")
async def index(request: Request) -> Response:
return templates.TemplateResponse("index.html", {"request": request})
@app.middleware("http")
async def some_middleware(request: Request, call_next: Callable) -> Response:
response: Response = await call_next(request)
return response
with TestClient(app) as client:
client.get("/")
Error (partial)
File "/home/lazka/.cache/pypoetry/virtualenvs/msys2-web-tQnlPJcR-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/home/lazka/.cache/pypoetry/virtualenvs/msys2-web-tQnlPJcR-py3.8/lib/python3.8/site-packages/starlette/middleware/base.py", line 25, in __call__
response = await self.dispatch_func(request, self.call_next)
File "a.py", line 25, in some_middleware
response: Response = await call_next(request)
File "/home/lazka/.cache/pypoetry/virtualenvs/msys2-web-tQnlPJcR-py3.8/lib/python3.8/site-packages/starlette/middleware/base.py", line 47, in call_next
assert message["type"] == "http.response.start"
AssertionError
Expected behavior
Adding a no-op middleware shouldn’t make the TestClient error out.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:6
- Comments:6 (3 by maintainers)
Top Results From Across the Web
RuntimeError: No response returned in FastAPI when refresh ...
assert message["type "] == "http.response.start". #4 is why, to reproduce this issue, we need two middlewares that inherit BaseHTTPMiddleware ...
Read more >Middleware - Starlette
The middleware responds to two particular types of HTTP request. ... send_with_extra_headers(message): if message["type"] == "http.response.start": headers ...
Read more >Working with ASGI and HTTP - Encode
There are two types of HTTP response message: One to initiate sending the response, and another to send the response body. await send({...
Read more >HTTP - Mangum
* RESPONSE - The http.response.start event has been sent by the application. The next expected message is the http.response.body event, containing the body ......
Read more >43 OSB-75050 to OSB-2032402 - Oracle Help Center
OSB-381319: HTTP business service URLs must start with either http:// or ... with incompatible response message type in the JMS transport configuration.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks, good to know that it’s not just me.
Thanks! The issue would not be removed, only closed. So if they for that they will find it, just closed, and it will help me (try to) keep things a bit cleaner.
Given that, I’ll then close it. Thanks!