diff options
author | Cyril Roelandt <cyril.roelandt@enovance.com> | 2014-03-14 03:10:41 +0100 |
---|---|---|
committer | Cyril Roelandt <cyril.roelandt@enovance.com> | 2014-03-14 03:10:41 +0100 |
commit | 9c33362b90002a5500e9a3f243ebac96f24e23ad (patch) | |
tree | fb696823fc6ae5832509d5af3c39b480c3320f6c | |
parent | 3a67978cf41aecc752b7c11e2955d234a1add7bf (diff) | |
download | oslo-middleware-9c33362b90002a5500e9a3f243ebac96f24e23ad.tar.gz |
sizelimit: port to Python 3
In the tests, use bytes for the request body, since that is what is used by
webob in Python 3.
In RequestBodySizeLimiter.__call__(), do not compare NoneType to an integer: it
is forbidden in Python 3. Instead, properly handle the fact that
req.content_lenght might be None.
Change-Id: I179997789b9e2fcd3f3326e4d724e547c807f195
-rw-r--r-- | openstack/common/middleware/sizelimit.py | 3 | ||||
-rw-r--r-- | tests/unit/middleware/test_sizelimit.py | 6 |
2 files changed, 5 insertions, 4 deletions
diff --git a/openstack/common/middleware/sizelimit.py b/openstack/common/middleware/sizelimit.py index 64c12d9..4036b2a 100644 --- a/openstack/common/middleware/sizelimit.py +++ b/openstack/common/middleware/sizelimit.py @@ -71,7 +71,8 @@ class RequestBodySizeLimiter(base.Middleware): @webob.dec.wsgify def __call__(self, req): - if req.content_length > CONF.max_request_body_size: + if (req.content_length is not None and + req.content_length > CONF.max_request_body_size): msg = _("Request is too large.") raise webob.exc.HTTPRequestEntityTooLarge(explanation=msg) if req.content_length is None and req.is_body_readable: diff --git a/tests/unit/middleware/test_sizelimit.py b/tests/unit/middleware/test_sizelimit.py index 0f14b41..0b471c3 100644 --- a/tests/unit/middleware/test_sizelimit.py +++ b/tests/unit/middleware/test_sizelimit.py @@ -82,18 +82,18 @@ class TestRequestBodySizeLimiter(test.BaseTestCase): def test_content_length_acceptable(self): self.request.headers['Content-Length'] = self.MAX_REQUEST_BODY_SIZE - self.request.body = "0" * self.MAX_REQUEST_BODY_SIZE + self.request.body = b"0" * self.MAX_REQUEST_BODY_SIZE response = self.request.get_response(self.middleware) self.assertEqual(response.status_int, 200) def test_content_length_too_large(self): self.request.headers['Content-Length'] = self.MAX_REQUEST_BODY_SIZE + 1 - self.request.body = "0" * (self.MAX_REQUEST_BODY_SIZE + 1) + self.request.body = b"0" * (self.MAX_REQUEST_BODY_SIZE + 1) response = self.request.get_response(self.middleware) self.assertEqual(response.status_int, 413) def test_request_too_large_no_content_length(self): - self.request.body = "0" * (self.MAX_REQUEST_BODY_SIZE + 1) + self.request.body = b"0" * (self.MAX_REQUEST_BODY_SIZE + 1) self.request.headers['Content-Length'] = None response = self.request.get_response(self.middleware) self.assertEqual(response.status_int, 413) |