Package cherrypy :: Package test :: Module test_logging
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.test.test_logging

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