]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Clarify error messages about http request bodies.
authorBen Darnell <ben@bendarnell.com>
Sun, 5 Oct 2014 19:17:27 +0000 (15:17 -0400)
committerBen Darnell <ben@bendarnell.com>
Sun, 5 Oct 2014 19:17:27 +0000 (15:17 -0400)
Change AssertionError to ValueError and s/empty/None.

Closes #1213.

tornado/curl_httpclient.py
tornado/simple_httpclient.py
tornado/test/httpclient_test.py

index eba6af9ed11c63ae54dc39b4da2e87cb8833d5c2..d51ae3502ae779ee22eb5e7beb40d49c2c6b3121 100644 (file)
@@ -377,11 +377,11 @@ class CurlAsyncHTTPClient(AsyncHTTPClient):
         # Handle curl's cryptic options for every individual HTTP method
         if request.method == "GET":
             if request.body is not None:
-                raise AssertionError('Body must be empty for GET request')
+                raise ValueError('Body must be None for GET request')
         elif request.method in ("POST", "PUT") or request.body:
             if request.body is None:
-                raise AssertionError(
-                    'Body must not be empty for "%s" request'
+                raise ValueError(
+                    'Body must not be None for "%s" request'
                     % request.method)
 
             request_buffer = BytesIO(utf8(request.body))
index f0f73fa0c519fc91080545664f3ed0b5be864820..e60c434f8e05cf70a357febece10b915d36d7b88 100644 (file)
@@ -314,18 +314,18 @@ class _HTTPConnection(httputil.HTTPMessageDelegate):
         if self.request.user_agent:
             self.request.headers["User-Agent"] = self.request.user_agent
         if not self.request.allow_nonstandard_methods:
-            if self.request.method in ("POST", "PATCH", "PUT"):
-                if (self.request.body is None and
-                        self.request.body_producer is None):
-                    raise AssertionError(
-                        'Body must not be empty for "%s" request'
-                        % self.request.method)
-            else:
-                if (self.request.body is not None or
-                        self.request.body_producer is not None):
-                    raise AssertionError(
-                        'Body must be empty for "%s" request'
-                        % self.request.method)
+            # Some HTTP methods nearly always have bodies while others
+            # almost never do. Fail in this case unless the user has
+            # opted out of sanity checks with allow_nonstandard_methods.
+            body_expected = self.request.method in ("POST", "PATCH", "PUT")
+            body_present = (self.request.body is not None or
+                            self.request.body_producer is not None)
+            if ((body_expected and not body_present) or
+                (body_present and not body_expected)):
+                raise ValueError(
+                    'Body must %sbe None for method %s (unelss '
+                    'allow_nonstandard_methods is true)' %
+                    ('not ' if body_expected else '', self.request.method))
         if self.request.expect_100_continue:
             self.request.headers["Expect"] = "100-continue"
         if self.request.body is not None:
index c90e6ce9ac5868b8e092e6279638afc2e666fea3..36fdeb0c77c12d9f11452b5d81b61512351cbc23 100644 (file)
@@ -416,17 +416,17 @@ Transfer-Encoding: chunked
         self.assertEqual(response.body, b'OTHER')
 
     @gen_test
-    def test_body(self):
+    def test_body_sanity_checks(self):
         hello_url = self.get_url('/hello')
-        with self.assertRaises(AssertionError) as context:
+        with self.assertRaises(ValueError) as context:
             yield self.http_client.fetch(hello_url, body='data')
 
-        self.assertTrue('must be empty' in str(context.exception))
+        self.assertTrue('must be None' in str(context.exception))
 
-        with self.assertRaises(AssertionError) as context:
+        with self.assertRaises(ValueError) as context:
             yield self.http_client.fetch(hello_url, method='POST')
 
-        self.assertTrue('must not be empty' in str(context.exception))
+        self.assertTrue('must not be None' in str(context.exception))
 
     # This test causes odd failures with the combination of
     # curl_httpclient (at least with the version of libcurl available