diff tests/multipart.py @ 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
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'