Package cherrypy :: Package test :: Module test_config
[hide private]
[frames] | no frames]

Source Code for Module cherrypy.test.test_config

  1  """Tests for the CherryPy configuration system.""" 
  2   
  3  from cherrypy.test import test 
  4  test.prefer_parent_path() 
  5   
  6  import os, sys 
  7  localDir = os.path.join(os.getcwd(), os.path.dirname(__file__)) 
  8  import StringIO 
  9   
 10  import cherrypy 
 11   
 12   
13 -def setup_server():
14 15 class Root: 16 17 _cp_config = {'foo': 'this', 18 'bar': 'that'} 19 20 def __init__(self): 21 cherrypy.config.namespaces['db'] = self.db_namespace
22 23 def db_namespace(self, k, v): 24 if k == "scheme": 25 self.db = v 26 27 # @cherrypy.expose(alias=('global_', 'xyz')) 28 def index(self, key): 29 return cherrypy.request.config.get(key, "None") 30 index = cherrypy.expose(index, alias=('global_', 'xyz')) 31 32 def repr(self, key): 33 return repr(cherrypy.request.config.get(key, None)) 34 repr.exposed = True 35 36 def dbscheme(self): 37 return self.db 38 dbscheme.exposed = True 39 40 favicon_ico = cherrypy.tools.staticfile.handler( 41 filename=os.path.join(localDir, '../favicon.ico')) 42 43 class Foo: 44 45 _cp_config = {'foo': 'this2', 46 'baz': 'that2'} 47 48 def index(self, key): 49 return cherrypy.request.config.get(key, "None") 50 index.exposed = True 51 nex = index 52 53 def bar(self, key): 54 return `cherrypy.request.config.get(key, None)` 55 bar.exposed = True 56 bar._cp_config = {'foo': 'this3', 'bax': 'this4'} 57 58 class Another: 59 60 def index(self, key): 61 return str(cherrypy.request.config.get(key, "None")) 62 index.exposed = True 63 64 65 def raw_namespace(key, value): 66 if key == 'input.map': 67 params = cherrypy.request.params 68 for name, coercer in value.iteritems(): 69 try: 70 params[name] = coercer(params[name]) 71 except KeyError: 72 pass 73 elif key == 'output': 74 handler = cherrypy.request.handler 75 def wrapper(): 76 # 'value' is a type (like int or str). 77 return value(handler()) 78 cherrypy.request.handler = wrapper 79 80 class Raw: 81 82 _cp_config = {'raw.output': repr} 83 84 def incr(self, num): 85 return num + 1 86 incr.exposed = True 87 incr._cp_config = {'raw.input.map': {'num': int}} 88 89 ioconf = StringIO.StringIO(""" 90 [/] 91 neg: -1234 92 filename: os.path.join(sys.prefix, "hello.py") 93 thing1: cherrypy.lib.http.response_codes[404] 94 thing2: __import__('cherrypy.tutorial', globals(), locals(), ['']).thing2 95 complex: 3+2j 96 ones: "11" 97 twos: "22" 98 stradd: %%(ones)s + %%(twos)s + "33" 99 100 [/favicon.ico] 101 tools.staticfile.filename = %r 102 """ % os.path.join(localDir, 'static/dirback.jpg')) 103 104 root = Root() 105 root.foo = Foo() 106 root.raw = Raw() 107 app = cherrypy.tree.mount(root, config=ioconf) 108 app.request_class.namespaces['raw'] = raw_namespace 109 110 cherrypy.tree.mount(Another(), "/another") 111 cherrypy.config.update({'environment': 'test_suite', 112 'luxuryyacht': 'throatwobblermangrove', 113 'db.scheme': r"sqlite///memory", 114 }) 115 116 117 # Client-side code # 118 119 from cherrypy.test import helper 120
121 -class ConfigTests(helper.CPWebCase):
122
123 - def testConfig(self):
124 tests = [ 125 ('/', 'nex', 'None'), 126 ('/', 'foo', 'this'), 127 ('/', 'bar', 'that'), 128 ('/xyz', 'foo', 'this'), 129 ('/foo/', 'foo', 'this2'), 130 ('/foo/', 'bar', 'that'), 131 ('/foo/', 'bax', 'None'), 132 ('/foo/bar', 'baz', "'that2'"), 133 ('/foo/nex', 'baz', 'that2'), 134 # If 'foo' == 'this', then the mount point '/another' leaks into '/'. 135 ('/another/','foo', 'None'), 136 ] 137 for path, key, expected in tests: 138 self.getPage(path + "?key=" + key) 139 self.assertBody(expected) 140 141 expectedconf = { 142 # From CP defaults 143 'tools.log_headers.on': False, 144 'tools.log_tracebacks.on': True, 145 'request.show_tracebacks': True, 146 'log.screen': False, 147 'environment': 'test_suite', 148 'engine.autoreload_on': False, 149 # From global config 150 'luxuryyacht': 'throatwobblermangrove', 151 # From Root._cp_config 152 'bar': 'that', 153 # From Foo._cp_config 154 'baz': 'that2', 155 # From Foo.bar._cp_config 156 'foo': 'this3', 157 'bax': 'this4', 158 } 159 for key, expected in expectedconf.iteritems(): 160 self.getPage("/foo/bar?key=" + key) 161 self.assertBody(`expected`)
162
163 - def testUnrepr(self):
164 self.getPage("/repr?key=neg") 165 self.assertBody("-1234") 166 167 self.getPage("/repr?key=filename") 168 self.assertBody(repr(os.path.join(sys.prefix, "hello.py"))) 169 170 self.getPage("/repr?key=thing1") 171 self.assertBody(repr(cherrypy.lib.http.response_codes[404])) 172 173 if not getattr(cherrypy.server, "using_apache", False): 174 # The object ID's won't match up when using Apache, since the 175 # server and client are running in different processes. 176 self.getPage("/repr?key=thing2") 177 from cherrypy.tutorial import thing2 178 self.assertBody(repr(thing2)) 179 180 self.getPage("/repr?key=complex") 181 self.assertBody("(3+2j)") 182 183 self.getPage("/repr?key=stradd") 184 self.assertBody(repr("112233"))
185
186 - def testCustomNamespaces(self):
187 self.getPage("/raw/incr?num=12") 188 self.assertBody("13") 189 190 self.getPage("/dbscheme") 191 self.assertBody(r"sqlite///memory")
192
194 # Assert that config overrides tool constructor args. Above, we set 195 # the favicon in the page handler to be '../favicon.ico', 196 # but then overrode it in config to be './static/dirback.jpg'. 197 self.getPage("/favicon.ico") 198 self.assertBody(open(os.path.join(localDir, "static/dirback.jpg"), 199 "rb").read())
200 201 202 if __name__ == '__main__': 203 setup_server() 204 helper.testmain() 205