Added token property to Request object & added test to confirm new functionality.

This commit is contained in:
Sean Parsons 2016-12-25 18:01:26 -05:00
parent d5ad5e46da
commit 8220515e72
2 changed files with 36 additions and 1 deletions

View File

@ -8,8 +8,9 @@ from sanic.exceptions import InvalidUsage
from .log import log from .log import log
DEFAULT_HTTP_CONTENT_TYPE = "application/octet-stream" DEFAULT_HTTP_CONTENT_TYPE = "application/octet-stream"
# HTTP/1.1: https://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1 # HTTP/1.1: https://www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1
# > If the media type remains unknown, the recipient SHOULD treat it # > If the media type remains unknown, the recipient SHOULD treat it
# > as type "application/octet-stream" # > as type "application/octet-stream"
@ -72,6 +73,17 @@ class Request(dict):
return self.parsed_json return self.parsed_json
@property
def token(self):
"""
Attempts to return the auth header token.
:return: token related to request
"""
auth_header = self.headers.get('Authorization', None)
if auth_header is not None:
return auth_header.split()[1]
return auth_header
@property @property
def form(self): def form(self):
if self.parsed_form is None: if self.parsed_form is None:

View File

@ -112,6 +112,29 @@ def test_post_json():
assert response.text == 'OK' assert response.text == 'OK'
def test_post_token():
app = Sanic('test_post_token')
@app.route('/')
async def handler(request):
return text('OK')
payload = {'test': 'OK'}
# uuid4 generated token.
token = 'a1d895e0-553a-421a-8e22-5ff8ecb48cbf'
headers = {
'content-type': 'application/json',
'Authorization': 'Token {}'.format(token)
}
request, response = sanic_endpoint_test(app, data=json_dumps(payload), headers=headers)
assert request.token == token
assert response.text == 'OK'
def test_post_form_urlencoded(): def test_post_form_urlencoded():
app = Sanic('test_post_form_urlencoded') app = Sanic('test_post_form_urlencoded')