@@ -34,6 +34,7 @@ def test_default_configuration(self) -> None:
3434 assert config .event_type == "org.eoapi.stac"
3535 assert config .timeout == 30.0
3636 assert config .max_retries == 3
37+ assert config .max_header_length == 2048
3738
3839 def test_endpoint_validation_error (self ) -> None :
3940 """Test endpoint validation."""
@@ -198,6 +199,62 @@ def test_convert_to_cloudevent(
198199 assert cloud_event ["subject" ] == "test-item"
199200 assert cloud_event ["collection" ] == "test-collection"
200201
202+ def test_truncate_header (self , adapter : CloudEventsAdapter ) -> None :
203+ """Test header value truncation."""
204+ # Short string should not be truncated
205+ short = "short-string"
206+ assert adapter ._truncate_header (short ) == short
207+
208+ # None should remain None
209+ assert adapter ._truncate_header (None ) is None
210+
211+ # Long string should be truncated to max_header_length bytes
212+ long_string = "a" * 3000
213+ truncated = adapter ._truncate_header (long_string )
214+ assert truncated is not None
215+ assert len (truncated .encode ("utf-8" )) <= adapter .config .max_header_length
216+ assert len (truncated ) <= adapter .config .max_header_length
217+
218+ # UTF-8 multi-byte characters should be handled correctly
219+ unicode_string = "测试" * 1000 # Chinese characters (3 bytes each)
220+ truncated_unicode = adapter ._truncate_header (unicode_string )
221+ assert truncated_unicode is not None
222+ assert (
223+ len (truncated_unicode .encode ("utf-8" )) <= adapter .config .max_header_length
224+ )
225+ # Should not break in the middle of a character
226+ assert truncated_unicode .encode ("utf-8" ).decode ("utf-8" ) == truncated_unicode
227+
228+ def test_convert_to_cloudevent_with_long_headers (
229+ self , config : CloudEventsConfig
230+ ) -> None :
231+ """Test CloudEvent conversion with long header values."""
232+ config .max_header_length = 50 # Small limit for testing
233+ adapter = CloudEventsAdapter (config )
234+
235+ # Create event with long item_id and collection
236+ event = NotificationEvent (
237+ source = "/test/source" ,
238+ type = "test.type" ,
239+ operation = "INSERT" ,
240+ collection = "a-very-long-collection-name-that-exceeds-the-limit" ,
241+ item_id = "a-very-long-item-id-that-also-exceeds-the-configured-limit" ,
242+ )
243+
244+ cloud_event = adapter ._convert_to_cloudevent (event )
245+
246+ # Check that long values are truncated in headers
247+ assert "subject" in cloud_event
248+ assert "collection" in cloud_event
249+ assert len (cloud_event ["subject" ].encode ("utf-8" )) <= config .max_header_length
250+ assert (
251+ len (cloud_event ["collection" ].encode ("utf-8" )) <= config .max_header_length
252+ )
253+
254+ # Original values should still be in data payload
255+ assert cloud_event .data ["item_id" ] == event .item_id
256+ assert cloud_event .data ["collection" ] == event .collection
257+
201258 def test_operation_mapping (self , adapter : CloudEventsAdapter ) -> None :
202259 """Test operation to event type mapping."""
203260 test_cases = [
0 commit comments