From bc491817e174c35145fb1afd21141f878c103736 Mon Sep 17 00:00:00 2001 From: melanie witt Date: Fri, 2 Oct 2020 00:26:35 +0000 Subject: [PATCH] Allow logging of Content-Type text/plain Noticed this while doing some local testing, if a WSGI app replies with a text/plain content type to communicate a server error, we aren't able to see the error response message when passing --debug to the openstackclient, example: RESP: [500] Date: Thu, 01 Oct 2020 23:54:15 GMT Server: Apache/2.4.18 (Ubuntu) Content-Type: text/plain; charset=UTF-8 Connection: close Transfer-Encoding: chunked RESP BODY: Omitted, Content-Type is set to text/plain; charset=UTF-8. Only application/json responses have their bodies logged. Change-Id: Ibfd46c7725bd0aa26f1f80b0e8fc6eda2ac2e090 --- keystoneauth1/session.py | 2 +- keystoneauth1/tests/unit/test_session.py | 27 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/keystoneauth1/session.py b/keystoneauth1/session.py index 0fa31845..0330c249 100644 --- a/keystoneauth1/session.py +++ b/keystoneauth1/session.py @@ -48,7 +48,7 @@ DEFAULT_USER_AGENT = 'keystoneauth1/%s %s %s/%s' % ( # NOTE(jamielennox): Clients will likely want to print more than json. Please # propose a patch if you have a content type you think is reasonable to print # here and we'll add it to the list as required. -_LOG_CONTENT_TYPES = set(['application/json']) +_LOG_CONTENT_TYPES = set(['application/json', 'text/plain']) _MAX_RETRY_INTERVAL = 60.0 _EXPONENTIAL_DELAY_START = 0.5 diff --git a/keystoneauth1/tests/unit/test_session.py b/keystoneauth1/tests/unit/test_session.py index b191e8d9..cff5b544 100644 --- a/keystoneauth1/tests/unit/test_session.py +++ b/keystoneauth1/tests/unit/test_session.py @@ -383,11 +383,13 @@ class SessionTests(utils.TestCase): """Verify response body is only logged in specific content types. Response bodies are logged only when the response's Content-Type header - is set to application/json. This prevents us to get an unexpected - MemoryError when reading arbitrary responses, such as streams. + is set to application/json or text/plain. This prevents us to get an + unexpected MemoryError when reading arbitrary responses, such as + streams. """ - OMITTED_BODY = ('Omitted, Content-Type is set to %s. Only ' - 'application/json responses have their bodies logged.') + OMITTED_BODY = ('Omitted, Content-Type is set to %s. Only ' + + ', '.join(client_session._LOG_CONTENT_TYPES) + + ' responses have their bodies logged.') session = client_session.Session(verify=False) # Content-Type is not set @@ -422,6 +424,23 @@ class SessionTests(utils.TestCase): self.assertNotIn(OMITTED_BODY % 'application/json; charset=UTF-8', self.logger.output) + # Content-Type is set to text/plain + text = 'Error detected, unable to continue.' + self.stub_url('POST', text=text, + headers={'Content-Type': 'text/plain'}) + session.post(self.TEST_URL) + self.assertIn(text, self.logger.output) + self.assertNotIn(OMITTED_BODY % 'text/plain', self.logger.output) + + # Content-Type is set to text/plain; charset=UTF-8 + text = 'Error detected, unable to continue.' + self.stub_url('POST', text=text, + headers={'Content-Type': 'text/plain; charset=UTF-8'}) + session.post(self.TEST_URL) + self.assertIn(text, self.logger.output) + self.assertNotIn(OMITTED_BODY % 'text/plain; 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)