Port test_utils to Python 3

* Encode explicitly JSON to UTF-8 to get HTTP body as bytes
  on Python 3.
* Use a literal byte string, instead of a native string, for the HTTP
  body.

Change-Id: I1ee88e1abcded9dfaf3f1d9f720782b0d7ad662d
This commit is contained in:
Victor Stinner
2015-09-10 13:02:32 +02:00
parent e2f1e1d36b
commit 671a646f61

View File

@@ -165,10 +165,14 @@ class TestUtils(base.UnitTestCase):
def test_get_error_body_get_from_json(self):
error_body = 'This is error body'
resp = requests.Response()
resp._content = json.dumps({
body_json = json.dumps({
'message': error_body
})
if isinstance(body_json, six.text_type):
body_json = body_json.encode('utf-8')
resp = requests.Response()
resp._content = body_json
exception = requests.HTTPError()
exception.response = resp
@@ -176,7 +180,7 @@ class TestUtils(base.UnitTestCase):
self.assertEqual(error.get_error_body(exception), error_body)
def test_get_error_body_get_from_plaintext(self):
error_body = 'This is error body'
error_body = b'This is error body'
resp = requests.Response()
resp._content = error_body
@@ -184,7 +188,8 @@ class TestUtils(base.UnitTestCase):
exception = requests.HTTPError()
exception.response = resp
self.assertEqual(error.get_error_body(exception), error_body)
self.assertEqual(error.get_error_body(exception),
error_body.decode('utf-8'))
def test_get_display_data_single(self):
test_data = {'a': 1, 'b': 2, 'c': 3}