feat(Request): Add method to detect whether the client accepts XML.
This commit is contained in:
@@ -37,7 +37,7 @@ from falcon.api import API, DEFAULT_MEDIA_TYPE # NOQA
|
||||
from falcon.status_codes import * # NOQA
|
||||
from falcon.exceptions import * # NOQA
|
||||
from falcon.http_error import HTTPError # NOQA
|
||||
from falcon.util import dt_to_http, to_query_str # NOQA
|
||||
from falcon.util import dt_to_http, http_date_to_dt, to_query_str # NOQA
|
||||
from falcon.hooks import before, after # NOQA
|
||||
from falcon.request import Request # NOQA
|
||||
from falcon.response import Response # NOQA
|
||||
|
||||
@@ -87,7 +87,7 @@ class API(object):
|
||||
if ex.headers is not None:
|
||||
resp.set_headers(ex.headers)
|
||||
|
||||
if req.client_accepts_json():
|
||||
if req.client_accepts_json:
|
||||
resp.body = ex.json()
|
||||
|
||||
except TypeError as ex:
|
||||
|
||||
@@ -20,8 +20,9 @@ from datetime import datetime
|
||||
|
||||
import six
|
||||
|
||||
from falcon import request_helpers as helpers
|
||||
from falcon.exceptions import *
|
||||
from falcon import util
|
||||
from falcon import request_helpers as helpers
|
||||
|
||||
DEFAULT_ERROR_LOG_FORMAT = ('{0:%Y-%m-%d %H:%M:%S} [FALCON] [ERROR]'
|
||||
' {1} {2}?{3} => {4}\n')
|
||||
@@ -119,13 +120,22 @@ class Request(object):
|
||||
|
||||
self._wsgierrors.write(log_line)
|
||||
|
||||
@property
|
||||
def client_accepts_json(self):
|
||||
"""Return True if the Accept header indicates JSON support"""
|
||||
"""Return True if the Accept header indicates JSON support."""
|
||||
|
||||
accept = self.get_header('Accept')
|
||||
accept = self._get_header_by_wsgi_name('ACCEPT')
|
||||
return ((accept is not None) and
|
||||
(('application/json' in accept) or ('*/*' in accept)))
|
||||
|
||||
@property
|
||||
def client_accepts_xml(self):
|
||||
"""Return True if the Accept header indicates XML support."""
|
||||
|
||||
accept = self._get_header_by_wsgi_name('ACCEPT')
|
||||
return ((accept is not None) and
|
||||
(('application/xml' in accept) or ('*/*' in accept)))
|
||||
|
||||
@property
|
||||
def accept(self):
|
||||
"""Value of the Accept header, or None if not found."""
|
||||
|
||||
@@ -74,6 +74,23 @@ class TestReqVars(testing.TestBase):
|
||||
|
||||
self.assertEqual(req_noapp.relative_uri, self.relative_uri)
|
||||
|
||||
def test_accept_xml(self):
|
||||
headers = {'Accept': 'application/xml'}
|
||||
req = Request(testing.create_environ(headers=headers))
|
||||
self.assertTrue(req.client_accepts_xml)
|
||||
|
||||
headers = {'Accept': '*/*'}
|
||||
req = Request(testing.create_environ(headers=headers))
|
||||
self.assertTrue(req.client_accepts_xml)
|
||||
|
||||
headers = {'Accept': 'application/json'}
|
||||
req = Request(testing.create_environ(headers=headers))
|
||||
self.assertFalse(req.client_accepts_xml)
|
||||
|
||||
headers = {'Accept': 'application/xm'}
|
||||
req = Request(testing.create_environ(headers=headers))
|
||||
self.assertFalse(req.client_accepts_xml)
|
||||
|
||||
def test_range(self):
|
||||
headers = {'Range': '10-'}
|
||||
req = Request(testing.create_environ(headers=headers))
|
||||
@@ -89,11 +106,11 @@ class TestReqVars(testing.TestBase):
|
||||
|
||||
headers = {'Range': ''}
|
||||
req = Request(testing.create_environ(headers=headers))
|
||||
self.assertEqual(req.range, None)
|
||||
self.assertIs(req.range, None)
|
||||
|
||||
headers = {'Range': None}
|
||||
req = Request(testing.create_environ(headers=headers))
|
||||
self.assertEqual(req.range, None)
|
||||
self.assertIs(req.range, None)
|
||||
|
||||
def test_range_invalid(self):
|
||||
headers = {'Range': '10240'}
|
||||
|
||||
Reference in New Issue
Block a user