Fixed #7143 -- Modified the test client to better match (most) real browser behavior when uploading files. Now, only the file name is sent, rather than the full path. Thanks for the report, cpinto.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7577 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2008-06-06 13:39:42 +00:00
parent d234e92740
commit 46cd8bb5b6
2 changed files with 8 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import urllib import urllib
import sys import sys
import os
from cStringIO import StringIO from cStringIO import StringIO
from django.conf import settings from django.conf import settings
from django.contrib.auth import authenticate, login from django.contrib.auth import authenticate, login
@ -67,7 +68,7 @@ def encode_multipart(boundary, data):
if isinstance(value, file): if isinstance(value, file):
lines.extend([ lines.extend([
'--' + boundary, '--' + boundary,
'Content-Disposition: form-data; name="%s"; filename="%s"' % (to_str(key), to_str(value.name)), 'Content-Disposition: form-data; name="%s"; filename="%s"' % (to_str(key), to_str(os.path.basename(value.name))),
'Content-Type: application/octet-stream', 'Content-Type: application/octet-stream',
'', '',
value.read() value.read()

View File

@ -1,3 +1,5 @@
import os
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError from django.http import HttpResponse, HttpResponseRedirect, HttpResponseServerError
@ -13,6 +15,10 @@ def file_upload_view(request):
form_data = request.POST.copy() form_data = request.POST.copy()
form_data.update(request.FILES) form_data.update(request.FILES)
if isinstance(form_data['file_field'], dict) and isinstance(form_data['name'], unicode): if isinstance(form_data['file_field'], dict) and isinstance(form_data['name'], unicode):
# If a file is posted, the dummy client should only post the file name,
# not the full path.
if os.path.dirname(form_data['file_field']['filename']) != '':
return HttpResponseServerError()
return HttpResponse('') return HttpResponse('')
else: else:
return HttpResponseServerError() return HttpResponseServerError()