Accept a custom reason phrase for the HTTP response (Fixes #25)
This commit is contained in:
@@ -309,9 +309,10 @@ class Response():
|
|||||||
}
|
}
|
||||||
send_file_buffer_size = 1024
|
send_file_buffer_size = 1024
|
||||||
|
|
||||||
def __init__(self, body='', status_code=200, headers=None):
|
def __init__(self, body='', status_code=200, headers=None, reason=None):
|
||||||
self.status_code = status_code
|
self.status_code = status_code
|
||||||
self.headers = headers.copy() if headers else {}
|
self.headers = headers.copy() if headers else {}
|
||||||
|
self.reason = reason
|
||||||
if isinstance(body, (dict, list)):
|
if isinstance(body, (dict, list)):
|
||||||
self.body = json.dumps(body).encode()
|
self.body = json.dumps(body).encode()
|
||||||
self.headers['Content-Type'] = 'application/json'
|
self.headers['Content-Type'] = 'application/json'
|
||||||
@@ -364,9 +365,10 @@ class Response():
|
|||||||
self.complete()
|
self.complete()
|
||||||
|
|
||||||
# status code
|
# status code
|
||||||
|
reason = self.reason if self.reason is not None else \
|
||||||
|
('OK' if self.status_code == 200 else 'N/A')
|
||||||
stream.write('HTTP/1.0 {status_code} {reason}\r\n'.format(
|
stream.write('HTTP/1.0 {status_code} {reason}\r\n'.format(
|
||||||
status_code=self.status_code,
|
status_code=self.status_code, reason=reason).encode())
|
||||||
reason='OK' if self.status_code == 200 else 'N/A').encode())
|
|
||||||
|
|
||||||
# headers
|
# headers
|
||||||
for header, value in self.headers.items():
|
for header, value in self.headers.items():
|
||||||
|
|||||||
@@ -74,9 +74,10 @@ class Response(BaseResponse):
|
|||||||
self.complete()
|
self.complete()
|
||||||
|
|
||||||
# status code
|
# status code
|
||||||
|
reason = self.reason if self.reason is not None else \
|
||||||
|
('OK' if self.status_code == 200 else 'N/A')
|
||||||
await stream.awrite('HTTP/1.0 {status_code} {reason}\r\n'.format(
|
await stream.awrite('HTTP/1.0 {status_code} {reason}\r\n'.format(
|
||||||
status_code=self.status_code,
|
status_code=self.status_code, reason=reason).encode())
|
||||||
reason='OK' if self.status_code == 200 else 'N/A').encode())
|
|
||||||
|
|
||||||
# headers
|
# headers
|
||||||
for header, value in self.headers.items():
|
for header, value in self.headers.items():
|
||||||
|
|||||||
@@ -112,6 +112,28 @@ class TestResponse(unittest.TestCase):
|
|||||||
self.assertEqual(res.headers, {'X-Test': 'Foo'})
|
self.assertEqual(res.headers, {'X-Test': 'Foo'})
|
||||||
self.assertEqual(res.body, b'foo')
|
self.assertEqual(res.body, b'foo')
|
||||||
|
|
||||||
|
def test_create_with_reason(self):
|
||||||
|
res = Response('foo', reason='ALL GOOD!')
|
||||||
|
self.assertEqual(res.status_code, 200)
|
||||||
|
self.assertEqual(res.headers, {})
|
||||||
|
self.assertEqual(res.reason, 'ALL GOOD!')
|
||||||
|
self.assertEqual(res.body, b'foo')
|
||||||
|
fd = io.BytesIO()
|
||||||
|
res.write(fd)
|
||||||
|
response = fd.getvalue()
|
||||||
|
self.assertIn(b'HTTP/1.0 200 ALL GOOD!\r\n', response)
|
||||||
|
|
||||||
|
def test_create_with_status_and_reason(self):
|
||||||
|
res = Response('not found', 404, reason='NOT FOUND')
|
||||||
|
self.assertEqual(res.status_code, 404)
|
||||||
|
self.assertEqual(res.headers, {})
|
||||||
|
self.assertEqual(res.reason, 'NOT FOUND')
|
||||||
|
self.assertEqual(res.body, b'not found')
|
||||||
|
fd = io.BytesIO()
|
||||||
|
res.write(fd)
|
||||||
|
response = fd.getvalue()
|
||||||
|
self.assertIn(b'HTTP/1.0 404 NOT FOUND\r\n', response)
|
||||||
|
|
||||||
def test_cookies(self):
|
def test_cookies(self):
|
||||||
res = Response('ok')
|
res = Response('ok')
|
||||||
res.set_cookie('foo1', 'bar1')
|
res.set_cookie('foo1', 'bar1')
|
||||||
|
|||||||
@@ -85,6 +85,26 @@ class TestResponseAsync(unittest.TestCase):
|
|||||||
self.assertIn(b'Content-Type: application/json\r\n', fd.response)
|
self.assertIn(b'Content-Type: application/json\r\n', fd.response)
|
||||||
self.assertTrue(fd.response.endswith(b'\r\n\r\n[1, "2"]'))
|
self.assertTrue(fd.response.endswith(b'\r\n\r\n[1, "2"]'))
|
||||||
|
|
||||||
|
def test_create_with_reason(self):
|
||||||
|
res = Response('foo', reason='ALL GOOD!')
|
||||||
|
self.assertEqual(res.status_code, 200)
|
||||||
|
self.assertEqual(res.headers, {})
|
||||||
|
self.assertEqual(res.reason, 'ALL GOOD!')
|
||||||
|
self.assertEqual(res.body, b'foo')
|
||||||
|
fd = FakeStreamAsync()
|
||||||
|
_run(res.write(fd))
|
||||||
|
self.assertIn(b'HTTP/1.0 200 ALL GOOD!\r\n', fd.response)
|
||||||
|
|
||||||
|
def test_create_with_status_and_reason(self):
|
||||||
|
res = Response('not found', 404, reason='NOT FOUND')
|
||||||
|
self.assertEqual(res.status_code, 404)
|
||||||
|
self.assertEqual(res.headers, {})
|
||||||
|
self.assertEqual(res.reason, 'NOT FOUND')
|
||||||
|
self.assertEqual(res.body, b'not found')
|
||||||
|
fd = FakeStreamAsync()
|
||||||
|
_run(res.write(fd))
|
||||||
|
self.assertIn(b'HTTP/1.0 404 NOT FOUND\r\n', fd.response)
|
||||||
|
|
||||||
def test_send_file(self):
|
def test_send_file(self):
|
||||||
res = Response.send_file('tests/files/test.txt',
|
res = Response.send_file('tests/files/test.txt',
|
||||||
content_type='text/html')
|
content_type='text/html')
|
||||||
|
|||||||
Reference in New Issue
Block a user