|
| 1 | +# Copyright (c) 2013 OpenStack, LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +import webob |
| 16 | + |
| 17 | +from keystone import config |
| 18 | +from keystone import exception |
| 19 | +from keystone import middleware |
| 20 | +from keystone import test |
| 21 | + |
| 22 | +CONF = config.CONF |
| 23 | +MAX_REQUEST_BODY_SIZE = CONF.max_request_body_size |
| 24 | + |
| 25 | + |
| 26 | +class TestRequestBodySizeLimiter(test.TestCase): |
| 27 | + |
| 28 | + def setUp(self): |
| 29 | + super(TestRequestBodySizeLimiter, self).setUp() |
| 30 | + |
| 31 | + @webob.dec.wsgify() |
| 32 | + def fake_app(req): |
| 33 | + return webob.Response(req.body) |
| 34 | + |
| 35 | + self.middleware = middleware.RequestBodySizeLimiter(fake_app) |
| 36 | + self.request = webob.Request.blank('/', method='POST') |
| 37 | + |
| 38 | + def test_content_length_acceptable(self): |
| 39 | + self.request.headers['Content-Length'] = MAX_REQUEST_BODY_SIZE |
| 40 | + self.request.body = "0" * MAX_REQUEST_BODY_SIZE |
| 41 | + response = self.request.get_response(self.middleware) |
| 42 | + self.assertEqual(response.status_int, 200) |
| 43 | + |
| 44 | + def test_content_length_too_large(self): |
| 45 | + self.request.headers['Content-Length'] = MAX_REQUEST_BODY_SIZE + 1 |
| 46 | + self.request.body = "0" * (MAX_REQUEST_BODY_SIZE + 1) |
| 47 | + self.assertRaises(exception.RequestTooLarge, |
| 48 | + self.request.get_response, |
| 49 | + self.middleware) |
| 50 | + |
| 51 | + def test_request_too_large_no_content_length(self): |
| 52 | + self.request.body = "0" * (MAX_REQUEST_BODY_SIZE + 1) |
| 53 | + self.request.headers['Content-Length'] = None |
| 54 | + self.assertRaises(exception.RequestTooLarge, |
| 55 | + self.request.get_response, |
| 56 | + self.middleware) |
0 commit comments