| 1 |
from fastapi import Cookie, FastAPI, Header, Query |
| 2 |
from fastapi.testclient import TestClient |
| 3 |
from pydantic import BaseModel |
| 4 |
|
| 5 |
app = FastAPI() |
| 6 |
|
| 7 |
|
| 8 |
class Model(BaseModel): |
| 9 |
param: str |
| 10 |
|
| 11 |
model_config = {"extra": "allow"} |
| 12 |
|
| 13 |
|
| 14 |
class AuthHeaders(BaseModel): |
| 15 |
x_user_id: str |
| 16 |
|
| 17 |
|
| 18 |
@app.get("/query") |
| 19 |
async def query_model_with_extra(data: Model = Query()): |
| 20 |
return data |
| 21 |
|
| 22 |
|
| 23 |
@app.get("/header") |
| 24 |
async def header_model_with_extra(data: Model = Header()): |
| 25 |
return data |
| 26 |
|
| 27 |
|
| 28 |
@app.get("/cookie") |
| 29 |
async def cookies_model_with_extra(data: Model = Cookie()): |
| 30 |
return data |
| 31 |
|
| 32 |
|
| 33 |
@app.get("/header-requires-hyphen") |
| 34 |
async def header_model_requires_hyphen(data: AuthHeaders = Header()): |
| 35 |
return data |
| 36 |
|
| 37 |
|
| 38 |
def test_query_pass_extra_list(): |
| 39 |
client = TestClient(app) |
| 40 |
resp = client.get( |
| 41 |
"/query", |
| 42 |
params={ |
| 43 |
"param": "123", |
| 44 |
"param2": ["456", "789"], # Pass a list of values as extra parameter |
| 45 |
}, |
| 46 |
) |
| 47 |
assert resp.status_code == 200 |
| 48 |
assert resp.json() == { |
| 49 |
"param": "123", |
| 50 |
"param2": ["456", "789"], |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
def test_query_pass_extra_single(): |
| 55 |
client = TestClient(app) |
| 56 |
resp = client.get( |
| 57 |
"/query", |
| 58 |
params={ |
| 59 |
"param": "123", |
| 60 |
"param2": "456", |
| 61 |
}, |
| 62 |
) |
| 63 |
assert resp.status_code == 200 |
| 64 |
assert resp.json() == { |
| 65 |
"param": "123", |
| 66 |
"param2": "456", |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
def test_header_pass_extra_list(): |
| 71 |
client = TestClient(app) |
| 72 |
|
| 73 |
resp = client.get( |
| 74 |
"/header", |
| 75 |
headers=[ |
| 76 |
("param", "123"), |
| 77 |
("param2", "456"), # Pass a list of values as extra parameter |
| 78 |
("param2", "789"), |
| 79 |
], |
| 80 |
) |
| 81 |
assert resp.status_code == 200 |
| 82 |
resp_json = resp.json() |
| 83 |
assert "param2" in resp_json |
| 84 |
assert resp_json["param2"] == ["456", "789"] |
| 85 |
|
| 86 |
|
| 87 |
def test_header_pass_extra_single(): |
| 88 |
client = TestClient(app) |
| 89 |
|
| 90 |
resp = client.get( |
| 91 |
"/header", |
| 92 |
headers=[ |
| 93 |
("param", "123"), |
| 94 |
("param2", "456"), |
| 95 |
], |
| 96 |
) |
| 97 |
assert resp.status_code == 200 |
| 98 |
resp_json = resp.json() |
| 99 |
assert "param2" in resp_json |
| 100 |
assert resp_json["param2"] == "456" |
| 101 |
|
| 102 |
|
| 103 |
def test_header_model_prefers_hyphenated_header_with_convert_underscores(): |
| 104 |
client = TestClient(app) |
| 105 |
|
| 106 |
resp = client.get( |
| 107 |
"/header-requires-hyphen", |
| 108 |
headers=[ |
| 109 |
("x-user-id", "hyphenated-value"), |
| 110 |
("x_user_id", "underscore-value"), |
| 111 |
], |
| 112 |
) |
| 113 |
|
| 114 |
assert resp.status_code == 200 |
| 115 |
assert resp.json() == {"x_user_id": "hyphenated-value"} |
| 116 |
|
| 117 |
|
| 118 |
def test_header_model_rejects_underscore_header_with_convert_underscores(): |
| 119 |
client = TestClient(app) |
| 120 |
|
| 121 |
resp = client.get( |
| 122 |
"/header-requires-hyphen", headers={"x_user_id": "underscore-value"} |
| 123 |
) |
| 124 |
|
| 125 |
assert resp.status_code == 422 |
| 126 |
assert resp.json()["detail"][0]["loc"] == ["header", "x_user_id"] |
| 127 |
|
| 128 |
|
| 129 |
def test_cookie_pass_extra_list(): |
| 130 |
client = TestClient(app) |
| 131 |
client.cookies = [ |
| 132 |
("param", "123"), |
| 133 |
("param2", "456"), # Pass a list of values as extra parameter |
| 134 |
("param2", "789"), |
| 135 |
] |
| 136 |
resp = client.get("/cookie") |
| 137 |
assert resp.status_code == 200 |
| 138 |
resp_json = resp.json() |
| 139 |
assert "param2" in resp_json |
| 140 |
assert resp_json["param2"] == "789" # Cookies only keep the last value |