| 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 |
|
|---|
| 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 |
ioconf = StringIO.StringIO(""" |
|---|
| 48 |
[/] |
|---|
| 49 |
neg: -1234 |
|---|
| 50 |
""") |
|---|
| 51 |
|
|---|
| 52 |
root = Root() |
|---|
| 53 |
root.foo = Foo() |
|---|
| 54 |
cherrypy.tree.mount(root, config=ioconf) |
|---|
| 55 |
cherrypy.tree.mount(Another(), "/another") |
|---|
| 56 |
cherrypy.config.update({'environment': 'test_suite'}) |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
cherrypy.config.update({'luxuryyacht': 'throatwobblermangrove'}) |
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
from cherrypy.test import helper |
|---|
| 65 |
|
|---|
| 66 |
class ConfigTests(helper.CPWebCase): |
|---|
| 67 |
|
|---|
| 68 |
def testConfig(self): |
|---|
| 69 |
tests = [ |
|---|
| 70 |
('/', 'nex', 'None'), |
|---|
| 71 |
('/', 'foo', 'this'), |
|---|
| 72 |
('/', 'bar', 'that'), |
|---|
| 73 |
('/repr', 'neg', '-1234'), |
|---|
| 74 |
('/xyz', 'foo', 'this'), |
|---|
| 75 |
('/foo/', 'foo', 'this2'), |
|---|
| 76 |
('/foo/', 'bar', 'that'), |
|---|
| 77 |
('/foo/', 'bax', 'None'), |
|---|
| 78 |
('/foo/bar', 'baz', 'that2'), |
|---|
| 79 |
('/foo/nex', 'baz', 'that2'), |
|---|
| 80 |
|
|---|
| 81 |
('/another/','foo', 'None'), |
|---|
| 82 |
] |
|---|
| 83 |
for path, key, expected in tests: |
|---|
| 84 |
self.getPage(path + "?key=" + key) |
|---|
| 85 |
self.assertBody(expected) |
|---|
| 86 |
|
|---|
| 87 |
def testExternalDispatch(self): |
|---|
| 88 |
|
|---|
| 89 |
cherrypy.request.app = cherrypy.tree.apps[""] |
|---|
| 90 |
cherrypy.request.dispatch("/foo/bar") |
|---|
| 91 |
expectedconf = { |
|---|
| 92 |
|
|---|
| 93 |
'tools.log_headers.on': False, |
|---|
| 94 |
'tools.log_tracebacks.on': True, |
|---|
| 95 |
'request.show_tracebacks': True, |
|---|
| 96 |
'log.screen': False, |
|---|
| 97 |
'environment': 'test_suite', |
|---|
| 98 |
'engine.autoreload_on': False, |
|---|
| 99 |
|
|---|
| 100 |
'luxuryyacht': 'throatwobblermangrove', |
|---|
| 101 |
|
|---|
| 102 |
'bar': 'that', |
|---|
| 103 |
|
|---|
| 104 |
'baz': 'that2', |
|---|
| 105 |
|
|---|
| 106 |
'foo': 'this3', |
|---|
| 107 |
'bax': 'this4', |
|---|
| 108 |
} |
|---|
| 109 |
for k, v in expectedconf.iteritems(): |
|---|
| 110 |
self.assertEqual(cherrypy.request.config[k], v) |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
if __name__ == '__main__': |
|---|
| 114 |
setup_server() |
|---|
| 115 |
helper.testmain() |
|---|