Skip to content
Browse files

Added headers.content_is_json property.

JSON is so common now that having this a comfortable shortcut.
  • Loading branch information...
1 parent dc068e2 commit 2ab435554736fe34a4b5be253939fb5142a7f6d5 @JNRowe JNRowe committed with Feb 9, 2012
Showing with 15 additions and 0 deletions.
  1. +14 −0 http/headers.py
  2. +1 −0 tests/test_headers.py
View
14 http/headers.py
@@ -158,6 +158,20 @@ def content_length(self):
return int(self.get('Content-Length'))
@property
+ def content_is_json(self):
+ """
+ Returns True if the "Content-Type" header is set to application/json
+
+ :rtype: boolean
+ """
+ ct = self.content_type
+ if ct is None:
+ return False
+ if ct == 'application/json':
+ return True
+ return False
+
+ @property
def content_is_text(self):
"""
Returns True if the "Content-Type" header is set to text
View
1 tests/test_headers.py
@@ -57,6 +57,7 @@ def test_ct(ct, should_be):
method = getattr(headers, should_be)
self.assertTrue(method)
+ test_ct('application/json', 'content_is_json')
test_ct('text/html', 'content_is_text')
test_ct('application/xhtml+xml', 'content_is_xhtml')
test_ct('text/xml', 'content_is_xml')

0 comments on commit 2ab4355

Please sign in to comment.
Something went wrong with that request. Please try again.