Code

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

File base64-upload-with-tests.diff, 3.4 KB (added by claudep, 3 years ago)
Line 
1diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py
2index e45d5d1..ae143f7 100644
3--- a/django/http/multipartparser.py
4+++ b/django/http/multipartparser.py
5@@ -145,6 +145,8 @@ class MultiPartParser(object):
6                     continue
7 
8                 transfer_encoding = meta_data.get('content-transfer-encoding')
9+                if transfer_encoding is not None:
10+                    transfer_encoding = transfer_encoding[0].strip()
11                 field_name = force_unicode(field_name, encoding, errors='replace')
12 
13                 if item_type == FIELD:
14diff --git a/tests/regressiontests/file_uploads/tests.py b/tests/regressiontests/file_uploads/tests.py
15index 5da0a5f..3c126b7 100644
16--- a/tests/regressiontests/file_uploads/tests.py
17+++ b/tests/regressiontests/file_uploads/tests.py
18@@ -1,5 +1,6 @@
19 #! -*- coding: utf-8 -*-
20 
21+import base64
22 import errno
23 import hashlib
24 import os
25@@ -56,6 +57,30 @@ class FileUploadTests(TestCase):
26 
27         self.assertEqual(response.status_code, 200)
28 
29+    def test_base64_upload(self):
30+        test_string = "This data will be transmitted base64-encoded."
31+        payload = "\r\n".join([
32+            '--' + client.BOUNDARY,
33+            'Content-Disposition: form-data; name="file"; filename="test.txt"',
34+            'Content-Type: application/octet-stream',
35+            'Content-Transfer-Encoding: base64',
36+            '',
37+            base64.b64encode(test_string),
38+            '--' + client.BOUNDARY + '--',
39+            '',
40+        ])
41+        r = {
42+            'CONTENT_LENGTH': len(payload),
43+            'CONTENT_TYPE':   client.MULTIPART_CONTENT,
44+            'PATH_INFO':      "/file_uploads/echo_content/",
45+            'REQUEST_METHOD': 'POST',
46+            'wsgi.input':     client.FakePayload(payload),
47+        }
48+        response = self.client.request(**r)
49+        received = simplejson.loads(response.content)
50+
51+        self.assertEqual(received['file'], test_string)
52+
53     def test_unicode_file_name(self):
54         tdir = tempfile.gettempdir()
55 
56diff --git a/tests/regressiontests/file_uploads/urls.py b/tests/regressiontests/file_uploads/urls.py
57index 413080e..9f814c4 100644
58--- a/tests/regressiontests/file_uploads/urls.py
59+++ b/tests/regressiontests/file_uploads/urls.py
60@@ -6,6 +6,7 @@ urlpatterns = patterns('',
61     (r'^verify/$',          views.file_upload_view_verify),
62     (r'^unicode_name/$',    views.file_upload_unicode_name),
63     (r'^echo/$',            views.file_upload_echo),
64+    (r'^echo_content/$',    views.file_upload_echo_content),
65     (r'^quota/$',           views.file_upload_quota),
66     (r'^quota/broken/$',    views.file_upload_quota_broken),
67     (r'^getlist_count/$',   views.file_upload_getlist_count),
68diff --git a/tests/regressiontests/file_uploads/views.py b/tests/regressiontests/file_uploads/views.py
69index 0fd0b65..dba7522 100644
70--- a/tests/regressiontests/file_uploads/views.py
71+++ b/tests/regressiontests/file_uploads/views.py
72@@ -85,6 +85,13 @@ def file_upload_echo(request):
73     r = dict([(k, f.name) for k, f in request.FILES.items()])
74     return HttpResponse(simplejson.dumps(r))
75 
76+def file_upload_echo_content(request):
77+    """
78+    Simple view to echo back the content of uploaded files for tests.
79+    """
80+    r = dict([(k, f.read()) for k, f in request.FILES.items()])
81+    return HttpResponse(simplejson.dumps(r))
82+
83 def file_upload_quota(request):
84     """
85     Dynamically add in an upload handler.