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 <admhpkns@gmail.com>
This commit is contained in:
Ashley Sommer 2020-07-29 21:25:31 +10:00 committed by GitHub
parent 5d5ed10a45
commit 0072fd1573
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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("/") request, response = app.test_client.get("/")
assert response.json == { assert response.json == {
"user": "sanic", "user": "sanic",
@ -41,6 +58,9 @@ def test_custom_context(app):
"has_session": True, "has_session": True,
"has_missing": False, "has_missing": False,
"invalid": "'types.SimpleNamespace' object has no attribute 'missing'", "invalid": "'types.SimpleNamespace' object has no attribute 'missing'",
"response_mw_valid": "sanic",
"response_mw_invalid":
"'types.SimpleNamespace' object has no attribute 'missing'"
} }