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
77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
from dirty_equals import IsPartialDict
|
|
from fastapi import Cookie, FastAPI, Header, Query
|
|
from fastapi._compat import PYDANTIC_V2
|
|
from fastapi.testclient import TestClient
|
|
from pydantic import BaseModel, Field
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class Model(BaseModel):
|
|
param: str = Field(alias="param_alias")
|
|
|
|
|
|
@app.get("/query")
|
|
async def query_model(data: Model = Query()):
|
|
return {"param": data.param}
|
|
|
|
|
|
@app.get("/header")
|
|
async def header_model(data: Model = Header()):
|
|
return {"param": data.param}
|
|
|
|
|
|
@app.get("/cookie")
|
|
async def cookie_model(data: Model = Cookie()):
|
|
return {"param": data.param}
|
|
|
|
|
|
def test_query_model_with_alias():
|
|
client = TestClient(app)
|
|
response = client.get("/query", params={"param_alias": "value"})
|
|
assert response.status_code == 200, response.text
|
|
assert response.json() == {"param": "value"}
|
|
|
|
|
|
def test_header_model_with_alias():
|
|
client = TestClient(app)
|
|
response = client.get("/header", headers={"param_alias": "value"})
|
|
assert response.status_code == 200, response.text
|
|
assert response.json() == {"param": "value"}
|
|
|
|
|
|
def test_cookie_model_with_alias():
|
|
client = TestClient(app)
|
|
client.cookies.set("param_alias", "value")
|
|
response = client.get("/cookie")
|
|
assert response.status_code == 200, response.text
|
|
assert response.json() == {"param": "value"}
|
|
|
|
|
|
def test_query_model_with_alias_by_name():
|
|
client = TestClient(app)
|
|
response = client.get("/query", params={"param": "value"})
|
|
assert response.status_code == 422, response.text
|
|
details = response.json()
|
|
if PYDANTIC_V2:
|
|
assert details["detail"][0]["input"] == {"param": "value"}
|
|
|
|
|
|
def test_header_model_with_alias_by_name():
|
|
client = TestClient(app)
|
|
response = client.get("/header", headers={"param": "value"})
|
|
assert response.status_code == 422, response.text
|
|
details = response.json()
|
|
if PYDANTIC_V2:
|
|
assert details["detail"][0]["input"] == IsPartialDict({"param": "value"})
|
|
|
|
|
|
def test_cookie_model_with_alias_by_name():
|
|
client = TestClient(app)
|
|
client.cookies.set("param", "value")
|
|
response = client.get("/cookie")
|
|
assert response.status_code == 422, response.text
|
|
details = response.json()
|
|
if PYDANTIC_V2:
|
|
assert details["detail"][0]["input"] == {"param": "value"}
|