summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Krotscheck <krotscheck@gmail.com>2015-06-11 11:35:19 -0700
committerMichael Krotscheck <krotscheck@gmail.com>2015-06-16 15:18:24 -0700
commitecdb1b06c4638fab9816f0dd2278c939d14f5886 (patch)
tree5bb4913ffa12ad671caee53bd52e4ebd5cca11cd
parenta999eedb2729da293561e60d9c4a85fc4f57b077 (diff)
downloadoslo-middleware-ecdb1b06c4638fab9816f0dd2278c939d14f5886.tar.gz
Added request to parse_response
This adds the 'request' parameter to the base middleware's parse_response method. It is intended to support middleware whose logic is conditional on the request. For example, response header decorators that respond only to the presence of certain request headers (If-Not-Match, If-Modified-Since, Origin, etc). The request parameter has been made optional, in order to be backwards compatible with older middleware. Additional inspection has been provided, as well as tests for backwards compatibility. Change-Id: I5fa94f74765f14bcf79c48ba4c813929da449be3
-rw-r--r--oslo_middleware/base.py7
-rw-r--r--oslo_middleware/tests/test_base.py81
2 files changed, 87 insertions, 1 deletions
diff --git a/oslo_middleware/base.py b/oslo_middleware/base.py
index 464a1cc..ca998f7 100644
--- a/oslo_middleware/base.py
+++ b/oslo_middleware/base.py
@@ -15,6 +15,7 @@
"""Base class(es) for WSGI Middleware."""
+from inspect import getargspec
import webob.dec
@@ -43,7 +44,7 @@ class Middleware(object):
"""
return None
- def process_response(self, response):
+ def process_response(self, response, request=None):
"""Do whatever you'd like to the response."""
return response
@@ -53,4 +54,8 @@ class Middleware(object):
if response:
return response
response = req.get_response(self.application)
+
+ (args, varargs, varkw, defaults) = getargspec(self.process_response)
+ if 'request' in args:
+ return self.process_response(response, request=req)
return self.process_response(response)
diff --git a/oslo_middleware/tests/test_base.py b/oslo_middleware/tests/test_base.py
new file mode 100644
index 0000000..7124a49
--- /dev/null
+++ b/oslo_middleware/tests/test_base.py
@@ -0,0 +1,81 @@
+# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+import webob
+
+from oslo_middleware.base import Middleware
+from oslotest.base import BaseTestCase
+
+
+class TestBase(BaseTestCase):
+ """Test the base middleware class."""
+
+ def setUp(self):
+ """Setup the tests."""
+ super(BaseTestCase, self).setUp()
+
+ def test_extend_with_request(self):
+ """Assert that a newer middleware behaves as appropriate.
+
+ This tests makes sure that the request is passed to the
+ middleware's implementation.
+ """
+ # Create an application.
+ @webob.dec.wsgify
+ def application(req):
+ return 'Hello, World!!!'
+
+ # Bootstrap the application
+ self.application = RequestBase(application)
+
+ # Send a request through.
+ request = webob.Request({}, method='GET')
+ request.get_response(self.application)
+
+ self.assertTrue(self.application.called_with_request)
+
+ def test_extend_without_request(self):
+ """Assert that an older middleware behaves as appropriate.
+
+ This tests makes sure that the request method is NOT passed to the
+ middleware's implementation, and that there are no other expected
+ errors.
+ """
+ # Create an application.
+ @webob.dec.wsgify
+ def application(req):
+ return 'Hello, World!!!'
+
+ # Bootstrap the application
+ self.application = NoRequestBase(application)
+
+ # Send a request through.
+ request = webob.Request({}, method='GET')
+ request.get_response(self.application)
+
+ self.assertTrue(self.application.called_without_request)
+
+
+class NoRequestBase(Middleware):
+ """Test middleware, implements old model."""
+ def process_response(self, response):
+ self.called_without_request = True
+ return response
+
+
+class RequestBase(Middleware):
+ """Test middleware, implements new model."""
+ def process_response(self, response, request):
+ self.called_with_request = True
+ return response