From 1df9dbed89b6ecbcbe5d6ac50e5d942bddfd6021 Mon Sep 17 00:00:00 2001 From: Adam Kewley Date: Tue, 1 May 2018 15:59:54 +0100 Subject: [PATCH] - Fixed 'cannot call json.read() on bytes' issue, which can happen in python3 - Happens because python3 HTTPResponse, when read like a file (e.g. with `json.load`), assumes binary - see: https://stackoverflow.com/questions/23049767/parsing-http-response-in-python - Updated test mocks to make unit tests pass --- tests/test_base.py | 5 ++++- yarn_api_client/base.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/test_base.py b/tests/test_base.py index dc9ef52..169234d 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -4,7 +4,7 @@ except ImportError: from http.client import OK -from mock import patch +from mock import patch, MagicMock from tests import TestCase from yarn_api_client import base @@ -17,6 +17,9 @@ def test_request(self): with patch('yarn_api_client.base.HTTPConnection') as http_conn_mock: with patch('yarn_api_client.base.json'): http_conn_mock().getresponse().status = OK + headers_mock = MagicMock() + headers_mock.get_content_charset = MagicMock(return_value="utf-8") + http_conn_mock().getresponse().headers = headers_mock client.request('/ololo', foo='bar') diff --git a/yarn_api_client/base.py b/yarn_api_client/base.py index c86a617..8c41ffc 100644 --- a/yarn_api_client/base.py +++ b/yarn_api_client/base.py @@ -16,7 +16,7 @@ class Response(object): def __init__(self, http_response): - self.data = json.load(http_response) + self.data = json.loads(http_response.read().decode(http_response.headers.get_content_charset('utf-8'))) class BaseYarnAPI(object):