From ba19a534b7576846d542f9c6a4d6769253c603e8 Mon Sep 17 00:00:00 2001 From: Matt Riedemann Date: Mon, 15 Sep 2014 14:55:28 -0700 Subject: Handle UnicodeDecodeError in log_http_response Similar to commit dec9c9f35 and log_curl_request, this ignores decoding errors when logging response headers by passing errors='ignore' to safe_encode. Change-Id: Ic915a7d8334e9473f300c9db670a3a8f5cda8976 Closes-Bug: #1369756 --- glanceclient/common/http.py | 3 ++- tests/test_http.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/glanceclient/common/http.py b/glanceclient/common/http.py index 8c7937a..91193db 100644 --- a/glanceclient/common/http.py +++ b/glanceclient/common/http.py @@ -130,7 +130,8 @@ class HTTPClient(object): if body: body = strutils.safe_decode(body) dump.extend([body, '']) - LOG.debug('\n'.join([strutils.safe_encode(x) for x in dump])) + LOG.debug('\n'.join([strutils.safe_encode(x, errors='ignore') + for x in dump])) @staticmethod def encode_headers(headers): diff --git a/tests/test_http.py b/tests/test_http.py index 0c8a4ac..89bc2af 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -232,6 +232,16 @@ class TestClient(testtools.TestCase): self.assertTrue(isinstance(body, types.GeneratorType)) self.assertEqual([data], list(body)) + def test_log_http_response_with_non_ascii_char(self): + try: + response = 'Ok' + headers = {"Content-Type": "text/plain", + "test": "value1\xa5\xa6"} + fake = utils.FakeResponse(headers, six.StringIO(response)) + self.client.log_http_response(fake) + except UnicodeDecodeError as e: + self.fail("Unexpected UnicodeDecodeError exception '%s'" % e) + class TestVerifiedHTTPSConnection(testtools.TestCase): """Test fixture for glanceclient.common.http.VerifiedHTTPSConnection.""" -- cgit v1.2.1