Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Clarify error messages about http request bodies.
Change AssertionError to ValueError and s/empty/None.

Closes #1213.
  • Loading branch information
bdarnell committed Oct 5, 2014
1 parent 27d67a2 commit 542c8a5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions tornado/curl_httpclient.py
Expand Up @@ -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))
Expand Down
24 changes: 12 additions & 12 deletions tornado/simple_httpclient.py
Expand Up @@ -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:
Expand Down
10 changes: 5 additions & 5 deletions tornado/test/httpclient_test.py
Expand Up @@ -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
Expand Down

0 comments on commit 542c8a5

Please sign in to comment.