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

root/trunk/cherrypy/test/test_config.py

Revision 1299 (checked in by fumanchu, 4 years ago)

Oops. Was using old autoreload conf keys.

  • 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     class Foo:
23        
24         _cp_config = {'foo': 'this2',
25                       'baz': 'that2'}
26        
27         def index(self, key):
28             return cherrypy.request.config.get(key, "None")
29         index.exposed = True
30         nex = index
31        
32         def bar(self, key):
33             return cherrypy.request.config.get(key, "None")
34         bar.exposed = True
35         bar._cp_config = {'foo': 'this3', 'bax': 'this4'}
36    
37     class Another:
38        
39         def index(self, key):
40             return str(cherrypy.request.config.get(key, "None"))
41         index.exposed = True
42    
43     root = Root()
44     root.foo = Foo()
45     cherrypy.tree.mount(root)
46     cherrypy.tree.mount(Another(), "/another")
47     cherrypy.config.update({'environment': 'test_suite'})
48    
49     # Shortcut syntax--should get put in the "global" bucket
50     cherrypy.config.update({'luxuryyacht': 'throatwobblermangrove'})
51
52
53 #                             Client-side code                             #
54
55 from cherrypy.test import helper
56
57 class ConfigTests(helper.CPWebCase):
58    
59     def testConfig(self):
60         tests = [
61             ('/',        'nex', 'None'),
62             ('/',        'foo', 'this'),
63             ('/',        'bar', 'that'),
64             ('/xyz',     'foo', 'this'),
65             ('/foo/',    'foo', 'this2'),
66             ('/foo/',    'bar', 'that'),
67             ('/foo/',    'bax', 'None'),
68             ('/foo/bar', 'baz', 'that2'),
69             ('/foo/nex', 'baz', 'that2'),
70             # If 'foo' == 'this', then the mount point '/another' leaks into '/'.
71             ('/another/','foo', 'None'),
72         ]
73         for path, key, expected in tests:
74             self.getPage(path + "?key=" + key)
75             self.assertBody(expected)
76    
77     def testExternalDispatch(self):
78         # 'cherrypy.request' should reference a default instance
79         cherrypy.request.app = cherrypy.tree.apps[""]
80         cherrypy.request.dispatch("/foo/bar")
81         expectedconf = {
82             # From CP defaults
83             'tools.log_headers.on': False,
84             'tools.log_tracebacks.on': True,
85             'request.show_tracebacks': True,
86             'log.screen': False,
87             'environment': 'test_suite',
88             'engine.autoreload_on': False,
89             # From global config
90             'luxuryyacht': 'throatwobblermangrove',
91             # From Root._cp_config
92             'bar': 'that',
93             # From Foo._cp_config
94             'baz': 'that2',
95             # From Foo.bar._cp_config
96             'foo': 'this3',
97             'bax': 'this4',
98             }
99         for k, v in expectedconf.iteritems():
100             self.assertEqual(cherrypy.request.config[k], v)
101
102
103 if __name__ == '__main__':
104     setup_server()
105     helper.testmain()
Note: See TracBrowser for help on using the browser.

Hosted by WebFaction

Log in as guest/cpguest to create tickets