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

root/branches/cherrypy-3.0.x/cherrypy/test/test_config.py

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

Fix to unrepr for function calls.

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

Hosted by WebFaction

Log in as guest/cpguest to create tickets