| 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 cherrypy.config.update({'log.error_file': error_log,
58 'log.access_file': access_log,
59 })
60 cherrypy.tree.mount(root)
61
62
63 from cherrypy.test import helper, logtest
64
65
67 setup_server = staticmethod(setup_server)
68
69 logfile = access_log
70
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)
78
79 intro = '%s - - [' % self.interface()
80
81 self.assertLog(-1, intro)
82
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())
91
93 self.markLog()
94 self.getPage("/as_yield")
95 self.assertBody('content')
96 self.assertStatus(200)
97
98 intro = '%s - - [' % self.interface()
99
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())
107
109 # Test unicode in access log pieces.
110 self.markLog()
111 self.getPage("/uni_code")
112 self.assertStatus(200)
113 if py3k:
114 # The repr of a bytestring in py3k includes a b'' prefix
115 self.assertLog(-1, repr(tartaros.encode('utf8'))[2:-1])
116 else:
117 self.assertLog(-1, repr(tartaros.encode('utf8'))[1:-1])
118 # Test the erebos value. Included inline for your enlightenment.
119 # Note the 'r' prefix--those backslashes are literals.
120 self.assertLog(-1, r'\xce\x88\xcf\x81\xce\xb5\xce\xb2\xce\xbf\xcf\x82')
121
122 # Test backslashes in output.
123 self.markLog()
124 self.getPage("/slashes")
125 self.assertStatus(200)
126 if py3k:
127 self.assertLog(-1, ntob('"GET /slashed\\path HTTP/1.1"'))
128 else:
129 self.assertLog(-1, r'"GET /slashed\\path HTTP/1.1"')
130
131 # Test whitespace in output.
132 self.markLog()
133 self.getPage("/whitespace")
134 self.assertStatus(200)
135 # Again, note the 'r' prefix.
136 self.assertLog(-1, r'"Browzuh (1.0\r\n\t\t.3)"')
137
138
140 setup_server = staticmethod(setup_server)
141
142 logfile = error_log
143
145 # Test that tracebacks get written to the error log.
146 self.markLog()
147 ignore = helper.webtest.ignored_exceptions
148 ignore.append(ValueError)
149 try:
150 self.getPage("/error")
151 self.assertInBody("raise ValueError()")
152 self.assertLog(0, 'HTTP Traceback (most recent call last):')
153 self.assertLog(-3, 'raise ValueError()')
154 finally:
155 ignore.pop()
156
| Home | Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Tue Dec 2 09:59:40 2014 | http://epydoc.sourceforge.net |