|
|
@ -11,6 +11,10 @@ class Model(BaseModel): |
|
|
model_config = {"extra": "allow"} |
|
|
model_config = {"extra": "allow"} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AuthHeaders(BaseModel): |
|
|
|
|
|
x_user_id: str |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/query") |
|
|
@app.get("/query") |
|
|
async def query_model_with_extra(data: Model = Query()): |
|
|
async def query_model_with_extra(data: Model = Query()): |
|
|
return data |
|
|
return data |
|
|
@ -26,6 +30,11 @@ async def cookies_model_with_extra(data: Model = Cookie()): |
|
|
return data |
|
|
return data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/header-requires-hyphen") |
|
|
|
|
|
async def header_model_requires_hyphen(data: AuthHeaders = Header()): |
|
|
|
|
|
return data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_query_pass_extra_list(): |
|
|
def test_query_pass_extra_list(): |
|
|
client = TestClient(app) |
|
|
client = TestClient(app) |
|
|
resp = client.get( |
|
|
resp = client.get( |
|
|
@ -91,6 +100,32 @@ def test_header_pass_extra_single(): |
|
|
assert resp_json["param2"] == "456" |
|
|
assert resp_json["param2"] == "456" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_header_model_prefers_hyphenated_header_with_convert_underscores(): |
|
|
|
|
|
client = TestClient(app) |
|
|
|
|
|
|
|
|
|
|
|
resp = client.get( |
|
|
|
|
|
"/header-requires-hyphen", |
|
|
|
|
|
headers=[ |
|
|
|
|
|
("x-user-id", "hyphenated-value"), |
|
|
|
|
|
("x_user_id", "underscore-value"), |
|
|
|
|
|
], |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 200 |
|
|
|
|
|
assert resp.json() == {"x_user_id": "hyphenated-value"} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_header_model_rejects_underscore_header_with_convert_underscores(): |
|
|
|
|
|
client = TestClient(app) |
|
|
|
|
|
|
|
|
|
|
|
resp = client.get( |
|
|
|
|
|
"/header-requires-hyphen", headers={"x_user_id": "underscore-value"} |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
assert resp.status_code == 422 |
|
|
|
|
|
assert resp.json()["detail"][0]["loc"] == ["header", "x_user_id"] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cookie_pass_extra_list(): |
|
|
def test_cookie_pass_extra_list(): |
|
|
client = TestClient(app) |
|
|
client = TestClient(app) |
|
|
client.cookies = [ |
|
|
client.cookies = [ |
|
|
|