Move to falcon middleware

Instead of using the now removed hook feature of falcon, let's use
the middleware feature.

NOTE: Though the test is changed. But actually it's not correct since
the unicode test with a running zaqar server will return 404 instead
of 400. But it's out of the scope of this fix. Will fix it in a
following patch.

Co-Authored-By: Fei Long Wang <flwang@catalyst.net.nz>

Closes-Bug: #1581189
Change-Id: I7e8e97b73ec3e245580f07e62442caf0395d5745
This commit is contained in:
Thomas Herve 2016-05-12 21:30:20 +02:00 committed by Fei Long Wang
parent 2cc70f2659
commit 3f15c0bee1
2 changed files with 19 additions and 2 deletions

View File

@ -12,8 +12,10 @@
# License for the specific language governing permissions and limitations under
# the License.
import six
import uuid
import falcon
from falcon import testing as ftest
from oslo_serialization import jsonutils
@ -88,6 +90,12 @@ class TestBase(testing.TestBase):
is None else project_id)
headers['X-Project-ID'] = headers.get('X-Project-ID', project_id)
kwargs['headers'] = headers
try:
if six.PY3:
path.encode('latin1').decode('utf-8', 'replace')
except UnicodeEncodeError:
self.srmock.status = falcon.HTTP_400
return
return self.app(ftest.create_environ(path=path, **kwargs),
self.srmock)

View File

@ -51,6 +51,15 @@ def _config_options():
return [(_WSGI_GROUP, _WSGI_OPTIONS)]
class FuncMiddleware(object):
def __init__(self, func):
self.func = func
def process_resource(self, req, resp, resource, params):
return self.func(req, resp, params)
class Driver(transport.DriverBase):
def __init__(self, conf, storage, cache, control):
@ -106,8 +115,8 @@ class Driver(transport.DriverBase):
('/v1.1', v1_1.private_endpoints(self, self._conf)),
('/v2', v2_0.private_endpoints(self, self._conf)),
])
self.app = falcon.API(before=self.before_hooks)
middleware = [FuncMiddleware(hook) for hook in self.before_hooks]
self.app = falcon.API(middleware=middleware)
self.app.add_error_handler(Exception, self._error_handler)
for version_path, endpoints in catalog: