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