@@ -97,3 +97,55 @@ def tests_exception_to_response(skip_details: bool, error_code: ErrorCodeStr | N
9797 assert response_json ["error" ]["message" ] == expected_reason
9898 assert response_json ["error" ]["supportId" ] == error_code
9999 assert response_json ["error" ]["status" ] == response .status
100+
101+
102+ @pytest .mark .parametrize (
103+ "input_message, expected_output" ,
104+ [
105+ (None , None ), # None input returns None
106+ ("" , None ), # Empty string returns None
107+ ("Simple message" , "Simple message" ), # Simple message stays the same
108+ (
109+ "Message\n with\n newlines" ,
110+ "Message with newlines" ,
111+ ), # Newlines are replaced with spaces
112+ ("A" * 2000 , "A" * 1500 ), # Long message gets truncated to max_length (1500)
113+ (
114+ "Line1\n Line2\n Line3" + "X" * 1500 ,
115+ "Line1 Line2 Line3" + "X" * 1483 ,
116+ ), # Combined case: newlines and truncation
117+ ],
118+ ids = [
119+ "none_input" ,
120+ "empty_string" ,
121+ "simple_message" ,
122+ "newlines_replaced" ,
123+ "long_message_truncated" ,
124+ "newlines_and_truncation" ,
125+ ],
126+ )
127+ def test_safe_status_message (input_message : str | None , expected_output : str | None ):
128+ from servicelib .aiohttp .rest_responses import safe_status_message
129+
130+ result = safe_status_message (input_message )
131+ assert result == expected_output
132+
133+ # Test with custom max_length
134+ custom_max = 10
135+ result_custom = safe_status_message (input_message , max_length = custom_max )
136+
137+ # Check length constraint is respected
138+ if result_custom is not None :
139+ assert len (result_custom ) <= custom_max
140+
141+ # Verify it can be used in a web response without raising exceptions
142+ try :
143+ # This would fail with long or multiline reasons
144+ if result is not None :
145+ web .Response (reason = result )
146+
147+ # Test with custom length result too
148+ if result_custom is not None :
149+ web .Response (reason = result_custom )
150+ except ValueError :
151+ pytest .fail ("safe_status_message result caused an exception in web.Response" )
0 commit comments