summaryrefslogtreecommitdiff
path: root/cherrypy/test/test_logging.py
diff options
context:
space:
mode:
Diffstat (limited to 'cherrypy/test/test_logging.py')
-rw-r--r--cherrypy/test/test_logging.py117
1 files changed, 75 insertions, 42 deletions
diff --git a/cherrypy/test/test_logging.py b/cherrypy/test/test_logging.py
index 429b8f2f..4d686775 100644
--- a/cherrypy/test/test_logging.py
+++ b/cherrypy/test/test_logging.py
@@ -1,13 +1,17 @@
"""Basic tests for the CherryPy core: request handling."""
import os
-localDir = os.path.dirname(__file__)
+from unittest import mock
+
+import six
import cherrypy
-from cherrypy._cpcompat import ntob, ntou, py3k
+from cherrypy._cpcompat import ntob, ntou
+from cherrypy.test import helper, logtest
-access_log = os.path.join(localDir, "access.log")
-error_log = os.path.join(localDir, "error.log")
+localDir = os.path.dirname(__file__)
+access_log = os.path.join(localDir, 'access.log')
+error_log = os.path.join(localDir, 'error.log')
# Some unicode strings.
tartaros = ntou('\u03a4\u1f71\u03c1\u03c4\u03b1\u03c1\u03bf\u03c2', 'escape')
@@ -17,19 +21,20 @@ erebos = ntou('\u0388\u03c1\u03b5\u03b2\u03bf\u03c2.com', 'escape')
def setup_server():
class Root:
+ @cherrypy.expose
def index(self):
- return "hello"
- index.exposed = True
+ return 'hello'
+ @cherrypy.expose
def uni_code(self):
cherrypy.request.login = tartaros
cherrypy.request.remote.name = erebos
- uni_code.exposed = True
+ @cherrypy.expose
def slashes(self):
cherrypy.request.request_line = r'GET /slashed\path HTTP/1.1'
- slashes.exposed = True
+ @cherrypy.expose
def whitespace(self):
# User-Agent = "User-Agent" ":" 1*( product | comment )
# comment = "(" *( ctext | quoted-pair | comment ) ")"
@@ -37,32 +42,29 @@ def setup_server():
# TEXT = <any OCTET except CTLs, but including LWS>
# LWS = [CRLF] 1*( SP | HT )
cherrypy.request.headers['User-Agent'] = 'Browzuh (1.0\r\n\t\t.3)'
- whitespace.exposed = True
+ @cherrypy.expose
def as_string(self):
- return "content"
- as_string.exposed = True
+ return 'content'
+ @cherrypy.expose
def as_yield(self):
- yield "content"
- as_yield.exposed = True
+ yield 'content'
+ @cherrypy.expose
+ @cherrypy.config(**{'tools.log_tracebacks.on': True})
def error(self):
raise ValueError()
- error.exposed = True
- error._cp_config = {'tools.log_tracebacks.on': True}
root = Root()
- cherrypy.config.update({'log.error_file': error_log,
- 'log.access_file': access_log,
- })
+ cherrypy.config.update({
+ 'log.error_file': error_log,
+ 'log.access_file': access_log,
+ })
cherrypy.tree.mount(root)
-from cherrypy.test import helper, logtest
-
-
class AccessLogTests(helper.CPWebCase, logtest.LogCase):
setup_server = staticmethod(setup_server)
@@ -70,7 +72,7 @@ class AccessLogTests(helper.CPWebCase, logtest.LogCase):
def testNormalReturn(self):
self.markLog()
- self.getPage("/as_string",
+ self.getPage('/as_string',
headers=[('Referer', 'http://www.cherrypy.org/'),
('User-Agent', 'Mozilla/5.0')])
self.assertBody('content')
@@ -91,7 +93,7 @@ class AccessLogTests(helper.CPWebCase, logtest.LogCase):
def testNormalYield(self):
self.markLog()
- self.getPage("/as_yield")
+ self.getPage('/as_yield')
self.assertBody('content')
self.assertStatus(200)
@@ -105,33 +107,64 @@ class AccessLogTests(helper.CPWebCase, logtest.LogCase):
self.assertLog(-1, '] "GET %s/as_yield HTTP/1.1" 200 - "" ""'
% self.prefix())
+ @mock.patch(
+ 'cherrypy._cplogging.LogManager.access_log_format',
+ '{h} {l} {u} {t} "{r}" {s} {b} "{f}" "{a}" {o}'
+ if six.PY3 else
+ '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" %(o)s'
+ )
def testCustomLogFormat(self):
- '''Test a customized access_log_format string,
- which is a feature of _cplogging.LogManager.access() '''
-
- original_logformat = cherrypy._cplogging.LogManager.access_log_format
- cherrypy._cplogging.LogManager.access_log_format = \
- '{h} {l} {u} {t} "{r}" {s} {b} "{f}" "{a}" {o}' \
- if py3k else \
- '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" %(o)s'
-
+ """Test a customized access_log_format string, which is a
+ feature of _cplogging.LogManager.access()."""
self.markLog()
- self.getPage("/as_string", headers=[('Referer', 'REFERER'),
+ self.getPage('/as_string', headers=[('Referer', 'REFERER'),
('User-Agent', 'USERAGENT'),
('Host', 'HOST')])
self.assertLog(-1, '%s - - [' % self.interface())
self.assertLog(-1, '] "GET /as_string HTTP/1.1" '
'200 7 "REFERER" "USERAGENT" HOST')
- cherrypy._cplogging.LogManager.access_log_format = original_logformat
+ @mock.patch(
+ 'cherrypy._cplogging.LogManager.access_log_format',
+ '{h} {l} {u} {z} "{r}" {s} {b} "{f}" "{a}" {o}'
+ if six.PY3 else
+ '%(h)s %(l)s %(u)s %(z)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" %(o)s'
+ )
+ def testTimezLogFormat(self):
+ """Test a customized access_log_format string, which is a
+ feature of _cplogging.LogManager.access()."""
+ self.markLog()
+
+ expected_time = str(cherrypy._cplogging.LazyRfc3339UtcTime())
+ with mock.patch(
+ 'cherrypy._cplogging.LazyRfc3339UtcTime',
+ lambda: expected_time):
+ self.getPage('/as_string', headers=[('Referer', 'REFERER'),
+ ('User-Agent', 'USERAGENT'),
+ ('Host', 'HOST')])
+
+ self.assertLog(-1, '%s - - ' % self.interface())
+ self.assertLog(-1, expected_time)
+ self.assertLog(-1, ' "GET /as_string HTTP/1.1" '
+ '200 7 "REFERER" "USERAGENT" HOST')
+
+ @mock.patch(
+ 'cherrypy._cplogging.LogManager.access_log_format',
+ '{i}' if six.PY3 else '%(i)s'
+ )
+ def testUUIDv4ParameterLogFormat(self):
+ """Test rendering of UUID4 within access log."""
+ self.markLog()
+ self.getPage('/as_string')
+ self.assertValidUUIDv4()
def testEscapedOutput(self):
# Test unicode in access log pieces.
self.markLog()
- self.getPage("/uni_code")
+ self.getPage('/uni_code')
self.assertStatus(200)
- if py3k:
- # The repr of a bytestring in py3k includes a b'' prefix
+ if six.PY3:
+ # The repr of a bytestring in six.PY3 includes a b'' prefix
self.assertLog(-1, repr(tartaros.encode('utf8'))[2:-1])
else:
self.assertLog(-1, repr(tartaros.encode('utf8'))[1:-1])
@@ -141,16 +174,16 @@ class AccessLogTests(helper.CPWebCase, logtest.LogCase):
# Test backslashes in output.
self.markLog()
- self.getPage("/slashes")
+ self.getPage('/slashes')
self.assertStatus(200)
- if py3k:
+ if six.PY3:
self.assertLog(-1, ntob('"GET /slashed\\path HTTP/1.1"'))
else:
self.assertLog(-1, r'"GET /slashed\\path HTTP/1.1"')
# Test whitespace in output.
self.markLog()
- self.getPage("/whitespace")
+ self.getPage('/whitespace')
self.assertStatus(200)
# Again, note the 'r' prefix.
self.assertLog(-1, r'"Browzuh (1.0\r\n\t\t.3)"')
@@ -167,8 +200,8 @@ class ErrorLogTests(helper.CPWebCase, logtest.LogCase):
ignore = helper.webtest.ignored_exceptions
ignore.append(ValueError)
try:
- self.getPage("/error")
- self.assertInBody("raise ValueError()")
+ self.getPage('/error')
+ self.assertInBody('raise ValueError()')
self.assertLog(0, 'HTTP')
self.assertLog(1, 'Traceback (most recent call last):')
self.assertLog(-2, 'raise ValueError()')