test_exception.py 1.9 KB

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