Browse Source

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
(cherry-picked from: b1301e606d)
Samuel Pilla 2 years ago
parent
commit
912d99bdf0
2 changed files with 19 additions and 2 deletions
  1. 9
    2
      keystoneauth1/session.py
  2. 10
    0
      keystoneauth1/tests/unit/test_session.py

+ 9
- 2
keystoneauth1/session.py View File

@@ -361,8 +361,15 @@ class Session(object):
361 361
                 # stream of bytes and getting an unexpected MemoryError. See
362 362
                 # bug 1616105 for further details.
363 363
                 content_type = response.headers.get('content-type', None)
364
-                if content_type in _LOG_CONTENT_TYPES:
365
-                    text = self._remove_service_catalog(response.text)
364
+
365
+                # NOTE(lamt): Per [1], the Content-Type header can be of the
366
+                # form Content-Type := type "/" subtype *[";" parameter]
367
+                # [1] https://www.w3.org/Protocols/rfc1341/4_Content-Type.html
368
+                for log_type in _LOG_CONTENT_TYPES:
369
+                    if content_type is not None and content_type.startswith(
370
+                            log_type):
371
+                        text = self._remove_service_catalog(response.text)
372
+                        break
366 373
                 else:
367 374
                     text = ('Omitted, Content-Type is set to %s. Only '
368 375
                             '%s responses have their bodies logged.')

+ 10
- 0
keystoneauth1/tests/unit/test_session.py View File

@@ -265,6 +265,16 @@ class SessionTests(utils.TestCase):
265 265
         self.assertIn(body, self.logger.output)
266 266
         self.assertNotIn(OMITTED_BODY % 'application/json', self.logger.output)
267 267
 
268
+        # Content-Type is set to application/json; charset=UTF-8
269
+        body = json.dumps({'token': {'id': '...'}})
270
+        self.stub_url(
271
+            'POST', text=body,
272
+            headers={'Content-Type': 'application/json; charset=UTF-8'})
273
+        session.post(self.TEST_URL)
274
+        self.assertIn(body, self.logger.output)
275
+        self.assertNotIn(OMITTED_BODY % 'application/json; charset=UTF-8',
276
+                         self.logger.output)
277
+
268 278
     def test_logging_cacerts(self):
269 279
         path_to_certs = '/path/to/certs'
270 280
         session = client_session.Session(verify=path_to_certs)

Loading…
Cancel
Save