Coverage for tests / test_query_cookie_header_model_extra_params.py: 100%
48 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from fastapi import Cookie, FastAPI, Header, Query 1adbc
2from fastapi.testclient import TestClient 1adbc
3from pydantic import BaseModel 1adbc
5app = FastAPI() 1adbc
8class Model(BaseModel): 1adbc
9 param: str 1abc
11 model_config = {"extra": "allow"} 1adbc
14@app.get("/query") 1adbc
15async def query_model_with_extra(data: Model = Query()): 1adbc
16 return data 1qrstuvwx
19@app.get("/header") 1adbc
20async def header_model_with_extra(data: Model = Header()): 1adbc
21 return data 1ijklmnop
24@app.get("/cookie") 1adbc
25async def cookies_model_with_extra(data: Model = Cookie()): 1adbc
26 return data 1efgh
29def test_query_pass_extra_list(): 1adbc
30 client = TestClient(app) 1qsuw
31 resp = client.get( 1qsuw
32 "/query",
33 params={
34 "param": "123",
35 "param2": ["456", "789"], # Pass a list of values as extra parameter
36 },
37 )
38 assert resp.status_code == 200 1qsuw
39 assert resp.json() == { 1qsuw
40 "param": "123",
41 "param2": ["456", "789"],
42 }
45def test_query_pass_extra_single(): 1adbc
46 client = TestClient(app) 1rtvx
47 resp = client.get( 1rtvx
48 "/query",
49 params={
50 "param": "123",
51 "param2": "456",
52 },
53 )
54 assert resp.status_code == 200 1rtvx
55 assert resp.json() == { 1rtvx
56 "param": "123",
57 "param2": "456",
58 }
61def test_header_pass_extra_list(): 1adbc
62 client = TestClient(app) 1ikmo
64 resp = client.get( 1ikmo
65 "/header",
66 headers=[
67 ("param", "123"),
68 ("param2", "456"), # Pass a list of values as extra parameter
69 ("param2", "789"),
70 ],
71 )
72 assert resp.status_code == 200 1ikmo
73 resp_json = resp.json() 1ikmo
74 assert "param2" in resp_json 1ikmo
75 assert resp_json["param2"] == ["456", "789"] 1ikmo
78def test_header_pass_extra_single(): 1adbc
79 client = TestClient(app) 1jlnp
81 resp = client.get( 1jlnp
82 "/header",
83 headers=[
84 ("param", "123"),
85 ("param2", "456"),
86 ],
87 )
88 assert resp.status_code == 200 1jlnp
89 resp_json = resp.json() 1jlnp
90 assert "param2" in resp_json 1jlnp
91 assert resp_json["param2"] == "456" 1jlnp
94def test_cookie_pass_extra_list(): 1adbc
95 client = TestClient(app) 1efgh
96 client.cookies = [ 1efgh
97 ("param", "123"),
98 ("param2", "456"), # Pass a list of values as extra parameter
99 ("param2", "789"),
100 ]
101 resp = client.get("/cookie") 1efgh
102 assert resp.status_code == 200 1efgh
103 resp_json = resp.json() 1efgh
104 assert "param2" in resp_json 1efgh
105 assert resp_json["param2"] == "789" # Cookies only keep the last value 1efgh