From aac99c45c02247ac546852769c96d23e7232dabb Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Wed, 7 Jun 2017 20:46:48 -0700 Subject: [PATCH 1/3] add content_type property in request --- sanic/request.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/sanic/request.py b/sanic/request.py index f3de36f8..a5d204a8 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -174,6 +174,10 @@ class Request(dict): # so pull it from the headers return self.headers.get('Host', '') + @property + def content_type(self): + return self.headers.get('Content-Type', DEFAULT_HTTP_CONTENT_TYPE) + @property def path(self): return self._parsed_url.path.decode('utf-8') From 81889fd7a39b2fdab2a8362b7223773fea233668 Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Wed, 7 Jun 2017 20:48:07 -0700 Subject: [PATCH 2/3] add unit tests --- tests/test_requests.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/test_requests.py b/tests/test_requests.py index 06e2b7ae..b4acb2dd 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -8,7 +8,7 @@ import pytest from sanic import Sanic from sanic.exceptions import ServerError from sanic.response import json, text - +from sanic.request import DEFAULT_HTTP_CONTENT_TYPE from sanic.testing import HOST, PORT @@ -192,6 +192,23 @@ def test_token(): assert request.token is None +def test_content_type(): + app = Sanic('test_content_type') + + @app.route('/') + async def handler(request): + return text('OK') + + request, response = app.test_client.get('/') + assert request.content_type == DEFAULT_HTTP_CONTENT_TYPE + + headers = { + 'content-type': 'application/json', + } + request, response = app.test_client.get('/', headers=headers) + assert request.content_type == 'application/json' + + # ------------------------------------------------------------ # # POST # ------------------------------------------------------------ # From 3802f8ff65facf9f890358ff2d112b4ead06006a Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Thu, 8 Jun 2017 17:25:22 -0700 Subject: [PATCH 3/3] unit tests --- tests/test_requests.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_requests.py b/tests/test_requests.py index b4acb2dd..d61d4d55 100644 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -197,16 +197,18 @@ def test_content_type(): @app.route('/') async def handler(request): - return text('OK') + return text(request.content_type) request, response = app.test_client.get('/') assert request.content_type == DEFAULT_HTTP_CONTENT_TYPE + assert response.text == DEFAULT_HTTP_CONTENT_TYPE headers = { 'content-type': 'application/json', } request, response = app.test_client.get('/', headers=headers) assert request.content_type == 'application/json' + assert response.text == 'application/json' # ------------------------------------------------------------ #