2929
3030sys .path .append ("../common" )
3131
32+ import json
3233import unittest
3334
3435import numpy as np
3940# Each FP32 value is 4 bytes, so we need to divide target byte sizes by 4 to get element counts
4041BYTES_PER_FP32 = 4
4142MB = 2 ** 20 # 1 MB = 1,048,576 bytes
43+ GB = 2 ** 30 # 1 GB = 1,073,741,824 bytes
4244DEFAULT_LIMIT_BYTES = 64 * MB # 64MB default limit
4345INCREASED_LIMIT_BYTES = 128 * MB # 128MB increased limit
4446
@@ -167,8 +169,11 @@ def test_default_limit_rejection_json(self):
167169 )
168170
169171 # Test case 2: Input just under the 64MB limit (should succeed)
170- # (2^24 - 32) elements * 4 bytes = 64MB - 128 bytes = 67,108,736 bytes
171- shape_size = DEFAULT_LIMIT_ELEMENTS - OFFSET_ELEMENTS
172+ # The test creates a JSON payload with data, which adds overhead compared
173+ # to raw binary format. We adjust the shape size to ensure the final
174+ # JSON payload is under the size limit. An element is roughly 5
175+ # bytes in JSON, compared to 4 bytes as a raw FP32.
176+ shape_size = (DEFAULT_LIMIT_ELEMENTS - OFFSET_ELEMENTS ) * 4 // 5
172177
173178 payload = {
174179 "inputs" : [
@@ -180,9 +185,8 @@ def test_default_limit_rejection_json(self):
180185 }
181186 ]
182187 }
183- assert (
184- shape_size * BYTES_PER_FP32 < 64 * MB
185- ) # Verify we're actually under the 64MB limit
188+ # Verify we're actually under the 64MB limit
189+ self .assertLess (len (json .dumps (payload ).encode ("utf-8" )), DEFAULT_LIMIT_BYTES )
186190
187191 response = requests .post (
188192 self ._get_infer_url (model ), headers = headers , json = payload
@@ -320,8 +324,11 @@ def test_large_input_json(self):
320324 )
321325
322326 # Test case 2: Input just under the 128MB configured limit (should succeed)
323- # (2^25 - 32) elements * 4 bytes = 128MB - 128 bytes = 134,217,600 bytes
324- shape_size = INCREASED_LIMIT_ELEMENTS - OFFSET_ELEMENTS
327+ # The test creates a JSON payload with data, which adds overhead compared
328+ # to raw binary format. We adjust the shape size to ensure the final
329+ # JSON payload is under the size limit. An element is roughly 5
330+ # bytes in JSON, compared to 4 bytes as a raw FP32.
331+ shape_size = (INCREASED_LIMIT_ELEMENTS - OFFSET_ELEMENTS ) * 4 // 5
325332
326333 payload = {
327334 "inputs" : [
@@ -333,9 +340,8 @@ def test_large_input_json(self):
333340 }
334341 ]
335342 }
336- assert (
337- shape_size * BYTES_PER_FP32 < 128 * MB
338- ) # Verify we're actually under the 128MB limit
343+ # Verify we're actually under the 128MB limit
344+ self .assertLess (len (json .dumps (payload ).encode ("utf-8" )), INCREASED_LIMIT_BYTES )
339345
340346 response = requests .post (
341347 self ._get_infer_url (model ), headers = headers , json = payload
@@ -360,6 +366,55 @@ def test_large_input_json(self):
360366 f"Expected shape { [1 , shape_size ]} , got { result ['outputs' ][0 ]['shape' ]} " ,
361367 )
362368
369+ def test_large_string_in_json (self ):
370+ """Test JSON request with large string input"""
371+ model = "simple_identity"
372+
373+ # Create a string that is larger (large payload about 2GB) than the default limit of 64MB
374+ # (2^31 + 64) elements * 1 bytes = 2GB + 64 bytes = 2,147,483,712 bytes
375+ large_string_size = 2 * GB + 64
376+ large_string = "A" * large_string_size
377+
378+ payload = {
379+ "inputs" : [
380+ {
381+ "name" : "INPUT0" ,
382+ "datatype" : "BYTES" ,
383+ "shape" : [1 , 1 ],
384+ "data" : [large_string ],
385+ }
386+ ]
387+ }
388+
389+ headers = {"Content-Type" : "application/json" }
390+ response = requests .post (
391+ self ._get_infer_url (model ), headers = headers , json = payload
392+ )
393+
394+ # Should fail with 400 bad request
395+ self .assertEqual (
396+ 400 ,
397+ response .status_code ,
398+ "Expected error code for oversized JSON request, got: {}" .format (
399+ response .status_code
400+ ),
401+ )
402+
403+ # Verify error message
404+ error_msg = response .content .decode ()
405+ self .assertIn (
406+ "Request JSON size" ,
407+ error_msg ,
408+ )
409+ self .assertIn (
410+ "exceeds the maximum allowed value" ,
411+ error_msg ,
412+ )
413+ self .assertIn (
414+ "Use --http-max-input-size to increase the limit" ,
415+ error_msg ,
416+ )
417+
363418
364419if __name__ == "__main__" :
365420 unittest .main ()
0 commit comments