Accept responses with just a status code (Fixes #263)

This commit is contained in:
Miguel Grinberg
2024-11-10 20:04:30 +00:00
parent 4eac013087
commit c46e429106
3 changed files with 30 additions and 22 deletions

View File

@@ -274,6 +274,14 @@ class TestMicrodot(unittest.TestCase):
return '<p>four</p>', 202, \
{'Content-Type': 'text/html; charset=UTF-8'}
@app.route('/status')
def five(req):
return 202
@app.route('/status-headers')
def six(req):
return 202, {'Content-Type': 'text/html; charset=UTF-8'}
client = TestClient(app)
res = self._run(client.get('/body'))
@@ -299,6 +307,18 @@ class TestMicrodot(unittest.TestCase):
'text/html; charset=UTF-8')
self.assertEqual(res.text, '<p>four</p>')
res = self._run(client.get('/status'))
self.assertEqual(res.text, '')
self.assertEqual(res.status_code, 202)
self.assertEqual(res.headers['Content-Type'],
'text/plain; charset=UTF-8')
res = self._run(client.get('/status-headers'))
self.assertEqual(res.text, '')
self.assertEqual(res.status_code, 202)
self.assertEqual(res.headers['Content-Type'],
'text/html; charset=UTF-8')
def test_before_after_request(self):
app = Microdot()

View File

@@ -109,18 +109,6 @@ class TestResponse(unittest.TestCase):
fd.response)
self.assertTrue(fd.response.endswith(b'\r\n\r\n[1, "2"]'))
def test_create_status_code(self):
res = Response(202)
self.assertEqual(res.status_code, 202)
self.assertEqual(res.body, b'')
fd = FakeStreamAsync()
self._run(res.write(fd))
self.assertIn(b'HTTP/1.0 202 N/A\r\n', fd.response)
self.assertIn(b'Content-Length: 0\r\n', fd.response)
self.assertIn(b'Content-Type: text/plain; charset=UTF-8\r\n',
fd.response)
self.assertTrue(fd.response.endswith(b'\r\n\r\n'))
def test_create_from_none(self):
res = Response(None)
self.assertEqual(res.status_code, 204)