2017-02-22 17:34:14 +00:00
|
|
|
import random
|
|
|
|
|
2016-11-20 01:48:28 +00:00
|
|
|
from sanic.response import json
|
2017-05-15 02:49:25 +01:00
|
|
|
|
2019-04-23 22:44:42 +01:00
|
|
|
|
2017-05-15 02:49:25 +01:00
|
|
|
try:
|
|
|
|
from ujson import loads
|
|
|
|
except ImportError:
|
|
|
|
from json import loads
|
2016-11-20 01:48:28 +00:00
|
|
|
|
2019-10-23 17:12:20 +01:00
|
|
|
|
2019-09-26 22:11:31 +01:00
|
|
|
def test_custom_context(app):
|
|
|
|
@app.middleware("request")
|
|
|
|
def store(request):
|
|
|
|
request.ctx.user = "sanic"
|
|
|
|
request.ctx.session = None
|
|
|
|
|
|
|
|
@app.route("/")
|
|
|
|
def handler(request):
|
|
|
|
# Accessing non-existant key should fail with AttributeError
|
|
|
|
try:
|
|
|
|
invalid = request.ctx.missing
|
|
|
|
except AttributeError as e:
|
|
|
|
invalid = str(e)
|
2019-10-23 17:12:20 +01:00
|
|
|
return json(
|
|
|
|
{
|
|
|
|
"user": request.ctx.user,
|
|
|
|
"session": request.ctx.session,
|
|
|
|
"has_user": hasattr(request.ctx, "user"),
|
|
|
|
"has_session": hasattr(request.ctx, "session"),
|
|
|
|
"has_missing": hasattr(request.ctx, "missing"),
|
|
|
|
"invalid": invalid,
|
|
|
|
}
|
|
|
|
)
|
2019-09-26 22:11:31 +01:00
|
|
|
|
2020-07-29 12:25:31 +01:00
|
|
|
@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)
|
2020-09-28 22:40:24 +01:00
|
|
|
j["response_mw_valid"] = user
|
|
|
|
j["response_mw_invalid"] = invalid
|
2020-07-29 12:25:31 +01:00
|
|
|
return json(j)
|
|
|
|
|
2019-09-26 22:11:31 +01:00
|
|
|
request, response = app.test_client.get("/")
|
|
|
|
assert response.json == {
|
|
|
|
"user": "sanic",
|
|
|
|
"session": None,
|
|
|
|
"has_user": True,
|
|
|
|
"has_session": True,
|
|
|
|
"has_missing": False,
|
|
|
|
"invalid": "'types.SimpleNamespace' object has no attribute 'missing'",
|
2020-07-29 12:25:31 +01:00
|
|
|
"response_mw_valid": "sanic",
|
2020-09-28 22:40:24 +01:00
|
|
|
"response_mw_invalid": "'types.SimpleNamespace' object has no attribute 'missing'",
|
2019-09-26 22:11:31 +01:00
|
|
|
}
|
|
|
|
|
2016-11-20 01:48:28 +00:00
|
|
|
|
2018-08-26 15:43:14 +01:00
|
|
|
def test_app_injection(app):
|
2017-02-22 17:34:14 +00:00
|
|
|
expected = random.choice(range(0, 100))
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.listener("after_server_start")
|
2017-02-22 17:34:14 +00:00
|
|
|
async def inject_data(app, loop):
|
|
|
|
app.injected = expected
|
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
@app.get("/")
|
2017-02-22 17:34:14 +00:00
|
|
|
async def handler(request):
|
2018-12-30 11:18:06 +00:00
|
|
|
return json({"injected": request.app.injected})
|
2017-02-22 17:34:14 +00:00
|
|
|
|
2018-12-30 11:18:06 +00:00
|
|
|
request, response = app.test_client.get("/")
|
2017-02-22 17:34:14 +00:00
|
|
|
|
|
|
|
response_json = loads(response.text)
|
2018-12-30 11:18:06 +00:00
|
|
|
assert response_json["injected"] == expected
|