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-02-12 18:15 +0000

1from fastapi import Cookie, FastAPI, Header, Query 1abcd

2from fastapi.testclient import TestClient 1abcd

3from pydantic import BaseModel 1abcd

4 

5app = FastAPI() 1abcd

6 

7 

8class Model(BaseModel): 1abcd

9 param: str 1abcd

10 

11 model_config = {"extra": "allow"} 1abcd

12 

13 

14@app.get("/query") 1abcd

15async def query_model_with_extra(data: Model = Query()): 1abcd

16 return data 1nopqrs

17 

18 

19@app.get("/header") 1abcd

20async def header_model_with_extra(data: Model = Header()): 1abcd

21 return data 1hijklm

22 

23 

24@app.get("/cookie") 1abcd

25async def cookies_model_with_extra(data: Model = Cookie()): 1abcd

26 return data 1efg

27 

28 

29def test_query_pass_extra_list(): 1abcd

30 client = TestClient(app) 1npr

31 resp = client.get( 1npr

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 1npr

39 assert resp.json() == { 1npr

40 "param": "123", 

41 "param2": ["456", "789"], 

42 } 

43 

44 

45def test_query_pass_extra_single(): 1abcd

46 client = TestClient(app) 1oqs

47 resp = client.get( 1oqs

48 "/query", 

49 params={ 

50 "param": "123", 

51 "param2": "456", 

52 }, 

53 ) 

54 assert resp.status_code == 200 1oqs

55 assert resp.json() == { 1oqs

56 "param": "123", 

57 "param2": "456", 

58 } 

59 

60 

61def test_header_pass_extra_list(): 1abcd

62 client = TestClient(app) 1hjl

63 

64 resp = client.get( 1hjl

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 1hjl

73 resp_json = resp.json() 1hjl

74 assert "param2" in resp_json 1hjl

75 assert resp_json["param2"] == ["456", "789"] 1hjl

76 

77 

78def test_header_pass_extra_single(): 1abcd

79 client = TestClient(app) 1ikm

80 

81 resp = client.get( 1ikm

82 "/header", 

83 headers=[ 

84 ("param", "123"), 

85 ("param2", "456"), 

86 ], 

87 ) 

88 assert resp.status_code == 200 1ikm

89 resp_json = resp.json() 1ikm

90 assert "param2" in resp_json 1ikm

91 assert resp_json["param2"] == "456" 1ikm

92 

93 

94def test_cookie_pass_extra_list(): 1abcd

95 client = TestClient(app) 1efg

96 client.cookies = [ 1efg

97 ("param", "123"), 

98 ("param2", "456"), # Pass a list of values as extra parameter 

99 ("param2", "789"), 

100 ] 

101 resp = client.get("/cookie") 1efg

102 assert resp.status_code == 200 1efg

103 resp_json = resp.json() 1efg

104 assert "param2" in resp_json 1efg

105 assert resp_json["param2"] == "789" # Cookies only keep the last value 1efg