sanic/tests/test_request_data.py

25 lines
657 B
Python
Raw Normal View History

2016-11-20 01:48:28 +00:00
from sanic import Sanic
from sanic.response import json
from sanic.utils import sanic_endpoint_test
from ujson import loads
def test_storage():
app = Sanic('test_text')
@app.middleware('request')
def store(request):
2016-11-20 02:26:03 +00:00
request['user'] = 'sanic'
request['sidekick'] = 'tails'
del request['sidekick']
2016-11-20 01:48:28 +00:00
@app.route('/')
def handler(request):
2016-11-20 02:26:03 +00:00
return json({ 'user': request.get('user'), 'sidekick': request.get('sidekick') })
2016-11-20 01:48:28 +00:00
request, response = sanic_endpoint_test(app)
response_json = loads(response.text)
2016-11-20 02:26:03 +00:00
assert response_json['user'] == 'sanic'
2016-11-23 17:03:00 +00:00
assert response_json.get('sidekick') is None