# 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))
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:
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