diff --git a/tests/file_uploads/tests.py b/tests/file_uploads/tests.py index eaf4548c0c..fb333dcf13 100644 --- a/tests/file_uploads/tests.py +++ b/tests/file_uploads/tests.py @@ -10,7 +10,9 @@ from urllib.parse import quote from django.core.files import temp as tempfile from django.core.files.uploadedfile import SimpleUploadedFile -from django.http.multipartparser import MultiPartParser, parse_header +from django.http.multipartparser import ( + MultiPartParser, MultiPartParserError, parse_header, +) from django.test import SimpleTestCase, TestCase, client, override_settings from . import uploadhandler @@ -558,7 +560,7 @@ class DirectoryCreationTests(SimpleTestCase): self.assertEqual(exc_info.exception.args[0], "%s exists and is not a directory." % UPLOAD_TO) -class MultiParserTests(unittest.TestCase): +class MultiParserTests(SimpleTestCase): def test_empty_upload_handlers(self): # We're not actually parsing here; just checking if the parser properly @@ -568,6 +570,27 @@ class MultiParserTests(unittest.TestCase): 'CONTENT_LENGTH': '1' }, StringIO('x'), [], 'utf-8') + def test_invalid_content_type(self): + with self.assertRaisesMessage(MultiPartParserError, 'Invalid Content-Type: text/plain'): + MultiPartParser({ + 'CONTENT_TYPE': 'text/plain', + 'CONTENT_LENGTH': '1', + }, StringIO('x'), [], 'utf-8') + + def test_negative_content_length(self): + with self.assertRaisesMessage(MultiPartParserError, 'Invalid content length: -1'): + MultiPartParser({ + 'CONTENT_TYPE': 'multipart/form-data; boundary=_foo', + 'CONTENT_LENGTH': -1, + }, StringIO('x'), [], 'utf-8') + + def test_bad_type_content_length(self): + multipart_parser = MultiPartParser({ + 'CONTENT_TYPE': 'multipart/form-data; boundary=_foo', + 'CONTENT_LENGTH': 'a', + }, StringIO('x'), [], 'utf-8') + self.assertEqual(multipart_parser._content_length, 0) + def test_rfc2231_parsing(self): test_data = ( (b"Content-Type: application/x-stuff; title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A",