diff options
Diffstat (limited to 'cherrypy/test/test_config.py')
| -rw-r--r-- | cherrypy/test/test_config.py | 157 |
1 files changed, 78 insertions, 79 deletions
diff --git a/cherrypy/test/test_config.py b/cherrypy/test/test_config.py index 0508f92c..70fe535b 100644 --- a/cherrypy/test/test_config.py +++ b/cherrypy/test/test_config.py @@ -1,78 +1,81 @@ """Tests for the CherryPy configuration system.""" +import io import os import sys import unittest +import six + import cherrypy import cherrypy._cpcompat as compat +from cherrypy.test import helper + + localDir = os.path.join(os.getcwd(), os.path.dirname(__file__)) +def StringIOFromNative(x): + return io.StringIO(six.text_type(x)) + + def setup_server(): + @cherrypy.config(foo='this', bar='that') class Root: - _cp_config = {'foo': 'this', - 'bar': 'that'} - def __init__(self): cherrypy.config.namespaces['db'] = self.db_namespace def db_namespace(self, k, v): - if k == "scheme": + if k == 'scheme': self.db = v - # @cherrypy.expose(alias=('global_', 'xyz')) + @cherrypy.expose(alias=('global_', 'xyz')) def index(self, key): - return cherrypy.request.config.get(key, "None") - index = cherrypy.expose(index, alias=('global_', 'xyz')) + return cherrypy.request.config.get(key, 'None') + @cherrypy.expose def repr(self, key): return repr(cherrypy.request.config.get(key, None)) - repr.exposed = True + @cherrypy.expose def dbscheme(self): return self.db - dbscheme.exposed = True + @cherrypy.expose + @cherrypy.config(**{'request.body.attempt_charsets': ['utf-16']}) def plain(self, x): return x - plain.exposed = True - plain._cp_config = {'request.body.attempt_charsets': ['utf-16']} favicon_ico = cherrypy.tools.staticfile.handler( filename=os.path.join(localDir, '../favicon.ico')) + @cherrypy.config(foo='this2', baz='that2') class Foo: - _cp_config = {'foo': 'this2', - 'baz': 'that2'} - + @cherrypy.expose def index(self, key): - return cherrypy.request.config.get(key, "None") - index.exposed = True + return cherrypy.request.config.get(key, 'None') nex = index + @cherrypy.expose + @cherrypy.config(**{'response.headers.X-silly': 'sillyval'}) def silly(self): return 'Hello world' - silly.exposed = True - silly._cp_config = {'response.headers.X-silly': 'sillyval'} # Test the expose and config decorators - #@cherrypy.expose - #@cherrypy.config(foo='this3', **{'bax': 'this4'}) + @cherrypy.config(foo='this3', **{'bax': 'this4'}) + @cherrypy.expose def bar(self, key): return repr(cherrypy.request.config.get(key, None)) - bar.exposed = True - bar._cp_config = {'foo': 'this3', 'bax': 'this4'} class Another: + @cherrypy.expose def index(self, key): - return str(cherrypy.request.config.get(key, "None")) - index.exposed = True + return str(cherrypy.request.config.get(key, 'None')) def raw_namespace(key, value): if key == 'input.map': @@ -95,21 +98,20 @@ def setup_server(): return value(handler()) cherrypy.request.handler = wrapper + @cherrypy.config(**{'raw.output': repr}) class Raw: - _cp_config = {'raw.output': repr} - + @cherrypy.expose + @cherrypy.config(**{'raw.input.map': {'num': int}}) def incr(self, num): return num + 1 - incr.exposed = True - incr._cp_config = {'raw.input.map': {'num': int}} - if not compat.py3k: + if not six.PY3: thing3 = "thing3: unicode('test', errors='ignore')" else: thing3 = '' - ioconf = compat.StringIO(""" + ioconf = StringIOFromNative(""" [/] neg: -1234 filename: os.path.join(sys.prefix, "hello.py") @@ -132,29 +134,27 @@ tools.staticfile.filename = %r app = cherrypy.tree.mount(root, config=ioconf) app.request_class.namespaces['raw'] = raw_namespace - cherrypy.tree.mount(Another(), "/another") + cherrypy.tree.mount(Another(), '/another') cherrypy.config.update({'luxuryyacht': 'throatwobblermangrove', - 'db.scheme': r"sqlite///memory", + 'db.scheme': r'sqlite///memory', }) # Client-side code # -from cherrypy.test import helper - class ConfigTests(helper.CPWebCase): setup_server = staticmethod(setup_server) def testConfig(self): tests = [ - ('/', 'nex', 'None'), - ('/', 'foo', 'this'), - ('/', 'bar', 'that'), - ('/xyz', 'foo', 'this'), - ('/foo/', 'foo', 'this2'), - ('/foo/', 'bar', 'that'), - ('/foo/', 'bax', 'None'), + ('/', 'nex', 'None'), + ('/', 'foo', 'this'), + ('/', 'bar', 'that'), + ('/xyz', 'foo', 'this'), + ('/foo/', 'foo', 'this2'), + ('/foo/', 'bar', 'that'), + ('/foo/', 'bax', 'None'), ('/foo/bar', 'baz', "'that2'"), ('/foo/nex', 'baz', 'that2'), # If 'foo' == 'this', then the mount point '/another' leaks into @@ -162,7 +162,7 @@ class ConfigTests(helper.CPWebCase): ('/another/', 'foo', 'None'), ] for path, key, expected in tests: - self.getPage(path + "?key=" + key) + self.getPage(path + '?key=' + key) self.assertBody(expected) expectedconf = { @@ -184,65 +184,65 @@ class ConfigTests(helper.CPWebCase): 'bax': 'this4', } for key, expected in expectedconf.items(): - self.getPage("/foo/bar?key=" + key) + self.getPage('/foo/bar?key=' + key) self.assertBody(repr(expected)) def testUnrepr(self): - self.getPage("/repr?key=neg") - self.assertBody("-1234") + self.getPage('/repr?key=neg') + self.assertBody('-1234') - self.getPage("/repr?key=filename") - self.assertBody(repr(os.path.join(sys.prefix, "hello.py"))) + self.getPage('/repr?key=filename') + self.assertBody(repr(os.path.join(sys.prefix, 'hello.py'))) - self.getPage("/repr?key=thing1") + self.getPage('/repr?key=thing1') self.assertBody(repr(cherrypy.lib.httputil.response_codes[404])) - if not getattr(cherrypy.server, "using_apache", False): + if not getattr(cherrypy.server, 'using_apache', False): # The object ID's won't match up when using Apache, since the # server and client are running in different processes. - self.getPage("/repr?key=thing2") + self.getPage('/repr?key=thing2') from cherrypy.tutorial import thing2 self.assertBody(repr(thing2)) - if not compat.py3k: - self.getPage("/repr?key=thing3") - self.assertBody(repr(unicode('test'))) + if not six.PY3: + self.getPage('/repr?key=thing3') + self.assertBody(repr(six.text_type('test'))) - self.getPage("/repr?key=complex") - self.assertBody("(3+2j)") + self.getPage('/repr?key=complex') + self.assertBody('(3+2j)') - self.getPage("/repr?key=mul") - self.assertBody("18") + self.getPage('/repr?key=mul') + self.assertBody('18') - self.getPage("/repr?key=stradd") - self.assertBody(repr("112233")) + self.getPage('/repr?key=stradd') + self.assertBody(repr('112233')) def testRespNamespaces(self): - self.getPage("/foo/silly") + self.getPage('/foo/silly') self.assertHeader('X-silly', 'sillyval') self.assertBody('Hello world') def testCustomNamespaces(self): - self.getPage("/raw/incr?num=12") - self.assertBody("13") + self.getPage('/raw/incr?num=12') + self.assertBody('13') - self.getPage("/dbscheme") - self.assertBody(r"sqlite///memory") + self.getPage('/dbscheme') + self.assertBody(r'sqlite///memory') def testHandlerToolConfigOverride(self): # Assert that config overrides tool constructor args. Above, we set # the favicon in the page handler to be '../favicon.ico', # but then overrode it in config to be './static/dirback.jpg'. - self.getPage("/favicon.ico") - self.assertBody(open(os.path.join(localDir, "static/dirback.jpg"), - "rb").read()) + self.getPage('/favicon.ico') + self.assertBody(open(os.path.join(localDir, 'static/dirback.jpg'), + 'rb').read()) def test_request_body_namespace(self): - self.getPage("/plain", method='POST', headers=[ + self.getPage('/plain', method='POST', headers=[ ('Content-Type', 'application/x-www-form-urlencoded'), ('Content-Length', '13')], body=compat.ntob('\xff\xfex\x00=\xff\xfea\x00b\x00c\x00')) - self.assertBody("abc") + self.assertBody('abc') class VariableSubstitutionTests(unittest.TestCase): @@ -263,25 +263,24 @@ class VariableSubstitutionTests(unittest.TestCase): """) - fp = compat.StringIO(conf) + fp = StringIOFromNative(conf) cherrypy.config.update(fp) - self.assertEqual(cherrypy.config["my"]["my.dir"], "/some/dir/my/dir") - self.assertEqual(cherrypy.config["my"] - ["my.dir2"], "/some/dir/my/dir/dir2") + self.assertEqual(cherrypy.config['my']['my.dir'], '/some/dir/my/dir') + self.assertEqual(cherrypy.config['my'] + ['my.dir2'], '/some/dir/my/dir/dir2') class CallablesInConfigTest(unittest.TestCase): setup_server = staticmethod(setup_server) - def test_call_with_literal_dict(self): from textwrap import dedent conf = dedent(""" [my] value = dict(**{'foo': 'bar'}) """) - fp = compat.StringIO(conf) + fp = StringIOFromNative(conf) cherrypy.config.update(fp) self.assertEqual(cherrypy.config['my']['value'], {'foo': 'bar'}) @@ -292,12 +291,12 @@ class CallablesInConfigTest(unittest.TestCase): value = dict(foo="buzz", **cherrypy._test_dict) """) test_dict = { - "foo": "bar", - "bar": "foo", - "fizz": "buzz" + 'foo': 'bar', + 'bar': 'foo', + 'fizz': 'buzz' } cherrypy._test_dict = test_dict - fp = compat.StringIO(conf) + fp = StringIOFromNative(conf) cherrypy.config.update(fp) test_dict['foo'] = 'buzz' self.assertEqual(cherrypy.config['my']['value']['foo'], 'buzz') |
