diff --git a/src/microdot.py b/src/microdot.py index f277c73..06e84d9 100644 --- a/src/microdot.py +++ b/src/microdot.py @@ -309,9 +309,10 @@ class Response(): } 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.headers = headers.copy() if headers else {} + self.reason = reason if isinstance(body, (dict, list)): self.body = json.dumps(body).encode() self.headers['Content-Type'] = 'application/json' @@ -364,9 +365,10 @@ class Response(): self.complete() # 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( - status_code=self.status_code, - reason='OK' if self.status_code == 200 else 'N/A').encode()) + status_code=self.status_code, reason=reason).encode()) # headers for header, value in self.headers.items(): diff --git a/src/microdot_asyncio.py b/src/microdot_asyncio.py index 0425f1e..e3c6ea7 100644 --- a/src/microdot_asyncio.py +++ b/src/microdot_asyncio.py @@ -74,9 +74,10 @@ class Response(BaseResponse): self.complete() # 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( - status_code=self.status_code, - reason='OK' if self.status_code == 200 else 'N/A').encode()) + status_code=self.status_code, reason=reason).encode()) # headers for header, value in self.headers.items(): diff --git a/tests/microdot/test_response.py b/tests/microdot/test_response.py index 7563c06..46a950c 100644 --- a/tests/microdot/test_response.py +++ b/tests/microdot/test_response.py @@ -112,6 +112,28 @@ class TestResponse(unittest.TestCase): self.assertEqual(res.headers, {'X-Test': '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): res = Response('ok') res.set_cookie('foo1', 'bar1') diff --git a/tests/microdot_asyncio/test_response_asyncio.py b/tests/microdot_asyncio/test_response_asyncio.py index 7a72aae..266fe0e 100644 --- a/tests/microdot_asyncio/test_response_asyncio.py +++ b/tests/microdot_asyncio/test_response_asyncio.py @@ -85,6 +85,26 @@ class TestResponseAsync(unittest.TestCase): self.assertIn(b'Content-Type: application/json\r\n', fd.response) 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): res = Response.send_file('tests/files/test.txt', content_type='text/html')