Skip to content
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

Don't group duplicated headers on a single string on the TestClient #2219

Merged
merged 4 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions starlette/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,7 @@ def handle_request(self, request: httpx.Request) -> httpx.Response:
headers = [(b"host", (f"{host}:{port}").encode())]

# Include other request headers.
headers += [
(key.lower().encode(), value.encode())
for key, value in request.headers.items()
]
headers += [(key, value) for _, key, value in request.headers._list]
Kludex marked this conversation as resolved.
Show resolved Hide resolved

scope: typing.Dict[str, typing.Any]

Expand Down
20 changes: 20 additions & 0 deletions tests/test_testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sys
from asyncio import current_task as asyncio_current_task
from contextlib import asynccontextmanager
from typing import Callable, Dict, List, Tuple

import anyio
import pytest
Expand All @@ -10,6 +11,7 @@

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.requests import Request
from starlette.responses import JSONResponse, RedirectResponse, Response
from starlette.routing import Route
from starlette.testclient import TestClient
Expand Down Expand Up @@ -342,3 +344,21 @@ async def app(scope, receive, send):
client = test_client_factory(app, follow_redirects=False)
response = client.get("/")
assert response.status_code == 307


@pytest.mark.parametrize(
"headers,expected_response",
[([("x-token", "foo"), ("x-token", "bar")], {"x-token": ["foo", "bar"]})],
)
def test_headers_with_duplicate_field_name(
headers: List[Tuple[str, str]],
expected_response: Dict[str, List[str]],
test_client_factory: Callable[[Starlette], TestClient],
):
def homepage(request: Request) -> JSONResponse:
return JSONResponse({"x-token": request.headers.getlist("x-token")})

app = Starlette(routes=[Route("/", endpoint=homepage)])
client = test_client_factory(app)
response = client.get("/", headers=headers)
assert response.json() == expected_response