From 126645d433ec018992bb82a140f32577b30cc387 Mon Sep 17 00:00:00 2001 From: "A. Jesse Jiryu Davis" Date: Mon, 4 Nov 2013 14:57:05 -0500 Subject: [PATCH] Better AsyncHTTPClient error for GET with body or POST without one. --- tornado/curl_httpclient.py | 8 ++++++++ tornado/simple_httpclient.py | 10 ++++++++-- tornado/test/httpclient_test.py | 13 +++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/tornado/curl_httpclient.py b/tornado/curl_httpclient.py index efa361c46..cb97710a0 100644 --- a/tornado/curl_httpclient.py +++ b/tornado/curl_httpclient.py @@ -407,6 +407,11 @@ def _curl_setup_request(curl, request, buffer, headers): # Handle curl's cryptic options for every individual HTTP method if request.method in ("POST", "PUT"): + if request.body is None: + raise AssertionError( + 'Body must not be empty for "%s" request' + % request.method) + request_buffer = BytesIO(utf8(request.body)) curl.setopt(pycurl.READFUNCTION, request_buffer.read) if request.method == "POST": @@ -417,6 +422,9 @@ def _curl_setup_request(curl, request, buffer, headers): curl.setopt(pycurl.POSTFIELDSIZE, len(request.body)) else: curl.setopt(pycurl.INFILESIZE, len(request.body)) + elif request.method == "GET": + if request.body is not None: + raise AssertionError('Body must be empty for GET request') if request.auth_username is not None: userpwd = "%s:%s" % (request.auth_username, request.auth_password or '') diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index 271b25be5..cf962e696 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -297,9 +297,15 @@ class _HTTPConnection(object): self.request.headers["User-Agent"] = self.request.user_agent if not self.request.allow_nonstandard_methods: if self.request.method in ("POST", "PATCH", "PUT"): - assert self.request.body is not None + if self.request.body is None: + raise AssertionError( + 'Body must not be empty for "%s" request' + % self.request.method) else: - assert self.request.body is None + if self.request.body is not None: + raise AssertionError( + 'Body must be empty for "%s" request' + % self.request.method) if self.request.body is not None: self.request.headers["Content-Length"] = str(len( self.request.body)) diff --git a/tornado/test/httpclient_test.py b/tornado/test/httpclient_test.py index 15bb8e44b..b051e02ea 100644 --- a/tornado/test/httpclient_test.py +++ b/tornado/test/httpclient_test.py @@ -387,6 +387,19 @@ Transfer-Encoding: chunked allow_nonstandard_methods=True) self.assertEqual(response.body, b'OTHER') + @gen_test + def test_body(self): + hello_url = self.get_url('/hello') + with self.assertRaises(AssertionError) as context: + yield self.http_client.fetch(hello_url, body='data') + + self.assertTrue('must be empty' in str(context.exception)) + + with self.assertRaises(AssertionError) as context: + yield self.http_client.fetch(hello_url, method='POST') + + self.assertTrue('must not be empty' in str(context.exception)) + class RequestProxyTest(unittest.TestCase): def test_request_set(self): -- 2.47.2