1. from django.core.handlers.wsgi import WSGIHandler
    
  2. from django.test import SimpleTestCase, override_settings
    
  3. from django.test.client import (
    
  4.     BOUNDARY,
    
  5.     MULTIPART_CONTENT,
    
  6.     FakePayload,
    
  7.     encode_multipart,
    
  8. )
    
  9. 
    
  10. 
    
  11. class ExceptionHandlerTests(SimpleTestCase):
    
  12.     def get_suspicious_environ(self):
    
  13.         payload = FakePayload("a=1&a=2&a=3\r\n")
    
  14.         return {
    
  15.             "REQUEST_METHOD": "POST",
    
  16.             "CONTENT_TYPE": "application/x-www-form-urlencoded",
    
  17.             "CONTENT_LENGTH": len(payload),
    
  18.             "wsgi.input": payload,
    
  19.             "SERVER_NAME": "test",
    
  20.             "SERVER_PORT": "8000",
    
  21.         }
    
  22. 
    
  23.     @override_settings(DATA_UPLOAD_MAX_MEMORY_SIZE=12)
    
  24.     def test_data_upload_max_memory_size_exceeded(self):
    
  25.         response = WSGIHandler()(self.get_suspicious_environ(), lambda *a, **k: None)
    
  26.         self.assertEqual(response.status_code, 400)
    
  27. 
    
  28.     @override_settings(DATA_UPLOAD_MAX_NUMBER_FIELDS=2)
    
  29.     def test_data_upload_max_number_fields_exceeded(self):
    
  30.         response = WSGIHandler()(self.get_suspicious_environ(), lambda *a, **k: None)
    
  31.         self.assertEqual(response.status_code, 400)
    
  32. 
    
  33.     @override_settings(DATA_UPLOAD_MAX_NUMBER_FILES=2)
    
  34.     def test_data_upload_max_number_files_exceeded(self):
    
  35.         payload = FakePayload(
    
  36.             encode_multipart(
    
  37.                 BOUNDARY,
    
  38.                 {
    
  39.                     "a.txt": "Hello World!",
    
  40.                     "b.txt": "Hello Django!",
    
  41.                     "c.txt": "Hello Python!",
    
  42.                 },
    
  43.             )
    
  44.         )
    
  45.         environ = {
    
  46.             "REQUEST_METHOD": "POST",
    
  47.             "CONTENT_TYPE": MULTIPART_CONTENT,
    
  48.             "CONTENT_LENGTH": len(payload),
    
  49.             "wsgi.input": payload,
    
  50.             "SERVER_NAME": "test",
    
  51.             "SERVER_PORT": "8000",
    
  52.         }
    
  53. 
    
  54.         response = WSGIHandler()(environ, lambda *a, **k: None)
    
  55.         self.assertEqual(response.status_code, 400)