Bundled cherrypy.
[smonitor.git] / monitor / cherrypy / test / test_logging.py
blob5a13cd4ab9683d5d3af7677dc0af159fb09c2b0b
1 """Basic tests for the CherryPy core: request handling."""
3 import os
4 localDir = os.path.dirname(__file__)
6 import cherrypy
8 access_log = os.path.join(localDir, "access.log")
9 error_log = os.path.join(localDir, "error.log")
11 # Some unicode strings.
12 tartaros = u'\u03a4\u1f71\u03c1\u03c4\u03b1\u03c1\u03bf\u03c2'
13 erebos = u'\u0388\u03c1\u03b5\u03b2\u03bf\u03c2.com'
16 def setup_server():
17 class Root:
19 def index(self):
20 return "hello"
21 index.exposed = True
23 def uni_code(self):
24 cherrypy.request.login = tartaros
25 cherrypy.request.remote.name = erebos
26 uni_code.exposed = True
28 def slashes(self):
29 cherrypy.request.request_line = r'GET /slashed\path HTTP/1.1'
30 slashes.exposed = True
32 def whitespace(self):
33 # User-Agent = "User-Agent" ":" 1*( product | comment )
34 # comment = "(" *( ctext | quoted-pair | comment ) ")"
35 # ctext = <any TEXT excluding "(" and ")">
36 # TEXT = <any OCTET except CTLs, but including LWS>
37 # LWS = [CRLF] 1*( SP | HT )
38 cherrypy.request.headers['User-Agent'] = 'Browzuh (1.0\r\n\t\t.3)'
39 whitespace.exposed = True
41 def as_string(self):
42 return "content"
43 as_string.exposed = True
45 def as_yield(self):
46 yield "content"
47 as_yield.exposed = True
49 def error(self):
50 raise ValueError()
51 error.exposed = True
52 error._cp_config = {'tools.log_tracebacks.on': True}
54 root = Root()
57 cherrypy.config.update({'log.error_file': error_log,
58 'log.access_file': access_log,
60 cherrypy.tree.mount(root)
64 from cherrypy.test import helper, logtest
66 class AccessLogTests(helper.CPWebCase, logtest.LogCase):
67 setup_server = staticmethod(setup_server)
69 logfile = access_log
71 def testNormalReturn(self):
72 self.markLog()
73 self.getPage("/as_string",
74 headers=[('Referer', 'http://www.cherrypy.org/'),
75 ('User-Agent', 'Mozilla/5.0')])
76 self.assertBody('content')
77 self.assertStatus(200)
79 intro = '%s - - [' % self.interface()
81 self.assertLog(-1, intro)
83 if [k for k, v in self.headers if k.lower() == 'content-length']:
84 self.assertLog(-1, '] "GET %s/as_string HTTP/1.1" 200 7 '
85 '"http://www.cherrypy.org/" "Mozilla/5.0"'
86 % self.prefix())
87 else:
88 self.assertLog(-1, '] "GET %s/as_string HTTP/1.1" 200 - '
89 '"http://www.cherrypy.org/" "Mozilla/5.0"'
90 % self.prefix())
92 def testNormalYield(self):
93 self.markLog()
94 self.getPage("/as_yield")
95 self.assertBody('content')
96 self.assertStatus(200)
98 intro = '%s - - [' % self.interface()
100 self.assertLog(-1, intro)
101 if [k for k, v in self.headers if k.lower() == 'content-length']:
102 self.assertLog(-1, '] "GET %s/as_yield HTTP/1.1" 200 7 "" ""' %
103 self.prefix())
104 else:
105 self.assertLog(-1, '] "GET %s/as_yield HTTP/1.1" 200 - "" ""'
106 % self.prefix())
108 def testEscapedOutput(self):
109 # Test unicode in access log pieces.
110 self.markLog()
111 self.getPage("/uni_code")
112 self.assertStatus(200)
113 self.assertLog(-1, repr(tartaros.encode('utf8'))[1:-1])
114 # Test the erebos value. Included inline for your enlightenment.
115 # Note the 'r' prefix--those backslashes are literals.
116 self.assertLog(-1, r'\xce\x88\xcf\x81\xce\xb5\xce\xb2\xce\xbf\xcf\x82')
118 # Test backslashes in output.
119 self.markLog()
120 self.getPage("/slashes")
121 self.assertStatus(200)
122 self.assertLog(-1, r'"GET /slashed\\path HTTP/1.1"')
124 # Test whitespace in output.
125 self.markLog()
126 self.getPage("/whitespace")
127 self.assertStatus(200)
128 # Again, note the 'r' prefix.
129 self.assertLog(-1, r'"Browzuh (1.0\r\n\t\t.3)"')
132 class ErrorLogTests(helper.CPWebCase, logtest.LogCase):
133 setup_server = staticmethod(setup_server)
135 logfile = error_log
137 def testTracebacks(self):
138 # Test that tracebacks get written to the error log.
139 self.markLog()
140 ignore = helper.webtest.ignored_exceptions
141 ignore.append(ValueError)
142 try:
143 self.getPage("/error")
144 self.assertInBody("raise ValueError()")
145 self.assertLog(0, 'HTTP Traceback (most recent call last):')
146 self.assertLog(-3, 'raise ValueError()')
147 finally:
148 ignore.pop()