Merge "Use WebOb directly for locale testing"

This commit is contained in:
Jenkins 2014-02-13 04:28:33 +00:00 committed by Gerrit Code Review
commit 03b60340e8
4 changed files with 33 additions and 33 deletions

View File

@ -100,16 +100,15 @@ def validate_token_bind(context, token_ref):
raise exception.Unauthorized()
class Request(webob.Request):
def best_match_language(self):
"""Determines the best available locale from the Accept-Language
HTTP header passed in the request.
"""
def best_match_language(req):
"""Determines the best available locale from the Accept-Language
HTTP header passed in the request.
"""
if not self.accept_language:
return None
return self.accept_language.best_match(
gettextutils.get_available_languages('keystone'))
if not req.accept_language:
return None
return req.accept_language.best_match(
gettextutils.get_available_languages('keystone'))
class BaseApplication(object):
@ -143,7 +142,7 @@ class BaseApplication(object):
def __call__(self, environ, start_response):
r"""Subclasses will probably want to implement __call__ like this:
@webob.dec.wsgify(RequestClass=Request)
@webob.dec.wsgify()
def __call__(self, req):
# Any of the following objects work as responses:
@ -179,7 +178,7 @@ class BaseApplication(object):
@dependency.requires('assignment_api', 'policy_api', 'token_api')
class Application(BaseApplication):
@webob.dec.wsgify(RequestClass=Request)
@webob.dec.wsgify()
def __call__(self, req):
arg_dict = req.environ['wsgiorg.routing_args'][1]
action = arg_dict.pop('action')
@ -214,18 +213,18 @@ class Application(BaseApplication):
LOG.warning(
_('Authorization failed. %(exception)s from %(remote_addr)s'),
{'exception': e, 'remote_addr': req.environ['REMOTE_ADDR']})
return render_exception(e, user_locale=req.best_match_language())
return render_exception(e, user_locale=best_match_language(req))
except exception.Error as e:
LOG.warning(e)
return render_exception(e, user_locale=req.best_match_language())
return render_exception(e, user_locale=best_match_language(req))
except TypeError as e:
LOG.exception(e)
return render_exception(exception.ValidationError(e),
user_locale=req.best_match_language())
user_locale=best_match_language(req))
except Exception as e:
LOG.exception(e)
return render_exception(exception.UnexpectedError(exception=e),
user_locale=req.best_match_language())
user_locale=best_match_language(req))
if result is None:
return render_response(status=(204, 'No Content'))
@ -360,7 +359,7 @@ class Middleware(Application):
"""Do whatever you'd like to the response, based on the request."""
return response
@webob.dec.wsgify(RequestClass=Request)
@webob.dec.wsgify()
def __call__(self, request):
try:
response = self.process_request(request)
@ -371,15 +370,15 @@ class Middleware(Application):
except exception.Error as e:
LOG.warning(e)
return render_exception(e,
user_locale=request.best_match_language())
user_locale=best_match_language(request))
except TypeError as e:
LOG.exception(e)
return render_exception(exception.ValidationError(e),
user_locale=request.best_match_language())
user_locale=best_match_language(request))
except Exception as e:
LOG.exception(e)
return render_exception(exception.UnexpectedError(exception=e),
user_locale=request.best_match_language())
user_locale=best_match_language(request))
class Debug(Middleware):
@ -390,7 +389,7 @@ class Debug(Middleware):
"""
@webob.dec.wsgify(RequestClass=Request)
@webob.dec.wsgify()
def __call__(self, req):
if not hasattr(LOG, 'isEnabledFor') or LOG.isEnabledFor(LOG.debug):
LOG.debug('%s %s %s', ('*' * 20), 'REQUEST ENVIRON', ('*' * 20))
@ -454,7 +453,7 @@ class Router(object):
self._router = routes.middleware.RoutesMiddleware(self._dispatch,
self.map)
@webob.dec.wsgify(RequestClass=Request)
@webob.dec.wsgify()
def __call__(self, req):
"""Route the incoming request to a controller based on self.map.
@ -464,7 +463,7 @@ class Router(object):
return self._router
@staticmethod
@webob.dec.wsgify(RequestClass=Request)
@webob.dec.wsgify()
def _dispatch(req):
"""Dispatch the request to the appropriate controller.
@ -477,7 +476,7 @@ class Router(object):
if not match:
return render_exception(
exception.NotFound(_('The resource could not be found.')),
user_locale=req.best_match_language())
user_locale=best_match_language(req))
app = match['controller']
return app

View File

@ -191,7 +191,7 @@ class RequestBodySizeLimiter(wsgi.Middleware):
def __init__(self, *args, **kwargs):
super(RequestBodySizeLimiter, self).__init__(*args, **kwargs)
@webob.dec.wsgify(RequestClass=wsgi.Request)
@webob.dec.wsgify()
def __call__(self, req):
if req.content_length > CONF.max_request_body_size:

View File

@ -29,6 +29,7 @@ from paste import deploy
import six
import testtools
from testtools import testcase
import webob
from keystone.openstack.common.fixture import mockpatch
from keystone.openstack.common import gettextutils
@ -55,7 +56,6 @@ from keystone.common import kvs
from keystone.common.kvs import core as kvs_core
from keystone.common import sql
from keystone.common import utils
from keystone.common import wsgi
from keystone import config
from keystone import exception
from keystone import notifications
@ -239,7 +239,7 @@ class TestClient(object):
if self.token:
headers.setdefault('X-Auth-Token', self.token)
req = wsgi.Request.blank(path)
req = webob.Request.blank(path)
req.method = method
for k, v in six.iteritems(headers):
req.headers[k] = v

View File

@ -16,6 +16,7 @@ from babel import localedata
import gettext
import mock
import socket
import webob
from keystone.common import environment
from keystone.common import wsgi
@ -37,7 +38,7 @@ class BaseWSGITest(tests.TestCase):
super(BaseWSGITest, self).setUp()
def _make_request(self, url='/'):
req = wsgi.Request.blank(url)
req = webob.Request.blank(url)
args = {'action': 'index', 'controller': None}
req.environ['wsgiorg.routing_args'] = [None, args]
return req
@ -218,8 +219,8 @@ class LocalizedResponseTest(tests.TestCase):
def test_request_match_default(self):
# The default language if no Accept-Language is provided is None
req = wsgi.Request.blank('/')
self.assertIsNone(req.best_match_language())
req = webob.Request.blank('/')
self.assertIsNone(wsgi.best_match_language(req))
def test_request_match_language_expected(self):
# If Accept-Language is a supported language, best_match_language()
@ -227,8 +228,8 @@ class LocalizedResponseTest(tests.TestCase):
self._set_expected_languages(all_locales=['it'])
req = wsgi.Request.blank('/', headers={'Accept-Language': 'it'})
self.assertEqual(req.best_match_language(), 'it')
req = webob.Request.blank('/', headers={'Accept-Language': 'it'})
self.assertEqual(wsgi.best_match_language(req), 'it')
def test_request_match_language_unexpected(self):
# If Accept-Language is a language we do not support,
@ -236,8 +237,8 @@ class LocalizedResponseTest(tests.TestCase):
self._set_expected_languages(all_locales=['it'])
req = wsgi.Request.blank('/', headers={'Accept-Language': 'zh'})
self.assertIsNone(req.best_match_language())
req = webob.Request.blank('/', headers={'Accept-Language': 'zh'})
self.assertIsNone(wsgi.best_match_language(req))
def test_static_translated_string_is_Message(self):
# Statically created message strings are Message objects so that they