Return 204 when view function returns None
This commit is contained in:
@@ -373,6 +373,9 @@ class Response():
|
||||
send_file_buffer_size = 1024
|
||||
|
||||
def __init__(self, body='', status_code=200, headers=None, reason=None):
|
||||
if body is None and status_code == 200:
|
||||
body = ''
|
||||
status_code = 204
|
||||
self.status_code = status_code
|
||||
self.headers = headers.copy() if headers else {}
|
||||
self.reason = reason
|
||||
|
||||
@@ -88,6 +88,18 @@ class TestResponse(unittest.TestCase):
|
||||
self.assertIn(b'Content-Type: application/json\r\n', response)
|
||||
self.assertTrue(response.endswith(b'\r\n\r\n[1, "2"]'))
|
||||
|
||||
def test_create_from_none(self):
|
||||
res = Response(None)
|
||||
self.assertEqual(res.status_code, 204)
|
||||
self.assertEqual(res.body, b'')
|
||||
fd = io.BytesIO()
|
||||
res.write(fd)
|
||||
response = fd.getvalue()
|
||||
self.assertIn(b'HTTP/1.0 204 N/A\r\n', response)
|
||||
self.assertIn(b'Content-Length: 0\r\n', response)
|
||||
self.assertIn(b'Content-Type: text/plain\r\n', response)
|
||||
self.assertTrue(response.endswith(b'\r\n\r\n'))
|
||||
|
||||
def test_create_from_other(self):
|
||||
res = Response(123)
|
||||
self.assertEqual(res.status_code, 200)
|
||||
|
||||
Reference in New Issue
Block a user