handle bytes list in api middleware

In python3, the inner string is bytes, let's handle it to avoid
failure.
Also update a unit test related to response string.

Partially-Implements: blueprint magnum-python3

Change-Id: Ia85b4e4a49a1cb39a415da764343de001d61234d
This commit is contained in:
Yang Hongyang 2016-02-27 15:04:48 +08:00
parent e8e647a6c2
commit e1d2a38c08
2 changed files with 11 additions and 3 deletions

View File

@ -19,6 +19,7 @@ Based on pecan.middleware.errordocument
"""
import json
import six
from magnum.i18n import _
@ -57,7 +58,8 @@ class ParsableErrorMiddleware(object):
app_iter = self.app(environ, replacement_start_response)
if (state['status_code'] // 100) not in (2, 3):
body = [json.dumps({'error_message': '\n'.join(app_iter)})]
body = [six.b(json.dumps({'error_message':
six.b('\n').join(app_iter).decode('utf-8')}))]
state['headers'].append(('Content-Type', 'application/json'))
state['headers'].append(('Content-Length', str(len(body[0]))))
else:

View File

@ -14,6 +14,7 @@
# limitations under the License.
import json
import six
import mock
from oslo_config import cfg
@ -104,8 +105,13 @@ class TestNoExceptionTracebackHook(api_base.FunctionalTest):
# instead of'\n'.join(trace). But since RemoteError is kind of very
# rare thing (happens due to wrong deserialization settings etc.)
# we don't care about this garbage.
expected_msg = ("Remote error: %s %s"
% (test_exc_type, self.MSG_WITHOUT_TRACE) + "\n[u'")
if six.PY2:
expected_msg = ("Remote error: %s %s"
% (test_exc_type, self.MSG_WITHOUT_TRACE)
+ "\n[u'")
else:
expected_msg = ("Remote error: %s %s"
% (test_exc_type, self.MSG_WITHOUT_TRACE) + "\n['")
actual_msg = json.loads(response.json['error_message'])['faultstring']
self.assertEqual(expected_msg, actual_msg)