diff --git a/paypalhttp/http_client.py b/paypalhttp/http_client.py index f1422db..6b69e3e 100644 --- a/paypalhttp/http_client.py +++ b/paypalhttp/http_client.py @@ -69,6 +69,7 @@ def map_headers(self, raw_headers, formatted_headers): return raw_headers def parse_response(self, response): + response.encoding = response.encoding or 'utf-8' status_code = response.status_code if 200 <= status_code <= 299: diff --git a/paypalhttp/testutils/testharness.py b/paypalhttp/testutils/testharness.py index fe0fc09..577328c 100644 --- a/paypalhttp/testutils/testharness.py +++ b/paypalhttp/testutils/testharness.py @@ -15,7 +15,7 @@ def stub_request_with_empty_reponse(self, request): def stub_request_with_response(self, request, response_body="", status=200, content_type="application/json"): body = None if response_body: - if isinstance(response_body, str): + if isinstance(response_body, (bytes, str)): body = response_body else: body = json.dumps(response_body) diff --git a/tests/http_client_test.py b/tests/http_client_test.py index 56d36e3..8cffdf6 100644 --- a/tests/http_client_test.py +++ b/tests/http_client_test.py @@ -202,5 +202,18 @@ def test_HttpClient_execute_filesArePreservedInCopy(self): license.close() + @responses.activate + def test_HttpClient_encoding(self): + client = HttpClient(self.environment()) + + request = GenericRequest() + request.path = "/error" + request.verb = "GET" + self.stub_request_with_response( + request, status=200, response_body=u'{"city": "D\u00fcsseldorf"}'.encode('utf-8')) + + resp = client.execute(request) + self.assertEqual(resp.result.city, 'D\u00fcsseldorf') + if __name__ == '__main__': unittest.main()