Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

starlette.TestClient with Jinja2Templates and middleware #806

Closed
bloomingmath opened this issue Dec 19, 2019 · 4 comments
Closed

starlette.TestClient with Jinja2Templates and middleware #806

bloomingmath opened this issue Dec 19, 2019 · 4 comments

Comments

@bloomingmath
Copy link

Describe the bug

The problem arise when using starlette TestClient to get a template response that passes through a middleware.

To Reproduce

I made a repo with the minimal fastapi app to raise the error:

https://github.com/bloomingmath/fastapi-middleware-issue

To reproduce, clone the repo and run pytest main.py

Expected behavior

I expect the test to run flawlessly, but it raise AssertionError as shown in the readme.md of the same repo.

Environment

  • OS: Ubuntu 18.04
  • Python 3.7.5
  • attrs==19.3.0
  • certifi==2019.11.28
  • chardet==3.0.4
  • fastapi==0.45.0
  • idna==2.8
  • importlib-metadata==1.3.0
  • Jinja2==2.10.3
  • MarkupSafe==1.1.1
  • more-itertools==8.0.2
  • packaging==19.2
  • pluggy==0.13.1
  • py==1.8.0
  • pydantic==1.2
  • pyparsing==2.4.5
  • pytest==5.3.2
  • requests==2.22.0
  • six==1.13.0
  • starlette==0.12.9
  • urllib3==1.25.7
  • wcwidth==0.1.7
  • zipp==0.6.0
@bloomingmath bloomingmath added the bug Something isn't working label Dec 19, 2019
@podhmo
Copy link

podhmo commented Dec 20, 2019

This is related issue, in starlette, maybe...

encode/starlette#472

Hmm.. this is strange, maybe. Somehow, message["type"] is not "http.response.start", in starlette's middleware.
I print debugged, this value is "http.response.template".

actual value is like below.

{'type': 'http.response.template', 'template': <Template 'page.html'>, 'context': {'request': <starlette.requests.Request object at 0x7f9f2ced5850>}}

So, assertion error occured in this line

https://github.com/encode/starlette/blob/00e830080499c47a3e72f1b4adb2678758e388ee/starlette/middleware/base.py#L47

but async-asgi-testclient is ok

Writing similar tests, using async-asgi-testclient , test is passed.

import pytest


@pytest.mark.asyncio
async def test_middleware2():
    from async_asgi_testclient import TestClient

    async with TestClient(app) as client:
        response = await client.get("/")
        assert "X-Process-Time" in response.headers

then, message value is below

{'type': 'http.response.start', 'status': 200, 'headers': [(b'content-length', b'131'), (b'content-type', b'text/html; charset=utf-8')]}

@bloomingmath
Copy link
Author

You are right @podhmo, the problem is with starlette and not with fastapi. Also, using async-asgi-testclient permits testing the application until starlette's test client is fixed.
Thank you for your help.

@podhmo
Copy link

podhmo commented Dec 20, 2019

OK. I understood completely. This is the same issue as encode/starlette#472

  1. Starlette's template response class treats specially when scope has { "extensions": { "http.response.template": ...}}, then returns "http.response.template" message. ( here )
  2. when Starlette's testclient sendindg http request, always attaching data including { "extensions": { "http.response.template": ...}} ( here )
  3. And, BaseMiddleware asserting "http.response.start" message. ( here )

So, test is failed.

@tiangolo
Copy link
Member

Thanks for the help here @podhmo ! 👏 🙇

Thanks for reporting back and closing the issue @bloomingmath 👍

@tiangolo tiangolo added question Question or problem answered reviewed and removed bug Something isn't working labels Feb 22, 2023
@tiangolo tiangolo changed the title [BUG] starlette.TestClient with Jinja2Templates and middleware starlette.TestClient with Jinja2Templates and middleware Feb 24, 2023
@tiangolo tiangolo reopened this Feb 28, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 28, 2023
@tiangolo tiangolo converted this issue into discussion #7865 Feb 28, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

3 participants