Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

root/tags/cherrypy-3.0.0/cherrypy/test/test_config.py

Revision 1340 (checked in by fumanchu, 2 years ago)

Test support for wsgiref.validate (new '--validate' arg to test.py), plus a couple minor fixes based on the first validation run.

  • Property svn:eol-style set to native
Line 
1 """Tests for the CherryPy configuration system."""
2
3 from cherrypy.test import test
4 test.prefer_parent_path()
5
6 import StringIO
7 import cherrypy
8
9
10 def setup_server():
11    
12     class Root:
13        
14         _cp_config = {'foo': 'this',
15                       'bar': 'that'}
16        
17         # @cherrypy.expose(alias=('global_', 'xyz'))
18         def index(self, key):
19             return cherrypy.request.config.get(key, "None")
20         index = cherrypy.expose(index, alias=('global_', 'xyz'))
21        
22         def repr(self, key):
23             return repr(cherrypy.request.config.get(key, None))
24         repr.exposed = True
25    
26     class Foo:
27        
28         _cp_config = {'foo': 'this2',
29                       'baz': 'that2'}
30        
31         def index(self, key):
32             return cherrypy.request.config.get(key, "None")
33         index.exposed = True
34         nex = index
35        
36         def bar(self, key):
37             return `cherrypy.request.config.get(key, None)`
38         bar.exposed = True
39         bar._cp_config = {'foo': 'this3', 'bax': 'this4'}
40    
41     class Another:
42        
43         def index(self, key):
44             return str(cherrypy.request.config.get(key, "None"))
45         index.exposed = True
46    
47    
48     def raw_namespace(key, value):
49         if key == 'input.map':
50             params = cherrypy.request.params
51             for name, coercer in value.iteritems():
52                 try:
53                     params[name] = coercer(params[name])
54                 except KeyError:
55                     pass
56         elif key == 'output':
57             handler = cherrypy.request.handler
58             def wrapper():
59                 # 'value' is a type (like int or str).
60                 return value(handler())
61             cherrypy.request.handler = wrapper
62     cherrypy.engine.request_class.namespaces['raw'] = raw_namespace
63    
64     class Raw:
65        
66         _cp_config = {'raw.output': repr}
67        
68         def incr(self, num):
69             return num + 1
70         incr.exposed = True
71         incr._cp_config = {'raw.input.map': {'num': int}}
72    
73     ioconf = StringIO.StringIO("""
74 [/]
75 neg: -1234
76 """)
77    
78     root = Root()
79     root.foo = Foo()
80     root.raw = Raw()
81     cherrypy.tree.mount(root, config=ioconf)
82     cherrypy.tree.mount(Another(), "/another")
83     cherrypy.config.update({'environment': 'test_suite'})
84    
85     # Shortcut syntax--should get put in the "global" bucket
86     cherrypy.config.update({'luxuryyacht': 'throatwobblermangrove'})
87
88
89 #                             Client-side code                             #
90
91 from cherrypy.test import helper
92
93 class ConfigTests(helper.CPWebCase):
94    
95     def testConfig(self):
96         tests = [
97             ('/',        'nex', 'None'),
98             ('/',        'foo', 'this'),
99             ('/',        'bar', 'that'),
100             ('/repr',    'neg', '-1234'),
101             ('/xyz',     'foo', 'this'),
102             ('/foo/',    'foo', 'this2'),
103             ('/foo/',    'bar', 'that'),
104             ('/foo/',    'bax', 'None'),
105             ('/foo/bar', 'baz', "'that2'"),
106             ('/foo/nex', 'baz', 'that2'),
107             # If 'foo' == 'this', then the mount point '/another' leaks into '/'.
108             ('/another/','foo', 'None'),
109         ]
110         for path, key, expected in tests:
111             self.getPage(path + "?key=" + key)
112             self.assertBody(expected)
113        
114         expectedconf = {
115             # From CP defaults
116             'tools.log_headers.on': False,
117             'tools.log_tracebacks.on': True,
118             'request.show_tracebacks': True,
119             'log.screen': False,
120             'environment': 'test_suite',
121             'engine.autoreload_on': False,
122             # From global config
123             'luxuryyacht': 'throatwobblermangrove',
124             # From Root._cp_config
125             'bar': 'that',
126             # From Foo._cp_config
127             'baz': 'that2',
128             # From Foo.bar._cp_config
129             'foo': 'this3',
130             'bax': 'this4',
131             }
132         for key, expected in expectedconf.iteritems():
133             self.getPage("/foo/bar?key=" + key)
134             self.assertBody(`expected`)
135    
136     def testCustomNamespaces(self):
137         self.getPage("/raw/incr?num=12")
138         self.assertBody("13")
139
140
141 if __name__ == '__main__':
142     setup_server()
143     helper.testmain()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets