1 """Basic tests for the CherryPy core: request handling."""
4 localDir
= os
.path
.dirname(__file__
)
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'
24 cherrypy
.request
.login
= tartaros
25 cherrypy
.request
.remote
.name
= erebos
26 uni_code
.exposed
= True
29 cherrypy
.request
.request_line
= r
'GET /slashed\path HTTP/1.1'
30 slashes
.exposed
= True
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
43 as_string
.exposed
= True
47 as_yield
.exposed
= True
52 error
._cp
_config
= {'tools.log_tracebacks.on': True}
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
)
71 def testNormalReturn(self
):
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"'
88 self
.assertLog(-1, '] "GET %s/as_string HTTP/1.1" 200 - '
89 '"http://www.cherrypy.org/" "Mozilla/5.0"'
92 def testNormalYield(self
):
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 "" ""' %
105 self
.assertLog(-1, '] "GET %s/as_yield HTTP/1.1" 200 - "" ""'
108 def testEscapedOutput(self
):
109 # Test unicode in access log pieces.
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.
120 self
.getPage("/slashes")
121 self
.assertStatus(200)
122 self
.assertLog(-1, r
'"GET /slashed\\path HTTP/1.1"')
124 # Test whitespace in output.
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
)
137 def testTracebacks(self
):
138 # Test that tracebacks get written to the error log.
140 ignore
= helper
.webtest
.ignored_exceptions
141 ignore
.append(ValueError)
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()')