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 <tinlam@gmail.com>
Change-Id: I21a185db4ca55ff16dba60f85bb229ffdacc2afa
Closes-Bug: #1656981
This commit is contained in:
Samuel Pilla 2017-01-17 09:09:02 -06:00
parent b1301e606d
commit ed6ae95fa2
2 changed files with 19 additions and 2 deletions

View File

@ -366,8 +366,15 @@ class Session(object):
# stream of bytes and getting an unexpected MemoryError. See # stream of bytes and getting an unexpected MemoryError. See
# bug 1616105 for further details. # bug 1616105 for further details.
content_type = response.headers.get('content-type', None) 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: else:
text = ('Omitted, Content-Type is set to %s. Only ' text = ('Omitted, Content-Type is set to %s. Only '
'%s responses have their bodies logged.') '%s responses have their bodies logged.')

View File

@ -265,6 +265,16 @@ class SessionTests(utils.TestCase):
self.assertIn(body, self.logger.output) self.assertIn(body, self.logger.output)
self.assertNotIn(OMITTED_BODY % 'application/json', 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): def test_logging_cacerts(self):
path_to_certs = '/path/to/certs' path_to_certs = '/path/to/certs'
session = client_session.Session(verify=path_to_certs) session = client_session.Session(verify=path_to_certs)