diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2008-08-30 19:56:14 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2008-08-30 19:56:14 +0000 |
| commit | 15644cb255f673c07d9bafc3595edaaa4cdc3a90 (patch) | |
| tree | 1fa3fee74606b352a9eb73f9f08deb169411608a /tests | |
| parent | 7c65a316060f88a8604d51c7bc20945fae476c1a (diff) | |
Fixed #8622: accessing POST after a POST handling exception no longer throws the server into an infinite loop. Thanks to vung for tracking this one down and fixing it.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8748 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/regressiontests/file_uploads/tests.py | 42 | ||||
| -rw-r--r-- | tests/regressiontests/file_uploads/uploadhandler.py | 10 | ||||
| -rw-r--r-- | tests/regressiontests/file_uploads/urls.py | 1 | ||||
| -rw-r--r-- | tests/regressiontests/file_uploads/views.py | 6 |
4 files changed, 57 insertions, 2 deletions
diff --git a/tests/regressiontests/file_uploads/tests.py b/tests/regressiontests/file_uploads/tests.py index 7c8b53ea89..6fcd8a99aa 100644 --- a/tests/regressiontests/file_uploads/tests.py +++ b/tests/regressiontests/file_uploads/tests.py @@ -10,6 +10,7 @@ from django.utils import simplejson from django.utils.hashcompat import sha_constructor from models import FileModel, temp_storage, UPLOAD_TO +import uploadhandler class FileUploadTests(TestCase): def test_simple_upload(self): @@ -187,6 +188,47 @@ class FileUploadTests(TestCase): self.assertEqual(got.get('file1'), 1) self.assertEqual(got.get('file2'), 2) + def test_file_error_blocking(self): + """ + The server should not block when there are upload errors (bug #8622). + This can happen if something -- i.e. an exception handler -- tries to + access POST while handling an error in parsing POST. This shouldn't + cause an infinite loop! + """ + class POSTAccessingHandler(client.ClientHandler): + """A handler that'll access POST during an exception.""" + def handle_uncaught_exception(self, request, resolver, exc_info): + ret = super(POSTAccessingHandler, self).handle_uncaught_exception(request, resolver, exc_info) + p = request.POST + return ret + + post_data = { + 'name': 'Ringo', + 'file_field': open(__file__), + } + # Maybe this is a little more complicated that it needs to be; but if + # the django.test.client.FakePayload.read() implementation changes then + # this test would fail. So we need to know exactly what kind of error + # it raises when there is an attempt to read more than the available bytes: + try: + client.FakePayload('a').read(2) + except Exception, reference_error: + pass + + # install the custom handler that tries to access request.POST + self.client.handler = POSTAccessingHandler() + + try: + response = self.client.post('/file_uploads/upload_errors/', post_data) + except reference_error.__class__, err: + self.failIf( + str(err) == str(reference_error), + "Caught a repeated exception that'll cause an infinite loop in file uploads." + ) + except Exception, err: + # CustomUploadError is the error that should have been raised + self.assertEqual(err.__class__, uploadhandler.CustomUploadError) + class DirectoryCreationTests(unittest.TestCase): """ Tests for error handling during directory creation diff --git a/tests/regressiontests/file_uploads/uploadhandler.py b/tests/regressiontests/file_uploads/uploadhandler.py index 54f82f626c..9f3960a8d8 100644 --- a/tests/regressiontests/file_uploads/uploadhandler.py +++ b/tests/regressiontests/file_uploads/uploadhandler.py @@ -23,4 +23,12 @@ class QuotaUploadHandler(FileUploadHandler): return raw_data def file_complete(self, file_size): - return None
\ No newline at end of file + return None + +class CustomUploadError(Exception): + pass + +class ErroringUploadHandler(FileUploadHandler): + """A handler that raises an exception.""" + def receive_data_chunk(self, raw_data, start): + raise CustomUploadError("Oops!") diff --git a/tests/regressiontests/file_uploads/urls.py b/tests/regressiontests/file_uploads/urls.py index cc4cc80fdd..607e1d1034 100644 --- a/tests/regressiontests/file_uploads/urls.py +++ b/tests/regressiontests/file_uploads/urls.py @@ -8,4 +8,5 @@ urlpatterns = patterns('', (r'^quota/$', views.file_upload_quota), (r'^quota/broken/$', views.file_upload_quota_broken), (r'^getlist_count/$', views.file_upload_getlist_count), + (r'^upload_errors/$', views.file_upload_errors), ) diff --git a/tests/regressiontests/file_uploads/views.py b/tests/regressiontests/file_uploads/views.py index b1de169cf0..a989069a30 100644 --- a/tests/regressiontests/file_uploads/views.py +++ b/tests/regressiontests/file_uploads/views.py @@ -3,7 +3,7 @@ from django.core.files.uploadedfile import UploadedFile from django.http import HttpResponse, HttpResponseServerError from django.utils import simplejson from models import FileModel -from uploadhandler import QuotaUploadHandler +from uploadhandler import QuotaUploadHandler, ErroringUploadHandler from django.utils.hashcompat import sha_constructor def file_upload_view(request): @@ -84,3 +84,7 @@ def file_upload_getlist_count(request): for key in request.FILES.keys(): file_counts[key] = len(request.FILES.getlist(key)) return HttpResponse(simplejson.dumps(file_counts)) + +def file_upload_errors(request): + request.upload_handlers.insert(0, ErroringUploadHandler()) + return file_upload_echo(request) |
