diff --git a/swift/proxy/controllers/base.py b/swift/proxy/controllers/base.py index c2ab760cbb..b4569c4caf 100644 --- a/swift/proxy/controllers/base.py +++ b/swift/proxy/controllers/base.py @@ -1,4 +1,4 @@ -# Copyright (c) 2010-2012 OpenStack Foundation +# Copyright (c) 2010-2016 OpenStack Foundation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -475,6 +475,10 @@ def _get_info_cache(app, env, account, container=None): for key in info: if isinstance(info[key], six.text_type): info[key] = info[key].encode("utf-8") + if isinstance(info[key], dict): + for subkey, value in info[key].items(): + if isinstance(value, six.text_type): + info[key][subkey] = value.encode("utf-8") env[env_key] = info return info return None diff --git a/test/unit/proxy/test_server.py b/test/unit/proxy/test_server.py index ab300928d3..37ad4ea407 100644 --- a/test/unit/proxy/test_server.py +++ b/test/unit/proxy/test_server.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright (c) 2010-2012 OpenStack Foundation +# Copyright (c) 2010-2016 OpenStack Foundation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -71,7 +71,7 @@ from swift.common.utils import mkdirs, normalize_timestamp, NullLogger from swift.common.wsgi import monkey_patch_mimetools, loadapp from swift.proxy.controllers import base as proxy_base from swift.proxy.controllers.base import get_container_memcache_key, \ - get_account_memcache_key, cors_validation + get_account_memcache_key, cors_validation, _get_info_cache import swift.proxy.controllers import swift.proxy.controllers.obj from swift.common.swob import Request, Response, HTTPUnauthorized, \ @@ -681,6 +681,37 @@ class TestController(unittest.TestCase): test(404, 507, 503) test(503, 503, 503) + def test_get_info_cache_returns_values_as_strings(self): + app = mock.MagicMock() + app.memcache = mock.MagicMock() + app.memcache.get = mock.MagicMock() + app.memcache.get.return_value = { + u'foo': u'\u2603', + u'meta': {u'bar': u'\u2603'}, + u'sysmeta': {u'baz': u'\u2603'}, + u'cors': {u'expose_headers': u'\u2603'}} + env = {} + r = _get_info_cache(app, env, 'account', 'container') + + # Test info is returned as strings + self.assertEqual(r.get('foo'), '\xe2\x98\x83') + self.assertTrue(isinstance(r.get('foo'), str)) + + # Test info['meta'] is returned as strings + m = r.get('meta', {}) + self.assertEqual(m.get('bar'), '\xe2\x98\x83') + self.assertTrue(isinstance(m.get('bar'), str)) + + # Test info['sysmeta'] is returned as strings + m = r.get('sysmeta', {}) + self.assertEqual(m.get('baz'), '\xe2\x98\x83') + self.assertTrue(isinstance(m.get('baz'), str)) + + # Test info['cors'] is returned as strings + m = r.get('cors', {}) + self.assertEqual(m.get('expose_headers'), '\xe2\x98\x83') + self.assertTrue(isinstance(m.get('expose_headers'), str)) + @patch_policies([StoragePolicy(0, 'zero', True, object_ring=FakeRing())]) class TestProxyServer(unittest.TestCase):