]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Better AsyncHTTPClient error for GET with body or POST without one. 928/head
authorA. Jesse Jiryu Davis <jesse@mongodb.com>
Mon, 4 Nov 2013 19:57:05 +0000 (14:57 -0500)
committerA. Jesse Jiryu Davis <jesse@mongodb.com>
Mon, 4 Nov 2013 19:57:05 +0000 (14:57 -0500)
tornado/curl_httpclient.py
tornado/simple_httpclient.py
tornado/test/httpclient_test.py

index efa361c461080ae8eaa365127713189b62241f7b..cb97710a0bd6fd60ad1a51c9aa6bb8d33a663640 100644 (file)
@@ -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 '')
index 271b25be54bb14d8781284e3dd2da92c042b126f..cf962e696d95aed46e61610b4dd31dd131c92f55 100644 (file)
@@ -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))
index 15bb8e44bd8372e0aef16a4d0733b0aeb12f674f..b051e02ea143894e3c7cf4446f184570c915e37f 100644 (file)
@@ -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):