diff --git a/src/RequestsLibrary/log.py b/src/RequestsLibrary/log.py index 3c9f7c1..09807f4 100644 --- a/src/RequestsLibrary/log.py +++ b/src/RequestsLibrary/log.py @@ -26,7 +26,7 @@ def log_request(response): original_request = request redirected = "" safe_headers = dict(original_request.headers) - if AUTHORIZATION in safe_headers: + if logger.LOGLEVEL not in ['TRACE', 'DEBUG'] and AUTHORIZATION in safe_headers: safe_headers[AUTHORIZATION] = '*****' logger.info( "%s Request : " % original_request.method.upper() diff --git a/utests/test_log.py b/utests/test_log.py index 137965c..eda6d75 100644 --- a/utests/test_log.py +++ b/utests/test_log.py @@ -1,5 +1,6 @@ import json import os +import pytest from requests import Request @@ -79,8 +80,10 @@ def test_log_request_with_headers(mocked_logger): "body=%s \n " % request.body) +@pytest.mark.parametrize('log_level', ['INFO', 'CONSOLE', 'HTML', 'WARN', 'ERROR']) @mock.patch('RequestsLibrary.log.logger') -def test_log_request_with_headers_auth(mocked_logger): +def test_log_request_with_headers_auth_with_no_debug_trace_logger(mocked_logger, log_level): + mocked_logger.LOGLEVEL = log_level headers = {'User-Agent': 'python-requests/2.31.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', @@ -101,6 +104,28 @@ def test_log_request_with_headers_auth(mocked_logger): "body=%s \n " % request.body) +@pytest.mark.parametrize('log_level', ['DEBUG', 'TRACE']) +@mock.patch('RequestsLibrary.log.logger') +def test_log_request_with_headers_auth_with_debug_trace_logger(mocked_logger, log_level): + headers = {'User-Agent': 'python-requests/2.31.0', + 'Accept-Encoding': 'gzip, deflate', + 'Accept': '*/*', + 'Connection': 'keep-alive', + 'Authorization': 'some_token'} + mocked_logger.LOGLEVEL = log_level + request = Request(method='get', url='http://mock.rulezz', headers=headers) + request = request.prepare() + response = mock.MagicMock() + response.history = [] + response.request = request + log_request(response) + assert mocked_logger.info.call_args[0][0] == ("%s Request : " % request.method + + "url=%s \n " % request.url + + "path_url=%s \n " % request.path_url + + "headers=%s \n " % request.headers + + "body=%s \n " % request.body) + + @mock.patch('RequestsLibrary.log.logger') def test_log_request_with_redirect(mocked_logger): request = Request(method='get', url='http://mock.rulezz/redirected')