Permalink
Browse files

Added tests for incorrect content type and size in MultiPartParser.

  • Loading branch information...
atombrella authored and timgraham committed Jun 12, 2018
1 parent 9e4f26b commit 416795910520dafd8f4b963da39385fde39c8303
Showing with 25 additions and 2 deletions.
  1. +25 −2 tests/file_uploads/tests.py
@@ -10,7 +10,9 @@
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 @@ def test_not_a_directory(self):
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 @@ def test_empty_upload_handlers(self):
'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",

0 comments on commit 4167959

Please sign in to comment.