Ticket #8622: file_upload_hang_test.diff
File file_upload_hang_test.diff, 4.5 KB (added by , 16 years ago) |
---|
-
tests/regressiontests/file_uploads/views.py
3 3 from django.http import HttpResponse, HttpResponseServerError 4 4 from django.utils import simplejson 5 5 from models import FileModel 6 from uploadhandler import QuotaUploadHandler 6 from uploadhandler import QuotaUploadHandler, ErroringUploadHandler 7 7 from django.utils.hashcompat import sha_constructor 8 8 9 9 def file_upload_view(request): … … 84 84 for key in request.FILES.keys(): 85 85 file_counts[key] = len(request.FILES.getlist(key)) 86 86 return HttpResponse(simplejson.dumps(file_counts)) 87 88 def file_upload_errors(request): 89 request.upload_handlers.insert(0, ErroringUploadHandler()) 90 return file_upload_echo(request) -
tests/regressiontests/file_uploads/tests.py
10 10 from django.utils.hashcompat import sha_constructor 11 11 12 12 from models import FileModel, temp_storage, UPLOAD_TO 13 import uploadhandler 13 14 15 16 class 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 14 28 class FileUploadTests(TestCase): 15 29 def test_simple_upload(self): 16 30 post_data = { … … 20 34 response = self.client.post('/file_uploads/upload/', post_data) 21 35 self.assertEqual(response.status_code, 200) 22 36 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 23 67 def test_large_upload(self): 24 68 tdir = tempfile.gettempdir() 25 69 -
tests/regressiontests/file_uploads/uploadhandler.py
23 23 return raw_data 24 24 25 25 def file_complete(self, file_size): 26 return None 27 No newline at end of file 26 return None 27 28 29 class CustomUploadError(Exception): 30 pass 31 32 33 class 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
8 8 (r'^quota/$', views.file_upload_quota), 9 9 (r'^quota/broken/$', views.file_upload_quota_broken), 10 10 (r'^getlist_count/$', views.file_upload_getlist_count), 11 (r'^upload_errors/$', views.file_upload_errors), 11 12 )