From ed6ae95fa2ea529088d288e3e5f4099e8e1a2520 Mon Sep 17 00:00:00 2001 From: Samuel Pilla Date: Tue, 17 Jan 2017 09:09:02 -0600 Subject: [PATCH] Correctly Omit Response Body in Debug Mode A response with header Content-Type set to "application/json; charset=UTF-8" would be omitted but not correctly logged. This patch set correctly omits and logs a response with the mentioned header. Co-Authored-By: Tin Lam Change-Id: I21a185db4ca55ff16dba60f85bb229ffdacc2afa Closes-Bug: #1656981 --- keystoneauth1/session.py | 11 +++++++++-- keystoneauth1/tests/unit/test_session.py | 10 ++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/keystoneauth1/session.py b/keystoneauth1/session.py index 40550b49..ae6204b5 100644 --- a/keystoneauth1/session.py +++ b/keystoneauth1/session.py @@ -366,8 +366,15 @@ class Session(object): # stream of bytes and getting an unexpected MemoryError. See # bug 1616105 for further details. content_type = response.headers.get('content-type', None) - if content_type in _LOG_CONTENT_TYPES: - text = self._remove_service_catalog(response.text) + + # NOTE(lamt): Per [1], the Content-Type header can be of the + # form Content-Type := type "/" subtype *[";" parameter] + # [1] https://www.w3.org/Protocols/rfc1341/4_Content-Type.html + for log_type in _LOG_CONTENT_TYPES: + if content_type is not None and content_type.startswith( + log_type): + text = self._remove_service_catalog(response.text) + break else: text = ('Omitted, Content-Type is set to %s. Only ' '%s responses have their bodies logged.') diff --git a/keystoneauth1/tests/unit/test_session.py b/keystoneauth1/tests/unit/test_session.py index ee2049c9..bae2408a 100644 --- a/keystoneauth1/tests/unit/test_session.py +++ b/keystoneauth1/tests/unit/test_session.py @@ -265,6 +265,16 @@ class SessionTests(utils.TestCase): self.assertIn(body, self.logger.output) self.assertNotIn(OMITTED_BODY % 'application/json', self.logger.output) + # Content-Type is set to application/json; charset=UTF-8 + body = json.dumps({'token': {'id': '...'}}) + self.stub_url( + 'POST', text=body, + headers={'Content-Type': 'application/json; charset=UTF-8'}) + session.post(self.TEST_URL) + self.assertIn(body, self.logger.output) + self.assertNotIn(OMITTED_BODY % 'application/json; charset=UTF-8', + self.logger.output) + def test_logging_cacerts(self): path_to_certs = '/path/to/certs' session = client_session.Session(verify=path_to_certs)