From 249c49d6cc7c28ffd2bfef92b2402f746d24ca8b Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Sun, 25 Aug 2019 05:05:18 -0700 Subject: Modify TestApp.encode_multipart to handle bytes filenames and params. (#29) As the test shows, this fixes passing boths params and upload_files on Python 3. --- paste/fixture.py | 22 +++++++--------------- tests/test_fixture.py | 28 ++++++++++++++++++++++++++++ tests/test_wsgiwrappers.py | 2 +- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/paste/fixture.py b/paste/fixture.py index 9b2a0c4..969863c 100644 --- a/paste/fixture.py +++ b/paste/fixture.py @@ -330,28 +330,20 @@ class TestApp(object): lines = [] for key, value in params: lines.append(b'--'+boundary) - line = 'Content-Disposition: form-data; name="%s"' % key - if six.PY3: - line = line.encode('utf8') + line = b'Content-Disposition: form-data; name="%s"' % six.ensure_binary(key) lines.append(line) lines.append(b'') - line = value - if six.PY3 and isinstance(line, six.text_type): - line = line.encode('utf8') + line = six.ensure_binary(value) lines.append(line) for file_info in files: key, filename, value = self._get_file_info(file_info) lines.append(b'--'+boundary) - line = ('Content-Disposition: form-data; name="%s"; filename="%s"' - % (key, filename)) - if six.PY3: - line = line.encode('utf8') + line = (b'Content-Disposition: form-data; name="%s"; filename="%s"' + % (six.ensure_binary(key), six.ensure_binary(filename))) lines.append(line) - fcontent = mimetypes.guess_type(filename)[0] - line = ('Content-Type: %s' - % (fcontent or 'application/octet-stream')) - if six.PY3: - line = line.encode('utf8') + fcontent = mimetypes.guess_type(six.ensure_str(filename, 'ascii', 'ignore'))[0] + line = (b'Content-Type: %s' + % (fcontent.encode('ascii') if fcontent else b'application/octet-stream')) lines.append(line) lines.append(b'') lines.append(value) diff --git a/tests/test_fixture.py b/tests/test_fixture.py index 4d1ddc8..2954140 100644 --- a/tests/test_fixture.py +++ b/tests/test_fixture.py @@ -1,5 +1,8 @@ +import cgi + from paste.debug.debugapp import SimpleApplication, SlowConsumer from paste.fixture import TestApp +from paste.wsgiwrappers import WSGIRequest def test_fixture(): @@ -44,3 +47,28 @@ def test_fixture_form_end(): ('Content-Length', str(len(body)))]) return [body] TestApp(response).get('/') + +def test_params_and_upload_files(): + class PostApp(object): + def __call__(self, environ, start_response): + start_response("204 No content", []) + self.request = WSGIRequest(environ) + return [b''] + post_app = PostApp() + app = TestApp(post_app) + app.post( + '/', + params={'param1': 'a', 'param2': 'b'}, + upload_files=[ + ('file1', 'myfile.txt', b'data1'), + ('file2', b'yourfile.txt', b'data2'), + ], + ) + params = post_app.request.params + assert len(params) == 4 + assert params['param1'] == 'a' + assert params['param2'] == 'b' + assert params['file1'].value == b'data1' + assert params['file1'].filename == 'myfile.txt' + assert params['file2'].value == b'data2' + assert params['file2'].filename == 'yourfile.txt' diff --git a/tests/test_wsgiwrappers.py b/tests/test_wsgiwrappers.py index 43210c2..bfdd4b3 100644 --- a/tests/test_wsgiwrappers.py +++ b/tests/test_wsgiwrappers.py @@ -96,7 +96,7 @@ def test_wsgirequest_charset_fileupload(): return [] app = TestApp(handle_fileupload) - res = app.post('/', upload_files=[('thefile', '寿司.txt', b'Sushi')]) + res = app.post('/', upload_files=[('thefile', u'寿司.txt'.encode('utf-8'), b'Sushi')]) def test_wsgiresponse_charset(): response = WSGIResponse(mimetype='text/html; charset=UTF-8') -- cgit v1.2.1