question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

middleware: assert message["type"] == "http.response.start"

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
lazkacommented, Aug 18, 2020

Thanks, good to know that it’s not just me.

0reactions
tiangolocommented, Oct 19, 2022

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found