Skip to content

Commit

Permalink
Add an additional component to the request_data context test. This ch…
Browse files Browse the repository at this point in the history
…ecks 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 <[email protected]>
  • Loading branch information
ashleysommer and ahopkins authored Jul 29, 2020
1 parent 5d5ed10 commit 0072fd1
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions tests/test_request_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ def handler(request):
}
)

@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",
Expand All @@ -41,6 +58,9 @@ def handler(request):
"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'"
}


Expand Down

0 comments on commit 0072fd1

Please sign in to comment.