Merge pull request #163 from channelcat/request-data-vars

Access Request like a dictionary

Closes #129 #132
This commit is contained in:
Eli Uriegas 2016-11-23 13:51:17 -06:00 committed by GitHub
commit fc19f2ea34
2 changed files with 25 additions and 1 deletions

View File

@ -32,7 +32,7 @@ class RequestParameters(dict):
return self.super.get(name, default) return self.super.get(name, default)
class Request: class Request(dict):
""" """
Properties of an HTTP request such as URL, headers, etc. Properties of an HTTP request such as URL, headers, etc.
""" """

View File

@ -0,0 +1,24 @@
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):
request['user'] = 'sanic'
request['sidekick'] = 'tails'
del request['sidekick']
@app.route('/')
def handler(request):
return json({ 'user': request.get('user'), 'sidekick': request.get('sidekick') })
request, response = sanic_endpoint_test(app)
response_json = loads(response.text)
assert response_json['user'] == 'sanic'
assert response_json.get('sidekick') is None