예제 #1
0
    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)
예제 #2
0
    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)
예제 #3
0
    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)
예제 #4
0
    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)
예제 #5
0
    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))
예제 #6
0
    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))
예제 #7
0
    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))
예제 #8
0
    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)