From: Ben Darnell Date: Sun, 4 Oct 2015 00:52:04 +0000 (-0400) Subject: Support other yieldables for httpclient body_producer X-Git-Tag: v4.3.0b1~8^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b20ca363c7df16d8bd7c21b9db4c639ebae543c5;p=thirdparty%2Ftornado.git Support other yieldables for httpclient body_producer --- diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index 074d18b84..37b0bc27f 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -1,8 +1,8 @@ #!/usr/bin/env python from __future__ import absolute_import, division, print_function, with_statement -from tornado.concurrent import is_future from tornado.escape import utf8, _unicode +from tornado import gen from tornado.httpclient import HTTPResponse, HTTPError, AsyncHTTPClient, main, _RequestProxy from tornado import httputil from tornado.http1connection import HTTP1Connection, HTTP1ConnectionParameters @@ -391,7 +391,9 @@ class _HTTPConnection(httputil.HTTPMessageDelegate): self.connection.write(self.request.body) elif self.request.body_producer is not None: fut = self.request.body_producer(self.connection.write) - if is_future(fut): + if fut is not None: + fut = gen.convert_yielded(fut) + def on_body_written(fut): fut.result() self.connection.finish() diff --git a/tornado/test/simple_httpclient_test.py b/tornado/test/simple_httpclient_test.py index d478071f3..b6687a298 100644 --- a/tornado/test/simple_httpclient_test.py +++ b/tornado/test/simple_httpclient_test.py @@ -22,7 +22,7 @@ from tornado.simple_httpclient import SimpleAsyncHTTPClient from tornado.test.httpclient_test import ChunkHandler, CountdownHandler, HelloWorldHandler, RedirectHandler from tornado.test import httpclient_test from tornado.testing import AsyncHTTPTestCase, AsyncHTTPSTestCase, AsyncTestCase, ExpectLog -from tornado.test.util import skipOnTravis, skipIfNoIPv6, refusing_port, unittest +from tornado.test.util import skipOnTravis, skipIfNoIPv6, refusing_port, unittest, skipBefore35, exec_test from tornado.web import RequestHandler, Application, asynchronous, url, stream_request_body @@ -404,6 +404,33 @@ class SimpleHTTPClientTestMixin(object): response.rethrow() self.assertEqual(response.body, b"12345678") + @skipBefore35 + def test_native_body_producer_chunked(self): + namespace = exec_test(globals(), locals(), """ + async def body_producer(write): + await write(b'1234') + await gen.Task(IOLoop.current().add_callback) + await write(b'5678') + """) + response = self.fetch("/echo_post", method="POST", + body_producer=namespace["body_producer"]) + response.rethrow() + self.assertEqual(response.body, b"12345678") + + @skipBefore35 + def test_native_body_producer_content_length(self): + namespace = exec_test(globals(), locals(), """ + async def body_producer(write): + await write(b'1234') + await gen.Task(IOLoop.current().add_callback) + await write(b'5678') + """) + response = self.fetch("/echo_post", method="POST", + body_producer=namespace["body_producer"], + headers={'Content-Length': '8'}) + response.rethrow() + self.assertEqual(response.body, b"12345678") + def test_100_continue(self): response = self.fetch("/echo_post", method="POST", body=b"1234",