Issue Manager / issue-manager (push) Has been cancelled
Build Docs / changes (push) Has been cancelled
Build Docs / langs (push) Has been cancelled
Build Docs / build-docs (push) Has been cancelled
Build Docs / docs-all-green (push) Has been cancelled
Conflict detector / main (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi) (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi-slim) (push) Has been cancelled
Test Redistribute / test-redistribute-alls-green (push) Has been cancelled
Test / lint (push) Has been cancelled
Test / test (pydantic-v1, 3.10) (push) Has been cancelled
Test / test (pydantic-v1, 3.11) (push) Has been cancelled
Test / test (pydantic-v1, 3.13) (push) Has been cancelled
Test / test (pydantic-v1, 3.8) (push) Has been cancelled
Test / test (pydantic-v1, 3.9) (push) Has been cancelled
Test / test (pydantic-v2, 3.10) (push) Has been cancelled
Test / test (pydantic-v2, 3.11) (push) Has been cancelled
Test / test (pydantic-v2, 3.12) (push) Has been cancelled
Test / test (pydantic-v2, 3.13) (push) Has been cancelled
Test / test (pydantic-v2, 3.14) (push) Has been cancelled
Test / test (pydantic-v2, 3.8) (push) Has been cancelled
Test / test (pydantic-v2, 3.9) (push) Has been cancelled
Test / coverage-combine (push) Has been cancelled
Test / check (push) Has been cancelled
Label Approved / label-approved (push) Has been cancelled
FastAPI People Contributors / job (push) Has been cancelled
FastAPI People Sponsors / job (push) Has been cancelled
Update Topic Repos / topic-repos (push) Has been cancelled
FastAPI People / job (push) Has been cancelled
Test / test (pydantic-v1, 3.12) (push) Has been cancelled
112 lines
2.5 KiB
Python
112 lines
2.5 KiB
Python
from fastapi import Cookie, FastAPI, Header, Query
|
|
from fastapi._compat import PYDANTIC_V2
|
|
from fastapi.testclient import TestClient
|
|
from pydantic import BaseModel
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class Model(BaseModel):
|
|
param: str
|
|
|
|
if PYDANTIC_V2:
|
|
model_config = {"extra": "allow"}
|
|
else:
|
|
|
|
class Config:
|
|
extra = "allow"
|
|
|
|
|
|
@app.get("/query")
|
|
async def query_model_with_extra(data: Model = Query()):
|
|
return data
|
|
|
|
|
|
@app.get("/header")
|
|
async def header_model_with_extra(data: Model = Header()):
|
|
return data
|
|
|
|
|
|
@app.get("/cookie")
|
|
async def cookies_model_with_extra(data: Model = Cookie()):
|
|
return data
|
|
|
|
|
|
def test_query_pass_extra_list():
|
|
client = TestClient(app)
|
|
resp = client.get(
|
|
"/query",
|
|
params={
|
|
"param": "123",
|
|
"param2": ["456", "789"], # Pass a list of values as extra parameter
|
|
},
|
|
)
|
|
assert resp.status_code == 200
|
|
assert resp.json() == {
|
|
"param": "123",
|
|
"param2": ["456", "789"],
|
|
}
|
|
|
|
|
|
def test_query_pass_extra_single():
|
|
client = TestClient(app)
|
|
resp = client.get(
|
|
"/query",
|
|
params={
|
|
"param": "123",
|
|
"param2": "456",
|
|
},
|
|
)
|
|
assert resp.status_code == 200
|
|
assert resp.json() == {
|
|
"param": "123",
|
|
"param2": "456",
|
|
}
|
|
|
|
|
|
def test_header_pass_extra_list():
|
|
client = TestClient(app)
|
|
|
|
resp = client.get(
|
|
"/header",
|
|
headers=[
|
|
("param", "123"),
|
|
("param2", "456"), # Pass a list of values as extra parameter
|
|
("param2", "789"),
|
|
],
|
|
)
|
|
assert resp.status_code == 200
|
|
resp_json = resp.json()
|
|
assert "param2" in resp_json
|
|
assert resp_json["param2"] == ["456", "789"]
|
|
|
|
|
|
def test_header_pass_extra_single():
|
|
client = TestClient(app)
|
|
|
|
resp = client.get(
|
|
"/header",
|
|
headers=[
|
|
("param", "123"),
|
|
("param2", "456"),
|
|
],
|
|
)
|
|
assert resp.status_code == 200
|
|
resp_json = resp.json()
|
|
assert "param2" in resp_json
|
|
assert resp_json["param2"] == "456"
|
|
|
|
|
|
def test_cookie_pass_extra_list():
|
|
client = TestClient(app)
|
|
client.cookies = [
|
|
("param", "123"),
|
|
("param2", "456"), # Pass a list of values as extra parameter
|
|
("param2", "789"),
|
|
]
|
|
resp = client.get("/cookie")
|
|
assert resp.status_code == 200
|
|
resp_json = resp.json()
|
|
assert "param2" in resp_json
|
|
assert resp_json["param2"] == "789" # Cookies only keep the last value
|