mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
Fixed #30422 -- Made TemporaryFileUploadHandler handle interrupted uploads.
This patch allows upload handlers to handle interrupted uploads. Co-Authored-By: Mariusz Felisiak <felisiak.mariusz@gmail.com>
This commit is contained in:
@@ -6,12 +6,13 @@ import sys
|
||||
import tempfile as sys_tempfile
|
||||
import unittest
|
||||
from io import BytesIO, StringIO
|
||||
from unittest import mock
|
||||
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, MultiPartParserError, parse_header,
|
||||
FILE, MultiPartParser, MultiPartParserError, Parser, parse_header,
|
||||
)
|
||||
from django.test import SimpleTestCase, TestCase, client, override_settings
|
||||
|
||||
@@ -443,6 +444,30 @@ class FileUploadTests(TestCase):
|
||||
temp_path = response.json()['temp_path']
|
||||
self.assertIs(os.path.exists(temp_path), False)
|
||||
|
||||
def test_upload_interrupted_temporary_file_handler(self):
|
||||
# Simulate an interrupted upload by omitting the closing boundary.
|
||||
class MockedParser(Parser):
|
||||
def __iter__(self):
|
||||
for item in super().__iter__():
|
||||
item_type, meta_data, field_stream = item
|
||||
yield item_type, meta_data, field_stream
|
||||
if item_type == FILE:
|
||||
return
|
||||
|
||||
with tempfile.NamedTemporaryFile() as temp_file:
|
||||
temp_file.write(b'a')
|
||||
temp_file.seek(0)
|
||||
with mock.patch(
|
||||
'django.http.multipartparser.Parser',
|
||||
MockedParser,
|
||||
):
|
||||
response = self.client.post(
|
||||
'/temp_file/upload_interrupted/',
|
||||
{'file': temp_file},
|
||||
)
|
||||
temp_path = response.json()['temp_path']
|
||||
self.assertIs(os.path.exists(temp_path), False)
|
||||
|
||||
def test_fileupload_getlist(self):
|
||||
file = tempfile.NamedTemporaryFile
|
||||
with file() as file1, file() as file2, file() as file2a:
|
||||
|
||||
Reference in New Issue
Block a user