Merge "Convert requests response from byte to string"
This commit is contained in:
commit
5be096a324
|
@ -122,6 +122,11 @@ def url_fetch(url, timeout=1, allowed_schemes=('http', 'https'), verify=True):
|
|||
reader = resp.iter_content(chunk_size=1000)
|
||||
result = ""
|
||||
for chunk in reader:
|
||||
if six.PY3 and isinstance(chunk, bytes):
|
||||
# in python 2.7, bytes were implicitly converted to strings
|
||||
# in python 3.5 this is no longer the case so we need this
|
||||
# code to manually convert it
|
||||
chunk = chunk.decode('utf-8')
|
||||
result += chunk
|
||||
if len(result) > cfg.CONF.max_response_size:
|
||||
raise URLFetchError("Data exceeds maximum allowed size (%s"
|
||||
|
|
|
@ -122,6 +122,28 @@ class UrlFetchTest(base.SenlinTestCase):
|
|||
utils.url_fetch, url)
|
||||
self.assertIn("Data exceeds", six.text_type(exception))
|
||||
|
||||
@mock.patch.object(requests, 'get')
|
||||
def test_string_response(self, mock_get):
|
||||
url = 'http://example.com/somedata'
|
||||
data = '{ "foo": "bar" }'
|
||||
|
||||
mock_resp = mock.Mock()
|
||||
mock_resp.iter_content.return_value = [data]
|
||||
mock_get.return_value = mock_resp
|
||||
|
||||
self.assertEqual(data, utils.url_fetch(url))
|
||||
|
||||
@mock.patch.object(requests, 'get')
|
||||
def test_byte_response(self, mock_get):
|
||||
url = 'http://example.com/somedata'
|
||||
data = b'{ "foo": "bar" }'
|
||||
|
||||
mock_resp = mock.Mock()
|
||||
mock_resp.iter_content.return_value = [data]
|
||||
mock_get.return_value = mock_resp
|
||||
|
||||
self.assertEqual('{ "foo": "bar" }', utils.url_fetch(url))
|
||||
|
||||
|
||||
class TestRandomName(base.SenlinTestCase):
|
||||
|
||||
|
|
Loading…
Reference in New Issue