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