From 986b0aa106702ffc015044b55746b8eb7d86ef9e Mon Sep 17 00:00:00 2001 From: Sean Parsons Date: Mon, 26 Dec 2016 06:41:41 -0500 Subject: [PATCH 1/2] Added token property to request object. --- sanic/request.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/sanic/request.py b/sanic/request.py index 62d89781..4309bbed 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -72,6 +72,17 @@ class Request(dict): 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') + if auth_header is not None: + return auth_header.split()[1] + return auth_header + @property def form(self): if self.parsed_form is None: From 548458c3e0e8201434dd9160cbfb4b151010eaf4 Mon Sep 17 00:00:00 2001 From: Sean Parsons Date: Mon, 26 Dec 2016 06:48:53 -0500 Subject: [PATCH 2/2] Added test for new token property on request object. --- tests/test_requests.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/test_requests.py b/tests/test_requests.py index 5895e3d5..4f81b9a0 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -92,6 +92,24 @@ def test_query_string(): assert request.args.get('test2') == 'false' +def test_token(): + app = Sanic('test_post_token') + + @app.route('/') + async def handler(request): + return text('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, headers=headers) + + assert request.token == token + # ------------------------------------------------------------ # # POST # ------------------------------------------------------------ #