def test_lazy_load_twice(self):
        # This test is sad and confusing. Need to figure out what's
        # going on with partial reads and socket reuse.

        pool = HTTPConnectionPool(self.host, self.port, block=True, maxsize=1, timeout=2)

        payload_size = 1024 * 2
        first_chunk = 512

        boundary = "foo"

        req_data = {"count": "a" * payload_size}
        resp_data = encode_multipart_formdata(req_data, boundary=boundary)[0]

        req2_data = {"count": "b" * payload_size}
        resp2_data = encode_multipart_formdata(req2_data, boundary=boundary)[0]

        r1 = yield From(
            pool.request("POST", "/echo", fields=req_data, multipart_boundary=boundary, preload_content=False)
        )

        self.assertEqual(r1.read(first_chunk), resp_data[:first_chunk])

        try:
            r2 = yield From(
                pool.request(
                    "POST",
                    "/echo",
                    fields=req2_data,
                    multipart_boundary=boundary,
                    preload_content=False,
                    pool_timeout=0.001,
                )
            )

            # This branch should generally bail here, but maybe someday it will
            # work? Perhaps by some sort of magic. Consider it a TODO.

            self.assertEqual(r2.read(first_chunk), resp2_data[:first_chunk])

            self.assertEqual((yield From(r1.read()), resp_data[first_chunk:]))
            self.assertEqual((yield From(r2.read()), resp2_data[first_chunk:]))
            self.assertEqual(pool.num_requests, 2)

        except EmptyPoolError:
            self.assertEqual((yield From(r1.read()), resp_data[first_chunk:]))
            self.assertEqual(pool.num_requests, 1)

        self.assertEqual(pool.num_connections, 1)
    def test_lazy_load_twice(self):
        # This test is sad and confusing. Need to figure out what's
        # going on with partial reads and socket reuse.

        pool = HTTPConnectionPool(self.host, self.port, block=True, maxsize=1, timeout=2)

        payload_size = 1024 * 2
        first_chunk = 512

        boundary = 'foo'

        req_data = {'count': 'a' * payload_size}
        resp_data = encode_multipart_formdata(req_data, boundary=boundary)[0]

        req2_data = {'count': 'b' * payload_size}
        resp2_data = encode_multipart_formdata(req2_data, boundary=boundary)[0]

        r1 = yield From(pool.request('POST', '/echo', fields=req_data, multipart_boundary=boundary, preload_content=False))

        self.assertEqual(r1.read(first_chunk), resp_data[:first_chunk])

        try:
            r2 = yield From(pool.request('POST', '/echo', fields=req2_data, multipart_boundary=boundary,
                                    preload_content=False, pool_timeout=0.001))

            # This branch should generally bail here, but maybe someday it will
            # work? Perhaps by some sort of magic. Consider it a TODO.

            self.assertEqual(r2.read(first_chunk), resp2_data[:first_chunk])

            self.assertEqual((yield From(r1.read()), resp_data[first_chunk:]))
            self.assertEqual((yield From(r2.read()), resp2_data[first_chunk:]))
            self.assertEqual(pool.num_requests, 2)

        except EmptyPoolError:
            self.assertEqual((yield From(r1.read()), resp_data[first_chunk:]))
            self.assertEqual(pool.num_requests, 1)

        self.assertEqual(pool.num_connections, 1)
    def test_post_with_multipart(self):
        data = {"banana": "hammock", "lol": "cat"}
        r = yield From(self.pool.request("POST", "/echo", fields=data, encode_multipart=True))
        body = (yield From(r.data).split(b"\r\n"))

        encoded_data = encode_multipart_formdata(data)[0]
        expected_body = encoded_data.split(b"\r\n")

        # TODO: Get rid of extra parsing stuff when you can specify
        # a custom boundary to encode_multipart_formdata
        """
        We need to loop the return lines because a timestamp is attached
        from within encode_multipart_formdata. When the server echos back
        the data, it has the timestamp from when the data was encoded, which
        is not equivalent to when we run encode_multipart_formdata on
        the data again.
        """
        for i, line in enumerate(body):
            if line.startswith(b"--"):
                continue

            self.assertEqual(body[i], expected_body[i])
    def test_post_with_multipart(self):
        data = {'banana': 'hammock', 'lol': 'cat'}
        r = yield From(self.pool.request('POST', '/echo',
                                    fields=data,
                                    encode_multipart=True))
        body = (yield From(r.data).split(b'\r\n'))

        encoded_data = encode_multipart_formdata(data)[0]
        expected_body = encoded_data.split(b'\r\n')

        # TODO: Get rid of extra parsing stuff when you can specify
        # a custom boundary to encode_multipart_formdata
        """
        We need to loop the return lines because a timestamp is attached
        from within encode_multipart_formdata. When the server echos back
        the data, it has the timestamp from when the data was encoded, which
        is not equivalent to when we run encode_multipart_formdata on
        the data again.
        """
        for i, line in enumerate(body):
            if line.startswith(b'--'):
                continue

            self.assertEqual(body[i], expected_body[i])