refactor: client_accepts_* -> client_accepts

This commit is contained in:
Zhihao Yuan
2013-07-10 16:04:10 -04:00
committed by kgriffs
parent d9a03e5107
commit cb468de5ee
3 changed files with 19 additions and 11 deletions

View File

@@ -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('application/json'):
resp.body = ex.json()
except TypeError as ex:

View File

@@ -138,18 +138,19 @@ class Request(object):
@property
def client_accepts_json(self):
"""Return True if the Accept header indicates JSON support."""
accept = self._get_header_by_wsgi_name('ACCEPT')
return ((accept is not None) and
(('application/json' in accept) or ('*/*' in accept)))
return self.client_accepts('application/json')
@property
def client_accepts_xml(self):
"""Return True if the Accept header indicates XML support."""
return self.client_accepts('application/xml')
def client_accepts(self, media_type):
"""Return True if the Accept header indicates a media type support."""
accept = self._get_header_by_wsgi_name('ACCEPT')
return ((accept is not None) and
(('application/xml' in accept) or ('*/*' in accept)))
((media_type in accept) or ('*/*' in accept)))
@property
def accept(self):

View File

@@ -83,22 +83,29 @@ class TestReqVars(testing.TestBase):
self.assertEqual(req_noapp.relative_uri, self.relative_uri)
def test_accept_xml(self):
def test_client_accepts(self):
headers = {'Accept': 'application/xml'}
req = Request(testing.create_environ(headers=headers))
self.assertTrue(req.client_accepts_xml)
self.assertTrue(req.client_accepts('application/xml'))
headers = {'Accept': '*/*'}
req = Request(testing.create_environ(headers=headers))
self.assertTrue(req.client_accepts_xml)
self.assertTrue(req.client_accepts('application/xml'))
headers = {'Accept': 'application/json'}
req = Request(testing.create_environ(headers=headers))
self.assertFalse(req.client_accepts_xml)
self.assertFalse(req.client_accepts('application/xml'))
headers = {'Accept': 'application/xm'}
req = Request(testing.create_environ(headers=headers))
self.assertFalse(req.client_accepts_xml)
self.assertFalse(req.client_accepts('application/xml'))
def test_client_accepts_props(self):
headers = {'Accept': 'application/xml'}
req = Request(testing.create_environ(headers=headers))
self.assertTrue(req.client_accepts_xml)
self.assertFalse(req.client_accepts_json)
def test_range(self):
headers = {'Range': '10-'}