From 0af56411176d8c6ccf49524c6dbe85c682c91b7e Mon Sep 17 00:00:00 2001 From: Yun Xu Date: Fri, 18 May 2018 13:15:03 -0700 Subject: [PATCH] fixing zero bytes case in atoms access log --- .cache/v/cache/lastfailed | 1 + gunicorn/glogging.py | 2 +- tests/test_logger.py | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 .cache/v/cache/lastfailed diff --git a/.cache/v/cache/lastfailed b/.cache/v/cache/lastfailed new file mode 100644 index 00000000..9e26dfee --- /dev/null +++ b/.cache/v/cache/lastfailed @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/gunicorn/glogging.py b/gunicorn/glogging.py index 713b4ea0..95cff4e4 100644 --- a/gunicorn/glogging.py +++ b/gunicorn/glogging.py @@ -299,7 +299,7 @@ class Logger(object): 'U': environ.get('PATH_INFO'), 'q': environ.get('QUERY_STRING'), 'H': environ.get('SERVER_PROTOCOL'), - 'b': getattr(resp, 'sent', None) and str(resp.sent) or '-', + 'b': getattr(resp, 'sent', None) is not None and str(resp.sent) or '-', 'B': getattr(resp, 'sent', None), 'f': environ.get('HTTP_REFERER', '-'), 'a': environ.get('HTTP_USER_AGENT', '-'), diff --git a/tests/test_logger.py b/tests/test_logger.py index ed23e98d..f2767946 100644 --- a/tests/test_logger.py +++ b/tests/test_logger.py @@ -31,6 +31,23 @@ def test_atoms_defaults(): assert atoms['{content-type}o'] == 'application/json' +def test_atoms_zero_bytes(): + response = SimpleNamespace( + status='200', response_length=0, + headers=(('Content-Type', 'application/json'),), sent=0, + ) + request = SimpleNamespace(headers=(('Accept', 'application/json'),)) + environ = { + 'REQUEST_METHOD': 'GET', 'RAW_URI': '/my/path?foo=bar', + 'PATH_INFO': '/my/path', 'QUERY_STRING': 'foo=bar', + 'SERVER_PROTOCOL': 'HTTP/1.1', + } + logger = Logger(Config()) + atoms = logger.atoms(response, request, environ, datetime.timedelta(seconds=1)) + assert atoms['b'] == '0' + assert atoms['B'] == 0 + + def test_get_username_from_basic_auth_header(): request = SimpleNamespace(headers=()) response = SimpleNamespace(