summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2015-06-22 13:33:20 +0000
committerGerrit Code Review <review@openstack.org>2015-06-22 13:33:20 +0000
commit2f78940871237c19256040d23eee03d8b067a589 (patch)
tree6864aa76dd4049356cb91e0a5b8623b079bda4c9
parentcf5b410704260a688f4d61a8fbf15987cae33bc6 (diff)
parentecdb1b06c4638fab9816f0dd2278c939d14f5886 (diff)
downloadoslo-middleware-2f78940871237c19256040d23eee03d8b067a589.tar.gz
Merge "Added request to parse_response"
-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