# HG changeset patch # User Jeff Hammel # Date 1330654534 28800 # Node ID bb8d993376aa890855caed256100642b6360d7e7 # Parent af1476a936fc7efceff7fd2bcd2a4ae3ed6a1e28 * convenience methods for multipart form * include this in test globals diff -r af1476a936fc -r bb8d993376aa tests/multipart.py --- a/tests/multipart.py Thu Mar 01 16:57:00 2012 -0800 +++ b/tests/multipart.py Thu Mar 01 18:15:34 2012 -0800 @@ -8,9 +8,10 @@ import itertools import mimetools import mimetypes -from cStringIO import StringIO +import os import urllib import urllib2 +from cStringIO import StringIO class MultiPartForm(object): """Accumulate the data to be used when posting a form.""" @@ -27,8 +28,14 @@ """Add a simple field to the form data.""" self.form_fields.append((name, value)) - def add_file(self, fieldname, filename, fileHandle, mimetype=None): - """Add a file to be uploaded.""" + def add_file(self, fieldname, path, mimetype=None): + filename = os.path.basename(path) + f = file(path) + self.add_file_obj(fieldname, filename, f, mimetype) + f.close() + + def add_file_obj(self, fieldname, filename, fileHandle, mimetype=None): + """Add a file object to be uploaded.""" body = fileHandle.read() if mimetype is None: mimetype = mimetypes.guess_type(filename)[0] or 'application/octet-stream' diff -r af1476a936fc -r bb8d993376aa tests/test.py --- a/tests/test.py Thu Mar 01 16:57:00 2012 -0800 +++ b/tests/test.py Thu Mar 01 18:15:34 2012 -0800 @@ -9,6 +9,7 @@ # http://pythonpaste.org/testing-applications.html import doctest +import multipart import os import shutil import sys @@ -35,7 +36,9 @@ directory = os.path.dirname(os.path.abspath(__file__)) extraglobs = {'here': directory, 'create_virtualenv': create_virtualenv, - 'testserver': testserver.TestWSGIServer } + 'testserver': testserver.TestWSGIServer, + 'MultiPartForm': multipart.MultiPartForm + } doctest_args = dict(extraglobs=extraglobs, raise_on_error=raise_on_error) if report_first: doctest_args['optionflags'] = doctest.REPORT_ONLY_FIRST_FAILURE