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
15 tartaros = u'\u03a4\u1f71\u03c1\u03c4\u03b1\u03c1\u03bf\u03c2'
16 erebos = u'\u0388\u03c1\u03b5\u03b2\u03bf\u03c2.com'
17
18
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
37
38
39
40
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
71
72 logfile = access_log
73
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
110
133
134
151
152
153 if __name__ == '__main__':
154 setup_server()
155 helper.testmain()
156