Merge pull request #783 from yunstanford/master

add content_type property in request
This commit is contained in:
Eli Uriegas 2017-06-08 17:30:07 -07:00 committed by GitHub
commit e1331fc0a2
2 changed files with 24 additions and 1 deletions

View File

@ -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')

View File

@ -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,25 @@ 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(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'
# ------------------------------------------------------------ #
# POST
# ------------------------------------------------------------ #