summaryrefslogtreecommitdiff
path: root/tests/file_uploads
diff options
context:
space:
mode:
authorMads Jensen <mje@inducks.org>2018-06-12 20:42:20 +0200
committerTim Graham <timograham@gmail.com>2018-06-12 14:42:20 -0400
commit416795910520dafd8f4b963da39385fde39c8303 (patch)
treed900eae3390aa776d1096823e5149191a9feaa05 /tests/file_uploads
parent9e4f26bb40abcade301ec836aa0b84acf5f60041 (diff)
downloaddjango-416795910520dafd8f4b963da39385fde39c8303.tar.gz
Added tests for incorrect content type and size in MultiPartParser.
Diffstat (limited to 'tests/file_uploads')
-rw-r--r--tests/file_uploads/tests.py27
1 files changed, 25 insertions, 2 deletions
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",