From 722b8548b624acf99fe75aecbde4cb1a2867a8a0 Mon Sep 17 00:00:00 2001 From: Alejandro Cabrera Date: Wed, 8 May 2013 09:53:11 -0400 Subject: [PATCH] feat(exception_411): add new exception, HTTP 411. --- falcon/exceptions.py | 21 +++++++++++++++++++++ falcon/tests/test_httperror.py | 15 +++++++++++++++ tools/test-requires | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/falcon/exceptions.py b/falcon/exceptions.py index a17ceea..76ba999 100644 --- a/falcon/exceptions.py +++ b/falcon/exceptions.py @@ -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 diff --git a/falcon/tests/test_httperror.py b/falcon/tests/test_httperror.py index 3ddc31d..6538e8e 100644 --- a/falcon/tests/test_httperror.py +++ b/falcon/tests/test_httperror.py @@ -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()) diff --git a/tools/test-requires b/tools/test-requires index d6b9853..d9918a7 100644 --- a/tools/test-requires +++ b/tools/test-requires @@ -2,4 +2,4 @@ coverage nose ordereddict six -testtools \ No newline at end of file +testtools