changeset 64:bb8d993376aa

* convenience methods for multipart form * include this in test globals
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 01 Mar 2012 18:15:34 -0800
parents af1476a936fc
children 83327bc715be
files tests/multipart.py tests/test.py
diffstat 2 files changed, 14 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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'
--- 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