From 0072fd1573d43c64a4a2b9b89b4e4f887bc07a70 Mon Sep 17 00:00:00 2001 From: Ashley Sommer Date: Wed, 29 Jul 2020 21:25:31 +1000 Subject: [PATCH] Add an additional component to the request_data context test. This checks if items stored a request.ctx are able to be accessed from a response-middleware after a response is issued. (#1888) Co-authored-by: Adam Hopkins --- tests/test_request_data.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/test_request_data.py b/tests/test_request_data.py index 8136eeb3..d9a6351f 100644 --- a/tests/test_request_data.py +++ b/tests/test_request_data.py @@ -33,6 +33,23 @@ def test_custom_context(app): } ) + @app.middleware("response") + def modify(request, response): + # Using response-middleware to access request ctx + try: + user = request.ctx.user + except AttributeError as e: + user = str(e) + try: + invalid = request.ctx.missing + except AttributeError as e: + invalid = str(e) + + j = loads(response.body) + j['response_mw_valid'] = user + j['response_mw_invalid'] = invalid + return json(j) + request, response = app.test_client.get("/") assert response.json == { "user": "sanic", @@ -41,6 +58,9 @@ def test_custom_context(app): "has_session": True, "has_missing": False, "invalid": "'types.SimpleNamespace' object has no attribute 'missing'", + "response_mw_valid": "sanic", + "response_mw_invalid": + "'types.SimpleNamespace' object has no attribute 'missing'" }