| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 150 |
'luxuryyacht': 'throatwobblermangrove', |
|---|
| 151 |
|
|---|
| 152 |
'bar': 'that', |
|---|
| 153 |
|
|---|
| 154 |
'baz': 'that2', |
|---|
| 155 |
|
|---|
| 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 |
self.getPage("/repr?key=thing2") |
|---|
| 174 |
from cherrypy.tutorial import thing2 |
|---|
| 175 |
self.assertBody(repr(thing2)) |
|---|
| 176 |
|
|---|
| 177 |
self.getPage("/repr?key=complex") |
|---|
| 178 |
self.assertBody("(3+2j)") |
|---|
| 179 |
|
|---|
| 180 |
self.getPage("/repr?key=stradd") |
|---|
| 181 |
self.assertBody(repr("112233")) |
|---|
| 182 |
|
|---|
| 183 |
def testCustomNamespaces(self): |
|---|
| 184 |
self.getPage("/raw/incr?num=12") |
|---|
| 185 |
self.assertBody("13") |
|---|
| 186 |
|
|---|
| 187 |
self.getPage("/dbscheme") |
|---|
| 188 |
self.assertBody(r"sqlite///memory") |
|---|
| 189 |
|
|---|
| 190 |
def testHandlerToolConfigOverride(self): |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
self.getPage("/favicon.ico") |
|---|
| 195 |
self.assertBody(open(os.path.join(localDir, "static/dirback.jpg"), |
|---|
| 196 |
"rb").read()) |
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 |
if __name__ == '__main__': |
|---|
| 200 |
setup_server() |
|---|
| 201 |
helper.testmain() |
|---|