feat(exception_411): add new exception, HTTP 411.

This commit is contained in:
Alejandro Cabrera
2013-05-08 09:53:11 -04:00
committed by kgriffs
parent 624e57477b
commit 722b8548b6
3 changed files with 37 additions and 1 deletions

View File

@@ -214,6 +214,27 @@ class HTTPConflict(HTTPError):
HTTPError.__init__(self, status.HTTP_409, title, description, **kwargs)
class HTTPLengthRequired(HTTPError):
"""411 Length Required
From RFC 2616:
"The server refuses to accept the request without a defined
Content- Length. The client MAY repeat the request if it adds a
valid Content-Length header field containing the length of the
message-body in the request message."
"""
def __init__(self, title, description, **kwargs):
"""Initialize
Args:
Same as for HTTPError, except status is set for you.
"""
HTTPError.__init__(self, status.HTTP_411, title, description, **kwargs)
class HTTPPreconditionFailed(HTTPError):
"""412 Precondition Failed

View File

@@ -89,6 +89,12 @@ class MethodNotAllowedResource:
raise falcon.HTTPMethodNotAllowed(['PUT'])
class LengthRequiredResource:
def on_get(self, req, resp):
raise falcon.HTTPLengthRequired('title', 'description')
class RangeNotSatisfiableResource:
def on_get(self, req, resp):
@@ -277,6 +283,15 @@ class TestHTTPError(testing.TestBase):
self.assertEqual(body, [])
self.assertIn(('Allow', 'PUT'), self.srmock.headers)
def test_411(self):
self.api.add_route('/411', LengthRequiredResource())
body = self.simulate_request('/411')
parsed_body = json.loads(body[0].decode())
self.assertEqual(self.srmock.status, falcon.HTTP_411)
self.assertEqual(parsed_body['title'], 'title')
self.assertEqual(parsed_body['description'], 'description')
def test_416_default_media_type(self):
self.api = falcon.API('application/xml')
self.api.add_route('/416', RangeNotSatisfiableResource())

View File

@@ -2,4 +2,4 @@ coverage
nose
ordereddict
six
testtools
testtools