def test_from_params(self): fp = open("tests/poster/test_encode.py") expected = [ encode.MultipartParam("foo", "bar"), encode.MultipartParam("baz", fileobj=fp, filename=fp.name, filetype=mimetypes.guess_type(fp.name)[0]) ] self.assertEqual( encode.MultipartParam.from_params([("foo", "bar"), ("baz", fp)]), expected) self.assertEqual( encode.MultipartParam.from_params((("foo", "bar"), ("baz", fp))), expected) self.assertEqual( encode.MultipartParam.from_params({ "foo": "bar", "baz": fp }), expected) self.assertEqual( encode.MultipartParam.from_params([expected[0], expected[1]]), expected)
def test_from_params_dict(self): p = encode.MultipartParam('file', fileobj=open("tests/poster/test_encode.py")) params = {"foo": "bar", "file": p} expected = [encode.MultipartParam("foo", "bar"), p] retval = encode.MultipartParam.from_params(params) expected.sort() retval.sort() self.assertEqual(retval, expected)
def test_simple(self): p = encode.MultipartParam("foo", "bar") boundary = "XYZXYZXYZ" expected = unix2dos(b"""--XYZXYZXYZ Content-Disposition: form-data; name="foo" Content-Type: text/plain; charset=utf-8 bar --XYZXYZXYZ-- """) self.assertEqual(p.encode(boundary), expected[:-len(boundary) - 6]) self.assertEqual(p.get_size(boundary), len(expected) - len(boundary) - 6) self.assertEqual(encode.get_body_size([p], boundary), len(expected)) self.assertEqual( encode.get_headers([p], boundary), { 'Content-Length': str(len(expected)), 'Content-Type': 'multipart/form-data; boundary=%s' % boundary }) datagen, headers = encode.multipart_encode([p], boundary) self.assertEqual( headers, { 'Content-Length': str(len(expected)), 'Content-Type': 'multipart/form-data; boundary=%s' % boundary }) self.assertEqual(b"".join(datagen), expected)
def test_from_params_assertion(self): p = encode.MultipartParam('file', fileobj=open("tests/poster/test_encode.py")) params = {"foo": "bar", "baz": p} self.assertRaises(AssertionError, encode.MultipartParam.from_params, params)
def test_MultipartParam_file_cb(self): log = [] def cb(p, current, total): log.append((p, current, total)) p = encode.MultipartParam("foo", fileobj=open(__file__, 'rb'), cb=cb) boundary = encode.gen_boundary() content = b''.join(list(p.iter_encode(boundary))) l = p.get_size(boundary) self.assertEquals(log[-1], (p, l, l))
def test_multipart_encode_cb(self): log = [] def cb(p, current, total): log.append((p, current, total)) p = encode.MultipartParam("foo", "bar") boundary = "XYZXYZXYZ" datagen, headers = encode.multipart_encode([p], boundary, cb=cb) b"".join(datagen) l = int(headers['Content-Length']) self.assertEquals(log[-1], (None, l, l))
def test_MultipartParam_cb(self): log = [] def cb(p, current, total): log.append((p, current, total)) p = encode.MultipartParam("foo", "bar", cb=cb) boundary = "XYZXYZXYZ" datagen, headers = encode.multipart_encode([p], boundary) b"".join(datagen) l = p.get_size(boundary) self.assertEquals(log[-1], (p, l, l))
def test_reset_string(self): p = encode.MultipartParam("foo", "bar") boundary = "XYZXYZXYZ" datagen, headers = encode.multipart_encode([p], boundary) expected = unix2dos(b"""--XYZXYZXYZ Content-Disposition: form-data; name="foo" Content-Type: text/plain; charset=utf-8 bar --XYZXYZXYZ-- """) self.assertEquals(b"".join(datagen), expected) datagen.reset() self.assertEquals(b"".join(datagen), expected)