TestClient follow_redirects #1871
-
I think before #1376 the TestClient would not follow redirects, I'm not sure if this was on purpose or by mistake. Other than failing existing tests, now it has a different behaviour compared to httpx because httpx has follow_redirects=False by default. Here's a sample code to show the difference: from starlette.applications import Starlette
from starlette.routing import Route
from starlette.responses import PlainTextResponse, RedirectResponse
from starlette.testclient import TestClient
from httpx import AsyncClient
def foo(request):
return PlainTextResponse("Hello World!")
def bar(request):
return RedirectResponse(request.url_for("foo"), status_code=302)
app = Starlette(
routes=[
Route("/foo", foo),
Route("/bar", bar),
]
)
async def test_async():
client = AsyncClient(app=app, base_url="http://testserver")
response = await client.get("/bar")
assert response.status_code == 302
def test_sync():
client = TestClient(app)
response = client.get("/bar")
assert response.status_code == 200 I found this today as I saw pipelines failing for my project: https://github.com/aminalaee/sqladmin which use both sync and async tests. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
@Kludex Any ideas? |
Beta Was this translation helpful? Give feedback.
-
This was the conclusion: #1872 (comment) |
Beta Was this translation helpful? Give feedback.
This was the conclusion: #1872 (comment)