def test_tooManyFields(self): """ Test when breaking the maximum number of fields. """ boundary = 'xyz' data = """--xyz\r Content-Disposition: form-data; name="foo"\r \r Foo Bar\r --xyz\r Content-Disposition: form-data; name="foo"\r \r Baz\r --xyz\r Content-Disposition: form-data; name="file"; filename="filename"\r Content-Type: text/html\r \r blah\r --xyz\r Content-Disposition: form-data; name="file"; filename="filename"\r Content-Type: text/plain\r \r bleh\r --xyz--\r """ s = stream.IStream(data) return self.assertFailure( fileupload.parseMultipartFormData(s, boundary, maxFields=3), fileupload.MimeFormatError)
def doTest(self, boundary, data, expected_args, expected_files): #import time, gc, cgi, cStringIO for bytes in range(1, 20): #s = TestStream(data, maxReturn=bytes) s = stream.IStream(data) #t=time.time() d = waitForDeferred(fileupload.parseMultipartFormData(s, boundary)) yield d args, files = d.getResult() #e=time.time() #print "%.2g"%(e-t) self.assertEquals(args, expected_args) # Read file data back into memory to compare. out = {} for name, l in files.items(): out[name] = [(filename, ctype, f.read()) for (filename, ctype, f) in l] self.assertEquals(out, expected_files)
def test_tooBigUpload(self): """ Test that a too big form post fails. """ boundary = '---------------------------155781040421463194511908194298' data = """-----------------------------155781040421463194511908194298\r Content-Disposition: form-data; name="foo"\r \r Foo Bar\r -----------------------------155781040421463194511908194298\r Content-Disposition: form-data; name="file"; filename="filename"\r Content-Type: text/html\r \r Contents of a file blah blah\r -----------------------------155781040421463194511908194298--\r """ s = stream.IStream(data) return self.assertFailure( fileupload.parseMultipartFormData(s, boundary, maxSize=200), fileupload.MimeFormatError)
def test_maxMem(self): """ An attachment with no filename goes to memory: check that the C{maxMem} parameter limits the size of this kind of attachment. """ boundary = '---------------------------155781040421463194511908194298' data = """-----------------------------155781040421463194511908194298\r Content-Disposition: form-data; name="foo"\r \r Foo Bar and more content\r -----------------------------155781040421463194511908194298\r Content-Disposition: form-data; name="file"; filename="filename"\r Content-Type: text/html\r \r Contents of a file blah blah\r -----------------------------155781040421463194511908194298--\r """ s = stream.IStream(data) return self.assertFailure( fileupload.parseMultipartFormData(s, boundary, maxMem=10), fileupload.MimeFormatError)