@@ -11,6 +11,10 @@ class Model(BaseModel):
1111 model_config = {"extra" : "allow" }
1212
1313
14+ class AuthHeaders (BaseModel ):
15+ x_user_id : str
16+
17+
1418@app .get ("/query" )
1519async def query_model_with_extra (data : Model = Query ()):
1620 return data
@@ -26,6 +30,11 @@ async def cookies_model_with_extra(data: Model = Cookie()):
2630 return data
2731
2832
33+ @app .get ("/header-requires-hyphen" )
34+ async def header_model_requires_hyphen (data : AuthHeaders = Header ()):
35+ return data
36+
37+
2938def test_query_pass_extra_list ():
3039 client = TestClient (app )
3140 resp = client .get (
@@ -91,6 +100,32 @@ def test_header_pass_extra_single():
91100 assert resp_json ["param2" ] == "456"
92101
93102
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+
94129def test_cookie_pass_extra_list ():
95130 client = TestClient (app )
96131 client .cookies = [
0 commit comments