From 89da3ef0191c0a24474e968cab4815024e3194fc Mon Sep 17 00:00:00 2001 From: Fabio Todaro Date: Fri, 3 Apr 2020 12:50:29 +0200 Subject: [PATCH] Handle x-www-form-urlencoded request body --- nephthys/clients/requests.py | 12 ++++---- setup.py | 2 +- tests/test_clients/test_requests.py | 44 +++++++++++++++++++++++------ 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/nephthys/clients/requests.py b/nephthys/clients/requests.py index e6fa190..14ea05b 100644 --- a/nephthys/clients/requests.py +++ b/nephthys/clients/requests.py @@ -40,7 +40,10 @@ def decorate_log_request(log_record, request): log_record.add_request_querystring(name, value) if request.body: try: - log_record.request_body = request.body.decode("UTF-8", errors="strict") + if isinstance(request.body, str): + log_record.request_body = request.body + else: + log_record.request_body = request.body.decode("UTF-8", errors="strict") except UnicodeDecodeError: log_record.request_body = "" @@ -53,12 +56,7 @@ def decorate_log_response(log_record, response): log_record.add_response_header(name, value) if response.content: - encoding = response.encoding - if encoding is None: - encoding = "utf-8" - # Forcefully remove BOM from UTF-8 - elif encoding.lower() == "utf-8": - encoding = "utf-8-sig" + encoding = response.encoding or "utf-8" try: log_record.response_body = response.content.decode( diff --git a/setup.py b/setup.py index 9ddfe62..63a205e 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ def make_long_description(): description="Advanced Python Logger", long_description=make_long_description(), long_description_content_type="text/markdown", - version="0.5.0", + version="0.5.1", author="Fabio Todaro", license="MIT", author_email="ft@ovalmoney.com", diff --git a/tests/test_clients/test_requests.py b/tests/test_clients/test_requests.py index 8be00ff..876a607 100644 --- a/tests/test_clients/test_requests.py +++ b/tests/test_clients/test_requests.py @@ -39,7 +39,7 @@ def on_my_funct(): def test_decorate_log_request_headers(): log_record = MagicMock() - test_headers = {"test": "ciao", "bla": "hi"} + test_headers = {"key1": "value1", "key2": "value2"} request = MagicMock(url="https://ovalmoney.com", headers=test_headers) decorate_log_request(log_record, request) for name, value in test_headers.items(): @@ -61,10 +61,10 @@ def test_decorate_log_request_body(): log_record = MagicMock() request = MagicMock(url=f"https://ovalmoney.com") body = MagicMock() - body.decode.return_value = "ciao" + body.decode.return_value = "return" request.body = body decorate_log_request(log_record, request) - assert log_record.request_body == "ciao" + assert log_record.request_body == "return" body.decode.assert_called_with("UTF-8", errors="strict") @@ -80,7 +80,7 @@ def test_decorate_log_request_body_raises(): def test_decorate_log_response_headers(): log_record = MagicMock() - test_headers = {"test": "ciao", "bla": "hi"} + test_headers = {"key1": "value1", "key2": "value2"} response = MagicMock(url="https://ovalmoney.com", headers=test_headers) decorate_log_response(log_record, response) for name, value in test_headers.items(): @@ -89,7 +89,7 @@ def test_decorate_log_response_headers(): @pytest.mark.parametrize( - "encoding,expected_encoding", [(None, "utf-8"), ("UtF-8", "utf-8-sig")] + "encoding,expected_encoding", [(None, "utf-8"), ("utf-8", "utf-8")] ) def test_decorate_log_response_content(encoding, expected_encoding): log_record = MagicMock() @@ -108,7 +108,7 @@ def test_decorate_log_response_content(encoding, expected_encoding): [ (TypeError, None, "utf-8"), (UnicodeDecodeError, None, "utf-8"), - (LookupError, "UtF-8", "utf-8-sig"), + (LookupError, "UtF8", "UtF8"), ], ) def test_decorate_log_response_content_raises(exception, encoding, expected_encoding): @@ -146,7 +146,7 @@ def test_send_log_record_exception(caplog, m): assert s._logger.exception.called -def test_raw_data_reponse_log(caplog, m): +def test_raw_data_reponse_body_log(caplog, m): caplog.set_level(logging.INFO) m.get( "https://ovalmoney.com/raw_data", @@ -164,7 +164,7 @@ def test_raw_data_reponse_log(caplog, m): ) -def test_raw_data_request_log(caplog, m): +def test_raw_data_request_body_log(caplog, m): caplog.set_level(logging.INFO) m.post("https://ovalmoney.com/raw_data") @@ -178,6 +178,34 @@ def test_raw_data_request_log(caplog, m): ) +def test_text_reponse_body_log(caplog, m): + caplog.set_level(logging.INFO) + m.get( + "https://ovalmoney.com/text_data", + headers={"Content-Type": "text/plain"}, + text="Response", + ) + + s = Session() + s.get("https://ovalmoney.com/text_data") + + log_rec = [rec for rec in caplog.records][0] + + assert log_rec.msg["response"]["body"] == "Response" + + +def test_form_data_request_body_log(caplog, m): + caplog.set_level(logging.INFO) + m.post("https://ovalmoney.com/form_data") + + s = Session() + s.post("https://ovalmoney.com/form_data", data={"key": "value", "key2": "value2"}) + + log_rec = [rec for rec in caplog.records][0] + + assert log_rec.msg["request"]["body"] == "key=value&key2=value2" + + def test_session_fail_http(m): m.get("https://ovalmoney.com/user", exc=requests.exceptions.ConnectTimeout)