Ticket #15496: base64-upload-with-tests.diff

File base64-upload-with-tests.diff, 3.4 KB (added by claudep, 4 years ago)
  • django/http/multipartparser.py

    diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py
    index e45d5d1..ae143f7 100644
    a b class MultiPartParser(object): 
    145145                    continue
    146146
    147147                transfer_encoding = meta_data.get('content-transfer-encoding')
     148                if transfer_encoding is not None:
     149                    transfer_encoding = transfer_encoding[0].strip()
    148150                field_name = force_unicode(field_name, encoding, errors='replace')
    149151
    150152                if item_type == FIELD:
  • tests/regressiontests/file_uploads/tests.py

    diff --git a/tests/regressiontests/file_uploads/tests.py b/tests/regressiontests/file_uploads/tests.py
    index 5da0a5f..3c126b7 100644
    a b  
    11#! -*- coding: utf-8 -*-
    22
     3import base64
    34import errno
    45import hashlib
    56import os
    class FileUploadTests(TestCase): 
    5657
    5758        self.assertEqual(response.status_code, 200)
    5859
     60    def test_base64_upload(self):
     61        test_string = "This data will be transmitted base64-encoded."
     62        payload = "\r\n".join([
     63            '--' + client.BOUNDARY,
     64            'Content-Disposition: form-data; name="file"; filename="test.txt"',
     65            'Content-Type: application/octet-stream',
     66            'Content-Transfer-Encoding: base64',
     67            '',
     68            base64.b64encode(test_string),
     69            '--' + client.BOUNDARY + '--',
     70            '',
     71        ])
     72        r = {
     73            'CONTENT_LENGTH': len(payload),
     74            'CONTENT_TYPE':   client.MULTIPART_CONTENT,
     75            'PATH_INFO':      "/file_uploads/echo_content/",
     76            'REQUEST_METHOD': 'POST',
     77            'wsgi.input':     client.FakePayload(payload),
     78        }
     79        response = self.client.request(**r)
     80        received = simplejson.loads(response.content)
     81
     82        self.assertEqual(received['file'], test_string)
     83
    5984    def test_unicode_file_name(self):
    6085        tdir = tempfile.gettempdir()
    6186
  • tests/regressiontests/file_uploads/urls.py

    diff --git a/tests/regressiontests/file_uploads/urls.py b/tests/regressiontests/file_uploads/urls.py
    index 413080e..9f814c4 100644
    a b urlpatterns = patterns('', 
    66    (r'^verify/$',          views.file_upload_view_verify),
    77    (r'^unicode_name/$',    views.file_upload_unicode_name),
    88    (r'^echo/$',            views.file_upload_echo),
     9    (r'^echo_content/$',    views.file_upload_echo_content),
    910    (r'^quota/$',           views.file_upload_quota),
    1011    (r'^quota/broken/$',    views.file_upload_quota_broken),
    1112    (r'^getlist_count/$',   views.file_upload_getlist_count),
  • tests/regressiontests/file_uploads/views.py

    diff --git a/tests/regressiontests/file_uploads/views.py b/tests/regressiontests/file_uploads/views.py
    index 0fd0b65..dba7522 100644
    a b def file_upload_echo(request): 
    8585    r = dict([(k, f.name) for k, f in request.FILES.items()])
    8686    return HttpResponse(simplejson.dumps(r))
    8787
     88def file_upload_echo_content(request):
     89    """
     90    Simple view to echo back the content of uploaded files for tests.
     91    """
     92    r = dict([(k, f.read()) for k, f in request.FILES.items()])
     93    return HttpResponse(simplejson.dumps(r))
     94
    8895def file_upload_quota(request):
    8996    """
    9097    Dynamically add in an upload handler.
Back to Top