diff options
Diffstat (limited to 'cherrypy/test/test_tools.py')
| -rw-r--r-- | cherrypy/test/test_tools.py | 236 |
1 files changed, 138 insertions, 98 deletions
diff --git a/cherrypy/test/test_tools.py b/cherrypy/test/test_tools.py index 4fb1a555..4e7b83f1 100644 --- a/cherrypy/test/test_tools.py +++ b/cherrypy/test/test_tools.py @@ -1,36 +1,40 @@ """Test the various means of instantiating and invoking tools.""" import gzip +import io import sys -import unittest -from cherrypy._cpcompat import BytesIO, copyitems, itervalues -from cherrypy._cpcompat import IncompleteRead, ntob, ntou, py3k, xrange -from cherrypy._cpcompat import bytestr, unicodestr import time -timeout = 0.2 import types +import unittest +import operator + +import six +from six.moves import range, map +from six.moves.http_client import IncompleteRead import cherrypy from cherrypy import tools +from cherrypy._cpcompat import ntob, ntou +from cherrypy.test import helper, _test_decorators +timeout = 0.2 europoundUnicode = ntou('\x80\xa3') # Client-side code # -from cherrypy.test import helper - class ToolTests(helper.CPWebCase): + @staticmethod def setup_server(): # Put check_access in a custom toolbox with its own namespace - myauthtools = cherrypy._cptools.Toolbox("myauth") + myauthtools = cherrypy._cptools.Toolbox('myauth') def check_access(default=False): - if not getattr(cherrypy.request, "userid", default): + if not getattr(cherrypy.request, 'userid', default): raise cherrypy.HTTPError(401) myauthtools.check_access = cherrypy.Tool( 'before_request_body', check_access) @@ -47,8 +51,8 @@ class ToolTests(helper.CPWebCase): def _setup(self): def makemap(): - m = self._merged_args().get("map", {}) - cherrypy.request.numerify_map = copyitems(m) + m = self._merged_args().get('map', {}) + cherrypy.request.numerify_map = list(six.iteritems(m)) cherrypy.request.hooks.attach('on_start_resource', makemap) def critical(): @@ -66,21 +70,21 @@ class ToolTests(helper.CPWebCase): def __init__(self): self.ended = {} - self._name = "nadsat" + self._name = 'nadsat' def nadsat(self): def nadsat_it_up(body): for chunk in body: - chunk = chunk.replace(ntob("good"), ntob("horrorshow")) - chunk = chunk.replace(ntob("piece"), ntob("lomtick")) + chunk = chunk.replace(ntob('good'), ntob('horrorshow')) + chunk = chunk.replace(ntob('piece'), ntob('lomtick')) yield chunk cherrypy.response.body = nadsat_it_up(cherrypy.response.body) nadsat.priority = 0 def cleanup(self): # This runs after the request has been completely written out. - cherrypy.response.body = [ntob("razdrez")] - id = cherrypy.request.params.get("id") + cherrypy.response.body = [ntob('razdrez')] + id = cherrypy.request.params.get('id') if id: self.ended[id] = True cleanup.failsafe = True @@ -101,17 +105,18 @@ class ToolTests(helper.CPWebCase): def __call__(self, scale): r = cherrypy.response r.collapse_body() - if py3k: + if six.PY3: r.body = [bytes([(x + scale) % 256 for x in r.body[0]])] else: r.body = [chr((ord(x) + scale) % 256) for x in r.body[0]] cherrypy.tools.rotator = cherrypy.Tool('before_finalize', Rotator()) def stream_handler(next_handler, *args, **kwargs): - assert cherrypy.request.config.get('tools.streamer.arg') == 'arg value' - cherrypy.response.output = o = BytesIO() + actual = cherrypy.request.config.get('tools.streamer.arg') + assert actual == 'arg value' + cherrypy.response.output = o = io.BytesIO() try: - response = next_handler(*args, **kwargs) + next_handler(*args, **kwargs) # Ignore the response and return our accumulated output # instead. return o.getvalue() @@ -122,17 +127,22 @@ class ToolTests(helper.CPWebCase): class Root: + @cherrypy.expose def index(self): - return "Howdy earth!" - index.exposed = True + return 'Howdy earth!' + @cherrypy.expose + @cherrypy.config(**{ + 'tools.streamer.on': True, + 'tools.streamer.arg': 'arg value', + }) def tarfile(self): - assert cherrypy.request.config.get('tools.streamer.arg') == 'arg value' + actual = cherrypy.request.config.get('tools.streamer.arg') + assert actual == 'arg value' cherrypy.response.output.write(ntob('I am ')) cherrypy.response.output.write(ntob('a tarfile')) - tarfile.exposed = True - tarfile._cp_config = {'tools.streamer.on': True, 'tools.streamer.arg': 'arg value'} + @cherrypy.expose def euro(self): hooks = list(cherrypy.request.hooks['before_finalize']) hooks.sort() @@ -140,24 +150,23 @@ class ToolTests(helper.CPWebCase): assert cbnames == ['gzip'], cbnames priorities = [x.priority for x in hooks] assert priorities == [80], priorities - yield ntou("Hello,") - yield ntou("world") + yield ntou('Hello,') + yield ntou('world') yield europoundUnicode - euro.exposed = True # Bare hooks + @cherrypy.expose + @cherrypy.config(**{'hooks.before_request_body': pipe_body}) def pipe(self): return cherrypy.request.body - pipe.exposed = True - pipe._cp_config = {'hooks.before_request_body': pipe_body} # Multiple decorators; include kwargs just for fun. # Note that rotator must run before gzip. + @cherrypy.expose def decorated_euro(self, *vpath): - yield ntou("Hello,") - yield ntou("world") + yield ntou('Hello,') + yield ntou('world') yield europoundUnicode - decorated_euro.exposed = True decorated_euro = tools.gzip(compress_level=6)(decorated_euro) decorated_euro = tools.rotator(scale=3)(decorated_euro) @@ -170,20 +179,19 @@ class ToolTests(helper.CPWebCase): """ def __init__(cls, name, bases, dct): type.__init__(cls, name, bases, dct) - for value in itervalues(dct): + for value in six.itervalues(dct): if isinstance(value, types.FunctionType): - value.exposed = True + cherrypy.expose(value) setattr(root, name.lower(), cls()) Test = TestType('Test', (object,), {}) # METHOD ONE: # Declare Tools in _cp_config + @cherrypy.config(**{'tools.nadsat.on': True}) class Demo(Test): - _cp_config = {"tools.nadsat.on": True} - def index(self, id=None): - return "A good piece of cherry pie" + return 'A good piece of cherry pie' def ended(self, id): return repr(tools.nadsat.ended[id]) @@ -192,33 +200,33 @@ class ToolTests(helper.CPWebCase): raise ValueError() def errinstream(self, id=None): - yield "nonconfidential" + yield 'nonconfidential' raise ValueError() - yield "confidential" + yield 'confidential' # METHOD TWO: decorator using Tool() # We support Python 2.3, but the @-deco syntax would look like # this: # @tools.check_access() def restricted(self): - return "Welcome!" + return 'Welcome!' restricted = myauthtools.check_access()(restricted) userid = restricted def err_in_onstart(self): - return "success!" + return 'success!' + @cherrypy.config(**{'response.stream': True}) def stream(self, id=None): - for x in xrange(100000000): + for x in range(100000000): yield str(x) - stream._cp_config = {'response.stream': True} conf = { # METHOD THREE: # Declare Tools in detached config '/demo': { 'tools.numerify.on': True, - 'tools.numerify.map': {ntob("pie"): ntob("3.14159")}, + 'tools.numerify.map': {ntob('pie'): ntob('3.14159')}, }, '/demo/restricted': { 'request.show_tracebacks': False, @@ -232,7 +240,7 @@ class ToolTests(helper.CPWebCase): }, '/demo/err_in_onstart': { # Because this isn't a dict, on_start_resource will error. - 'tools.numerify.map': "pie->3.14159" + 'tools.numerify.map': 'pie->3.14159' }, # Combined tools '/euro': { @@ -249,55 +257,52 @@ class ToolTests(helper.CPWebCase): app = cherrypy.tree.mount(root, config=conf) app.request_class.namespaces['myauth'] = myauthtools - if sys.version_info >= (2, 5): - from cherrypy.test import _test_decorators - root.tooldecs = _test_decorators.ToolExamples() - setup_server = staticmethod(setup_server) + root.tooldecs = _test_decorators.ToolExamples() def testHookErrors(self): - self.getPage("/demo/?id=1") + self.getPage('/demo/?id=1') # If body is "razdrez", then on_end_request is being called too early. - self.assertBody("A horrorshow lomtick of cherry 3.14159") + self.assertBody('A horrorshow lomtick of cherry 3.14159') # If this fails, then on_end_request isn't being called at all. time.sleep(0.1) - self.getPage("/demo/ended/1") - self.assertBody("True") + self.getPage('/demo/ended/1') + self.assertBody('True') valerr = '\n raise ValueError()\nValueError' - self.getPage("/demo/err?id=3") + self.getPage('/demo/err?id=3') # If body is "razdrez", then on_end_request is being called too early. self.assertErrorPage(502, pattern=valerr) # If this fails, then on_end_request isn't being called at all. time.sleep(0.1) - self.getPage("/demo/ended/3") - self.assertBody("True") + self.getPage('/demo/ended/3') + self.assertBody('True') # If body is "razdrez", then on_end_request is being called too early. - if (cherrypy.server.protocol_version == "HTTP/1.0" or - getattr(cherrypy.server, "using_apache", False)): - self.getPage("/demo/errinstream?id=5") + if (cherrypy.server.protocol_version == 'HTTP/1.0' or + getattr(cherrypy.server, 'using_apache', False)): + self.getPage('/demo/errinstream?id=5') # Because this error is raised after the response body has # started, the status should not change to an error status. - self.assertStatus("200 OK") - self.assertBody("nonconfidential") + self.assertStatus('200 OK') + self.assertBody('nonconfidential') else: # Because this error is raised after the response body has # started, and because it's chunked output, an error is raised by # the HTTP client when it encounters incomplete output. self.assertRaises((ValueError, IncompleteRead), self.getPage, - "/demo/errinstream?id=5") + '/demo/errinstream?id=5') # If this fails, then on_end_request isn't being called at all. time.sleep(0.1) - self.getPage("/demo/ended/5") - self.assertBody("True") + self.getPage('/demo/ended/5') + self.assertBody('True') # Test the "__call__" technique (compile-time decorator). - self.getPage("/demo/restricted") + self.getPage('/demo/restricted') self.assertErrorPage(401) # Test compile-time decorator with kwargs from config. - self.getPage("/demo/userid") - self.assertBody("Welcome!") + self.getPage('/demo/userid') + self.assertBody('Welcome!') def testEndRequestOnDrop(self): old_timeout = None @@ -314,8 +319,8 @@ class ToolTests(helper.CPWebCase): self.persistent = True try: conn = self.HTTP_CONN - conn.putrequest("GET", "/demo/stream?id=9", skip_host=True) - conn.putheader("Host", self.HOST) + conn.putrequest('GET', '/demo/stream?id=9', skip_host=True) + conn.putheader('Host', self.HOST) conn.endheaders() # Skip the rest of the request and close the conn. This will # cause the server's active socket to error, which *should* @@ -326,8 +331,8 @@ class ToolTests(helper.CPWebCase): self.persistent = False time.sleep(timeout * 2) # Test that the on_end_request hook was called. - self.getPage("/demo/ended/9") - self.assertBody("True") + self.getPage('/demo/ended/9') + self.assertBody('True') finally: if old_timeout is not None: httpserver.timeout = old_timeout @@ -338,60 +343,61 @@ class ToolTests(helper.CPWebCase): # This is NOT true of the other hooks. # Here, we have set up a failure in NumerifyTool.numerify_map, # but our 'critical' hook should run and set the error to 502. - self.getPage("/demo/err_in_onstart") + self.getPage('/demo/err_in_onstart') self.assertErrorPage(502) - self.assertInBody( - "AttributeError: 'str' object has no attribute 'items'") + tmpl = "AttributeError: 'str' object has no attribute '{attr}'" + expected_msg = tmpl.format(attr='items' if six.PY3 else 'iteritems') + self.assertInBody(expected_msg) def testCombinedTools(self): - expectedResult = (ntou("Hello,world") + + expectedResult = (ntou('Hello,world') + europoundUnicode).encode('utf-8') - zbuf = BytesIO() + zbuf = io.BytesIO() zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9) zfile.write(expectedResult) zfile.close() - self.getPage("/euro", + self.getPage('/euro', headers=[ - ("Accept-Encoding", "gzip"), - ("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")]) + ('Accept-Encoding', 'gzip'), + ('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7')]) self.assertInBody(zbuf.getvalue()[:3]) - zbuf = BytesIO() + zbuf = io.BytesIO() zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=6) zfile.write(expectedResult) zfile.close() - self.getPage("/decorated_euro", headers=[("Accept-Encoding", "gzip")]) + self.getPage('/decorated_euro', headers=[('Accept-Encoding', 'gzip')]) self.assertInBody(zbuf.getvalue()[:3]) # This returns a different value because gzip's priority was # lowered in conf, allowing the rotator to run after gzip. # Of course, we don't want breakage in production apps, # but it proves the priority was changed. - self.getPage("/decorated_euro/subpath", - headers=[("Accept-Encoding", "gzip")]) - if py3k: + self.getPage('/decorated_euro/subpath', + headers=[('Accept-Encoding', 'gzip')]) + if six.PY3: self.assertInBody(bytes([(x + 3) % 256 for x in zbuf.getvalue()])) else: self.assertInBody(''.join([chr((ord(x) + 3) % 256) for x in zbuf.getvalue()])) def testBareHooks(self): - content = "bit of a pain in me gulliver" - self.getPage("/pipe", - headers=[("Content-Length", str(len(content))), - ("Content-Type", "text/plain")], - method="POST", body=content) + content = 'bit of a pain in me gulliver' + self.getPage('/pipe', + headers=[('Content-Length', str(len(content))), + ('Content-Type', 'text/plain')], + method='POST', body=content) self.assertBody(content) def testHandlerWrapperTool(self): - self.getPage("/tarfile") - self.assertBody("I am a tarfile") + self.getPage('/tarfile') + self.assertBody('I am a tarfile') def testToolWithConfig(self): if not sys.version_info >= (2, 5): - return self.skip("skipped (Python 2.5+ only)") + return self.skip('skipped (Python 2.5+ only)') self.getPage('/tooldecs/blah') self.assertHeader('Content-Type', 'application/data') @@ -403,7 +409,7 @@ class ToolTests(helper.CPWebCase): except AttributeError: pass else: - raise AssertionError("Tool.on did not error as it should have.") + raise AssertionError('Tool.on did not error as it should have.') # set try: @@ -411,7 +417,24 @@ class ToolTests(helper.CPWebCase): except AttributeError: pass else: - raise AssertionError("Tool.on did not error as it should have.") + raise AssertionError('Tool.on did not error as it should have.') + + def testDecorator(self): + @cherrypy.tools.register('on_start_resource') + def example(): + pass + self.assertTrue(isinstance(cherrypy.tools.example, cherrypy.Tool)) + self.assertEqual(cherrypy.tools.example._point, 'on_start_resource') + + @cherrypy.tools.register( # noqa: F811 + 'before_finalize', name='renamed', priority=60, + ) + def example(): + pass + self.assertTrue(isinstance(cherrypy.tools.renamed, cherrypy.Tool)) + self.assertEqual(cherrypy.tools.renamed._point, 'before_finalize') + self.assertEqual(cherrypy.tools.renamed._name, 'renamed') + self.assertEqual(cherrypy.tools.renamed._priority, 60) class SessionAuthTest(unittest.TestCase): @@ -423,6 +446,23 @@ class SessionAuthTest(unittest.TestCase): username and password were unicode. """ sa = cherrypy.lib.cptools.SessionAuth() - res = sa.login_screen(None, username=unicodestr('nobody'), - password=unicodestr('anypass')) - self.assertTrue(isinstance(res, bytestr)) + res = sa.login_screen(None, username=six.text_type('nobody'), + password=six.text_type('anypass')) + self.assertTrue(isinstance(res, bytes)) + + +class TestHooks: + def test_priorities(self): + """ + Hooks should sort by priority order. + """ + Hook = cherrypy._cprequest.Hook + hooks = [ + Hook(None, priority=48), + Hook(None), + Hook(None, priority=49), + ] + hooks.sort() + by_priority = operator.attrgetter('priority') + priorities = list(map(by_priority, hooks)) + assert priorities == [48, 49, 50] |
