diff --git a/tornado/curl_httpclient.py b/tornado/curl_httpclient.py index eba6af9ed1..d51ae3502a 100644 --- a/tornado/curl_httpclient.py +++ b/tornado/curl_httpclient.py @@ -377,11 +377,11 @@ def _curl_setup_request(self, curl, request, buffer, headers): # 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)) diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index f0f73fa0c5..e60c434f8e 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -314,18 +314,18 @@ def _on_connect(self, stream): 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: diff --git a/tornado/test/httpclient_test.py b/tornado/test/httpclient_test.py index c90e6ce9ac..36fdeb0c77 100644 --- a/tornado/test/httpclient_test.py +++ b/tornado/test/httpclient_test.py @@ -416,17 +416,17 @@ def test_all_methods(self): 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