test_config.py (6741B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\carbon\common\lib\cherrypy\test\test_config.py 2 import os, sys 3 localDir = os.path.join(os.getcwd(), os.path.dirname(__file__)) 4 from cherrypy._cpcompat import ntob, StringIO 5 import unittest 6 import cherrypy 7 8 def setup_server(): 9 10 class Root: 11 _cp_config = {'foo': 'this', 12 'bar': 'that'} 13 14 def __init__(self): 15 cherrypy.config.namespaces['db'] = self.db_namespace 16 17 def db_namespace(self, k, v): 18 if k == 'scheme': 19 self.db = v 20 21 def index(self, key): 22 return cherrypy.request.config.get(key, 'None') 23 24 index = cherrypy.expose(index, alias=('global_', 'xyz')) 25 26 def repr(self, key): 27 return repr(cherrypy.request.config.get(key, None)) 28 29 repr.exposed = True 30 31 def dbscheme(self): 32 return self.db 33 34 dbscheme.exposed = True 35 36 def plain(self, x): 37 return x 38 39 plain.exposed = True 40 plain._cp_config = {'request.body.attempt_charsets': ['utf-16']} 41 favicon_ico = cherrypy.tools.staticfile.handler(filename=os.path.join(localDir, '../favicon.ico')) 42 43 class Foo: 44 _cp_config = {'foo': 'this2', 45 'baz': 'that2'} 46 47 def index(self, key): 48 return cherrypy.request.config.get(key, 'None') 49 50 index.exposed = True 51 nex = index 52 53 def silly(self): 54 return 'Hello world' 55 56 silly.exposed = True 57 silly._cp_config = {'response.headers.X-silly': 'sillyval'} 58 59 def bar(self, key): 60 return repr(cherrypy.request.config.get(key, None)) 61 62 bar.exposed = True 63 bar._cp_config = {'foo': 'this3', 64 'bax': 'this4'} 65 66 class Another: 67 68 def index(self, key): 69 return str(cherrypy.request.config.get(key, 'None')) 70 71 index.exposed = True 72 73 def raw_namespace(key, value): 74 if key == 'input.map': 75 handler = cherrypy.request.handler 76 77 def wrapper(): 78 params = cherrypy.request.params 79 for name, coercer in list(value.items()): 80 try: 81 params[name] = coercer(params[name]) 82 except KeyError: 83 pass 84 85 return handler() 86 87 cherrypy.request.handler = wrapper 88 elif key == 'output': 89 handler = cherrypy.request.handler 90 91 def wrapper(): 92 return value(handler()) 93 94 cherrypy.request.handler = wrapper 95 96 class Raw: 97 _cp_config = {'raw.output': repr} 98 99 def incr(self, num): 100 return num + 1 101 102 incr.exposed = True 103 incr._cp_config = {'raw.input.map': {'num': int}} 104 105 ioconf = StringIO('\n[/]\nneg: -1234\nfilename: os.path.join(sys.prefix, "hello.py")\nthing1: cherrypy.lib.httputil.response_codes[404]\nthing2: __import__(\'cherrypy.tutorial\', globals(), locals(), [\'\']).thing2\ncomplex: 3+2j\nones: "11"\ntwos: "22"\nstradd: %%(ones)s + %%(twos)s + "33"\n\n[/favicon.ico]\ntools.staticfile.filename = %r\n' % os.path.join(localDir, 'static/dirback.jpg')) 106 root = Root() 107 root.foo = Foo() 108 root.raw = Raw() 109 app = cherrypy.tree.mount(root, config=ioconf) 110 app.request_class.namespaces['raw'] = raw_namespace 111 cherrypy.tree.mount(Another(), '/another') 112 cherrypy.config.update({'luxuryyacht': 'throatwobblermangrove', 113 'db.scheme': 'sqlite///memory'}) 114 115 116 from cherrypy.test import helper 117 118 class ConfigTests(helper.CPWebCase): 119 setup_server = staticmethod(setup_server) 120 121 def testConfig(self): 122 tests = [('/', 'nex', 'None'), 123 ('/', 'foo', 'this'), 124 ('/', 'bar', 'that'), 125 ('/xyz', 'foo', 'this'), 126 ('/foo/', 'foo', 'this2'), 127 ('/foo/', 'bar', 'that'), 128 ('/foo/', 'bax', 'None'), 129 ('/foo/bar', 'baz', "'that2'"), 130 ('/foo/nex', 'baz', 'that2'), 131 ('/another/', 'foo', 'None')] 132 for path, key, expected in tests: 133 self.getPage(path + '?key=' + key) 134 self.assertBody(expected) 135 136 expectedconf = {'tools.log_headers.on': False, 137 'tools.log_tracebacks.on': True, 138 'request.show_tracebacks': True, 139 'log.screen': False, 140 'environment': 'test_suite', 141 'engine.autoreload_on': False, 142 'luxuryyacht': 'throatwobblermangrove', 143 'bar': 'that', 144 'baz': 'that2', 145 'foo': 'this3', 146 'bax': 'this4'} 147 for key, expected in expectedconf.items(): 148 self.getPage('/foo/bar?key=' + key) 149 self.assertBody(repr(expected)) 150 151 def testUnrepr(self): 152 self.getPage('/repr?key=neg') 153 self.assertBody('-1234') 154 self.getPage('/repr?key=filename') 155 self.assertBody(repr(os.path.join(sys.prefix, 'hello.py'))) 156 self.getPage('/repr?key=thing1') 157 self.assertBody(repr(cherrypy.lib.httputil.response_codes[404])) 158 if not getattr(cherrypy.server, 'using_apache', False): 159 self.getPage('/repr?key=thing2') 160 from cherrypy.tutorial import thing2 161 self.assertBody(repr(thing2)) 162 self.getPage('/repr?key=complex') 163 self.assertBody('(3+2j)') 164 self.getPage('/repr?key=stradd') 165 self.assertBody(repr('112233')) 166 167 def testRespNamespaces(self): 168 self.getPage('/foo/silly') 169 self.assertHeader('X-silly', 'sillyval') 170 self.assertBody('Hello world') 171 172 def testCustomNamespaces(self): 173 self.getPage('/raw/incr?num=12') 174 self.assertBody('13') 175 self.getPage('/dbscheme') 176 self.assertBody('sqlite///memory') 177 178 def testHandlerToolConfigOverride(self): 179 self.getPage('/favicon.ico') 180 self.assertBody(open(os.path.join(localDir, 'static/dirback.jpg'), 'rb').read()) 181 182 def test_request_body_namespace(self): 183 self.getPage('/plain', method='POST', headers=[('Content-Type', 'application/x-www-form-urlencoded'), ('Content-Length', '13')], body=ntob('\xff\xfex\x00=\xff\xfea\x00b\x00c\x00')) 184 self.assertBody('abc') 185 186 187 class VariableSubstitutionTests(unittest.TestCase): 188 setup_server = staticmethod(setup_server) 189 190 def test_config(self): 191 from textwrap import dedent 192 conf = dedent('\n [DEFAULT]\n dir = "/some/dir"\n my.dir = %(dir)s + "/sub"\n\n [my]\n my.dir = %(dir)s + "/my/dir"\n my.dir2 = %(my.dir)s + \'/dir2\'\n\n ') 193 fp = StringIO(conf) 194 cherrypy.config.update(fp) 195 self.assertEqual(cherrypy.config['my']['my.dir'], '/some/dir/my/dir') 196 self.assertEqual(cherrypy.config['my']['my.dir2'], '/some/dir/my/dir/dir2')