Skip to content

Commit 9066b7e

Browse files
committed
Handle api_requests with binary content correctly in Python 3. Added test.
1 parent 718fc7f commit 9066b7e

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

gcloud/storage/connection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ def api_request(self, method, path, query_params=None,
235235
if not 200 <= response.status < 300:
236236
raise make_exception(response, content)
237237

238+
if not isinstance(content, str):
239+
content = str(content.decode('utf-8'))
240+
238241
if content and expect_json:
239242
content_type = response.get('content-type', '')
240243
if not content_type.startswith('application/json'):

gcloud/storage/test_connection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,16 @@ def test_api_request_wo_json_expected(self):
206206
self.assertEqual(conn.api_request('GET', '/', expect_json=False),
207207
'CONTENT')
208208

209+
def test_api_request_w_binary_json_string(self):
210+
PROJECT = 'project'
211+
conn = self._makeOne(PROJECT)
212+
conn._http = Http(
213+
{'status': '200', 'content-type': 'application/json'},
214+
b'{"foo": "bar"}'
215+
)
216+
self.assertEqual(conn.api_request('GET', '/', expect_json=True),
217+
{'foo': 'bar'})
218+
209219
def test_api_request_w_query_params(self):
210220
from six.moves.urllib.parse import parse_qsl
211221
from six.moves.urllib.parse import urlsplit

0 commit comments

Comments
 (0)