Permalink
Browse files

Added tests for multipart parser.

  • Loading branch information...
1 parent be0c4c6 commit 23822ce82e5dfcef4a49dc913308d447fdfcbab1 @mitsuhiko mitsuhiko committed Feb 18, 2009
View
@@ -12,4 +12,4 @@
def setup(app):
- app.connect('autodoc-process-docstring', cut_lines(3, -3, what=['module']))
+ app.connect('autodoc-process-docstring', cut_lines(3, 3, what=['module']))
View
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+"""
+Hacky helper application to collect form data.
+"""
+from werkzeug import Request, Response, run_simple
+
+
+def copy_stream(request):
+ from os import mkdir
+ from time import time
+ folder = 'request-%d' % time()
+ mkdir(folder)
+ environ = request.environ
+ f = file(folder + '/request.txt', 'wb+')
+ f.write(environ['wsgi.input'].read(int(environ['CONTENT_LENGTH'])))
+ f.flush()
+ f.seek(0)
+ environ['wsgi.input'] = f
+ request.stat_folder = folder
+
+
+def stats(request):
+ copy_stream(request)
+ f1 = request.files['file1']
+ f2 = request.files['file2']
+ text = request.form['text']
+ f1.save(request.stat_folder + '/file1.bin')
+ f2.save(request.stat_folder + '/file2.bin')
+ file(request.stat_folder + '/text.txt', 'w').write(text.encode('utf-8'))
+ return Response('Done.')
+
+
+def upload_file(request):
+ return Response('''
+ <h1>Upload File</h1>
+ <form action="" method="post" enctype="multipart/form-data">
+ <input type="file" name="file1"><br>
+ <input type="file" name="file2"><br>
+ <input type="text" name="text"><br>
+ <input type="submit" value="Send">
+ </form>
+ ''', mimetype='text/html')
+
+
+def application(environ, start_responseonse):
+ request = Request(environ)
+ if request.method == 'POST':
+ response = stats(request)
+ else:
+ response = upload_file(request)
+ return response(environ, start_responseonse)
+
+
+if __name__ == '__main__':
+ run_simple('localhost', 5000, application, use_debugger=True)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
@@ -0,0 +1 @@
+example text
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
@@ -0,0 +1 @@
+blafasel öäü
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
@@ -0,0 +1 @@
+this is another text with ümläüts
View
@@ -0,0 +1,64 @@
+# -*- coding: utf-8 -*-
+from os.path import join, dirname, abspath
+from werkzeug import Client, Request, Response
+
+
+@Request.application
+def form_data_consumer(request):
+ result_object = request.args['object']
+ if result_object == 'text':
+ return Response(repr(request.form['text']))
+ f = request.files[result_object]
+ return Response('\n'.join((
+ repr(f.filename),
+ repr(f.name),
+ repr(f.content_type),
+ f.stream.read()
+ )))
+
+
+def get_contents(filename):
+ f = file(filename, 'rb')
+ try:
+ return f.read()
+ finally:
+ f.close()
+
+
+def test_multipart():
+ """Tests multipart parsing against data collected from webbrowsers"""
+ resources = join(dirname(__file__), 'multipart')
+
+ client = Client(form_data_consumer, Response)
+
+ repository = [
+ ('firefox3-2png1txt', '---------------------------186454651713519341951581030105', [
+ (u'anchor.png', 'file1', 'image/png', 'file1.png'),
+ (u'application_edit.png', 'file2', 'image/png', 'file2.png')
+ ], u'example text'),
+ ('opera8-2png1txt', '----------zEO9jQKmLc2Cq88c23Dx19', [
+ (u'arrow_branch.png', 'file1', 'image/png', 'file1.png'),
+ (u'award_star_bronze_1.png', 'file2', 'image/png', 'file2.png')
+ ], u'blafasel öäü'),
+ ('webkit3-2png1txt', '----WebKitFormBoundaryjdSFhcARk8fyGNy6', [
+ (u'gtk-apply.png', 'file1', 'image/png', 'file1.png'),
+ (u'gtk-no.png', 'file2', 'image/png', 'file2.png')
+ ], u'this is another text with ümläüts')
+ ]
+
+ for name, boundary, files, text in repository:
+ folder = join(resources, name)
+ data = get_contents(join(folder, 'request.txt'))
+ for filename, field, content_type, fsname in files:
+ response = client.post('/?object=' + field, data=data, content_type=
+ 'multipart/form-data; boundary="%s"' % boundary,
+ content_length=len(data))
+ lines = response.data.split('\n', 3)
+ assert lines[0] == repr(filename)
+ assert lines[1] == repr(field)
+ assert lines[2] == repr(content_type)
+ assert lines[3] == get_contents(join(folder, fsname))
+ response = client.post('/?object=text', data=data, content_type=
+ 'multipart/form-data; boundary="%s"' % boundary,
+ content_length=len(data))
+ assert response.data == repr(text)
View
@@ -4,9 +4,7 @@
~~~~~~~~~~~~~~~~~~~~~
This module provides a class called :class:`AtomFeed` which can be
- used to generate feeds in the Atom syndication format (see `RFC 4287`_).
-
- .. _RFC 4287: http://tools.ietf.org/html/rfc4287
+ used to generate feeds in the Atom syndication format (see :rfc:`4287`).
Example::
View
@@ -1621,10 +1621,10 @@ def parse_form_data(environ, stream_factory=None, charset='utf-8',
extra.get('boundary'),
content_length, stream_factory,
charset, errors)
- except ValueError:
- # just ignore parsing errors
+ except ValueError, e:
pass
- elif content_type == 'application/x-www-form-urlencoded':
+ elif content_type == 'application/x-www-form-urlencoded' or \
+ content_type == 'application/x-url-encoded':
form = url_decode(environ['wsgi.input'].read(content_length),
charset, errors=errors)
else:

0 comments on commit 23822ce

Please sign in to comment.