Ticket #8622: file_upload_hang_test.diff

File file_upload_hang_test.diff, 4.5 KB (added by vung, 16 years ago)
  • tests/regressiontests/file_uploads/views.py

     
    33from django.http import HttpResponse, HttpResponseServerError
    44from django.utils import simplejson
    55from models import FileModel
    6 from uploadhandler import QuotaUploadHandler
     6from uploadhandler import QuotaUploadHandler, ErroringUploadHandler
    77from django.utils.hashcompat import sha_constructor
    88
    99def file_upload_view(request):
     
    8484    for key in request.FILES.keys():
    8585        file_counts[key] = len(request.FILES.getlist(key))
    8686    return HttpResponse(simplejson.dumps(file_counts))
     87
     88def file_upload_errors(request):
     89    request.upload_handlers.insert(0, ErroringUploadHandler())
     90    return file_upload_echo(request)
  • tests/regressiontests/file_uploads/tests.py

     
    1010from django.utils.hashcompat import sha_constructor
    1111
    1212from models import FileModel, temp_storage, UPLOAD_TO
     13import uploadhandler
    1314
     15
     16class POSTAccessingHandler(client.ClientHandler):
     17
     18    """A handler meant to expose bug #8622."""
     19
     20    def handle_uncaught_exception(self, request, resolver, exc_info):
     21        """Extended to try to access request.POST, which should be safe to do."""
     22        ret = super(POSTAccessingHandler, self).handle_uncaught_exception(request, resolver, exc_info)
     23        # accessing request.POST should be safe:
     24        p = request.POST
     25        return ret
     26
     27
    1428class FileUploadTests(TestCase):
    1529    def test_simple_upload(self):
    1630        post_data = {
     
    2034        response = self.client.post('/file_uploads/upload/', post_data)
    2135        self.assertEqual(response.status_code, 200)
    2236
     37    def test_bug_8622(self):
     38        """The server should not block on read when there are upload errors (bug #8622)"""
     39        post_data = {
     40            'name': 'Ringo',
     41            'file_field': open(__file__),
     42        }
     43        # Maybe this is a little more complicated that it needs to be; but if
     44        # the django.test.client.FakePayload.read() implementation changes then
     45        # this test would fail.  So we need to know exactly what kind of error
     46        # it raises when there is an attempt to read more than the available bytes:
     47        try:
     48            client.FakePayload('a').read(2)
     49        except Exception, reference_error:
     50            pass
     51
     52        # install the custom handler that tries to access request.POST
     53        self.client.handler = POSTAccessingHandler()
     54
     55        try:
     56            response = self.client.post('/file_uploads/upload_errors/', post_data)
     57        except reference_error.__class__, err:
     58            block_on_read = False
     59            if str(err) == str(reference_error):
     60                block_on_read = True
     61            self.failIf(block_on_read)
     62        except Exception, err:
     63            # CustomUploadError is the error that should have been raised
     64            self.assertEqual(err.__class__, uploadhandler.CustomUploadError)
     65
     66
    2367    def test_large_upload(self):
    2468        tdir = tempfile.gettempdir()
    2569
  • tests/regressiontests/file_uploads/uploadhandler.py

     
    2323        return raw_data
    2424           
    2525    def file_complete(self, file_size):
    26         return None
    27  No newline at end of file
     26        return None
     27
     28
     29class CustomUploadError(Exception):
     30    pass
     31
     32
     33class ErroringUploadHandler(FileUploadHandler):
     34
     35    """A handler that raises a custom exception."""
     36
     37    def receive_data_chunk(self, raw_data, start):
     38        raise CustomUploadError
  • tests/regressiontests/file_uploads/urls.py

     
    88    (r'^quota/$',           views.file_upload_quota),
    99    (r'^quota/broken/$',    views.file_upload_quota_broken),
    1010    (r'^getlist_count/$',   views.file_upload_getlist_count),
     11    (r'^upload_errors/$',   views.file_upload_errors),
    1112)
Back to Top