Skip to content

Commit

Permalink
Handle x-www-form-urlencoded request body
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabio Todaro committed Apr 3, 2020
1 parent 33ae26d commit 89da3ef
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 16 deletions.
12 changes: 5 additions & 7 deletions nephthys/clients/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<RAW Data>"

Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
44 changes: 36 additions & 8 deletions tests/test_clients/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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")


Expand All @@ -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():
Expand All @@ -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()
Expand All @@ -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):
Expand Down Expand Up @@ -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",
Expand All @@ -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")

Expand All @@ -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)

Expand Down

0 comments on commit 89da3ef

Please sign in to comment.