Request-specific after_request handlers

This commit is contained in:
Miguel Grinberg
2022-07-30 14:52:56 +01:00
parent 7686b2ae38
commit 120abe45ec
2 changed files with 32 additions and 1 deletions

View File

@@ -117,6 +117,10 @@ class TestMicrodot(unittest.TestCase):
@app.before_request
def before_request(req):
if req.path == '/bar':
@req.after_request
def after_request(req, res):
res.headers['X-Two'] = '2'
return res
return 'bar', 202
req.g.message = 'baz'
@@ -143,6 +147,7 @@ class TestMicrodot(unittest.TestCase):
app.run()
self.assertTrue(fd.response.startswith(b'HTTP/1.0 202 N/A\r\n'))
self.assertIn(b'X-One: 1\r\n', fd.response)
self.assertIn(b'X-Two: 2\r\n', fd.response)
self.assertIn(b'Set-Cookie: foo=bar\r\n', fd.response)
self.assertIn(b'Content-Length: 3\r\n', fd.response)
self.assertIn(b'Content-Type: text/plain\r\n', fd.response)