Skip to content

Commit

Permalink
Handle api_requests with binary content correctly in Python 3. Added …
Browse files Browse the repository at this point in the history
…test.
  • Loading branch information
Ivan Vazquez committed Feb 20, 2015
1 parent 718fc7f commit 9066b7e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gcloud/storage/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ def api_request(self, method, path, query_params=None,
if not 200 <= response.status < 300:
raise make_exception(response, content)

if not isinstance(content, str):
content = str(content.decode('utf-8'))

if content and expect_json:
content_type = response.get('content-type', '')
if not content_type.startswith('application/json'):
Expand Down
10 changes: 10 additions & 0 deletions gcloud/storage/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,16 @@ def test_api_request_wo_json_expected(self):
self.assertEqual(conn.api_request('GET', '/', expect_json=False),
'CONTENT')

def test_api_request_w_binary_json_string(self):
PROJECT = 'project'
conn = self._makeOne(PROJECT)
conn._http = Http(
{'status': '200', 'content-type': 'application/json'},
b'{"foo": "bar"}'
)
self.assertEqual(conn.api_request('GET', '/', expect_json=True),
{'foo': 'bar'})

def test_api_request_w_query_params(self):
from six.moves.urllib.parse import parse_qsl
from six.moves.urllib.parse import urlsplit
Expand Down

0 comments on commit 9066b7e

Please sign in to comment.