@@ -111,14 +111,25 @@ async def test_gateway_update_add_multi_headers(self):
111111 assert decoded ["X-New-Header" ] == "new_value"
112112
113113 @pytest .mark .asyncio
114- async def test_special_characters_in_headers (self ):
115- """Test headers with special characters."""
114+ async def test_special_characters_in_headers_rejected (self ):
115+ """Test headers with invalid special characters are rejected ."""
116116 auth_headers = [{"key" : "X-Special-!@#" , "value" : "value-with-特殊字符" }, {"key" : "Content-Type" , "value" : "application/json; charset=utf-8" }]
117117
118+ with pytest .raises (ValidationError ) as exc_info :
119+ GatewayCreate (name = "Test Gateway" , url = "http://example.com" , auth_type = "authheaders" , auth_headers = auth_headers )
120+
121+ assert "Invalid header key format" in str (exc_info .value )
122+ assert "X-Special-!@#" in str (exc_info .value )
123+
124+ @pytest .mark .asyncio
125+ async def test_valid_special_characters_in_values (self ):
126+ """Test headers with special characters in values (allowed) but valid keys."""
127+ auth_headers = [{"key" : "X-Special-Header" , "value" : "value-with-特殊字符" }, {"key" : "Content-Type" , "value" : "application/json; charset=utf-8" }]
128+
118129 gateway = GatewayCreate (name = "Test Gateway" , url = "http://example.com" , auth_type = "authheaders" , auth_headers = auth_headers )
119130
120131 decoded = decode_auth (gateway .auth_value )
121- assert decoded ["X-Special-!@# " ] == "value-with-特殊字符"
132+ assert decoded ["X-Special-Header " ] == "value-with-特殊字符"
122133 assert decoded ["Content-Type" ] == "application/json; charset=utf-8"
123134
124135 @pytest .mark .asyncio
@@ -169,3 +180,77 @@ async def test_authorization_header_in_multi_headers(self):
169180 decoded = decode_auth (gateway .auth_value )
170181 assert decoded ["Authorization" ] == "Bearer token123"
171182 assert decoded ["X-API-Key" ] == "secret"
183+
184+ @pytest .mark .asyncio
185+ async def test_gateway_create_invalid_header_key_format (self ):
186+ """Test creating gateway with invalid header key format."""
187+ auth_headers = [{"key" : "Invalid@Key!" , "value" : "secret123" }]
188+
189+ with pytest .raises (ValidationError ) as exc_info :
190+ GatewayCreate (name = "Test Gateway" , url = "http://example.com" , auth_type = "authheaders" , auth_headers = auth_headers )
191+
192+ assert "Invalid header key format" in str (exc_info .value )
193+
194+ @pytest .mark .asyncio
195+ async def test_gateway_create_excessive_headers (self ):
196+ """Test creating gateway with more than 100 headers."""
197+ auth_headers = [{"key" : f"X-Header-{ i } " , "value" : f"value-{ i } " } for i in range (101 )]
198+
199+ with pytest .raises (ValidationError ) as exc_info :
200+ GatewayCreate (name = "Test Gateway" , url = "http://example.com" , auth_type = "authheaders" , auth_headers = auth_headers )
201+
202+ assert "Maximum of 100 headers allowed" in str (exc_info .value )
203+
204+ @pytest .mark .asyncio
205+ async def test_gateway_create_duplicate_keys_with_warning (self , caplog ):
206+ """Test creating gateway with duplicate header keys logs warning."""
207+ auth_headers = [
208+ {"key" : "X-API-Key" , "value" : "first_value" },
209+ {"key" : "X-API-Key" , "value" : "second_value" }, # Duplicate
210+ {"key" : "X-Client-ID" , "value" : "client123" }
211+ ]
212+
213+ gateway = GatewayCreate (name = "Test Gateway" , url = "http://example.com" , auth_type = "authheaders" , auth_headers = auth_headers )
214+
215+ # Check that duplicate warning was logged
216+ assert "Duplicate header keys detected" in caplog .text
217+ assert "X-API-Key" in caplog .text
218+
219+ # Check that last value wins
220+ decoded = decode_auth (gateway .auth_value )
221+ assert decoded ["X-API-Key" ] == "second_value"
222+ assert decoded ["X-Client-ID" ] == "client123"
223+
224+ @pytest .mark .asyncio
225+ async def test_gateway_create_mixed_valid_invalid_keys (self ):
226+ """Test creating gateway with mixed valid and invalid header keys."""
227+ auth_headers = [
228+ {"key" : "Valid-Header" , "value" : "test123" },
229+ {"key" : "Invalid@Key!" , "value" : "should_fail" } # This should fail validation
230+ ]
231+
232+ with pytest .raises (ValidationError ) as exc_info :
233+ GatewayCreate (name = "Test Gateway" , url = "http://example.com" , auth_type = "authheaders" , auth_headers = auth_headers )
234+
235+ assert "Invalid header key format" in str (exc_info .value )
236+ assert "Invalid@Key!" in str (exc_info .value )
237+
238+ @pytest .mark .asyncio
239+ async def test_gateway_create_edge_case_header_keys (self ):
240+ """Test creating gateway with edge case header keys."""
241+ # Test valid edge cases
242+ auth_headers = [
243+ {"key" : "X-API-Key" , "value" : "test1" }, # Standard format
244+ {"key" : "X_API_KEY" , "value" : "test2" }, # Underscores allowed
245+ {"key" : "API-Key-123" , "value" : "test3" }, # Numbers and hyphens
246+ {"key" : "UPPERCASE" , "value" : "test4" }, # Uppercase
247+ {"key" : "lowercase" , "value" : "test5" } # Lowercase
248+ ]
249+
250+ gateway = GatewayCreate (name = "Test Gateway" , url = "http://example.com" , auth_type = "authheaders" , auth_headers = auth_headers )
251+
252+ decoded = decode_auth (gateway .auth_value )
253+ assert len (decoded ) == 5
254+ assert decoded ["X-API-Key" ] == "test1"
255+ assert decoded ["X_API_KEY" ] == "test2"
256+ assert decoded ["API-Key-123" ] == "test3"
0 commit comments