diff options
Diffstat (limited to 'pecan/tests')
-rw-r--r-- | pecan/tests/compat/__init__.py | 0 | ||||
-rw-r--r-- | pecan/tests/compat/test_dictconfig.py | 733 | ||||
-rw-r--r-- | pecan/tests/middleware/test_debug.py | 7 | ||||
-rw-r--r-- | pecan/tests/middleware/test_errordocument.py | 16 | ||||
-rw-r--r-- | pecan/tests/middleware/test_recursive.py | 11 | ||||
-rw-r--r-- | pecan/tests/scaffold_builder.py | 26 | ||||
-rw-r--r-- | pecan/tests/test_base.py | 282 | ||||
-rw-r--r-- | pecan/tests/test_generic.py | 6 | ||||
-rw-r--r-- | pecan/tests/test_hooks.py | 55 | ||||
-rw-r--r-- | pecan/tests/test_jsonify.py | 6 | ||||
-rw-r--r-- | pecan/tests/test_rest.py | 210 | ||||
-rw-r--r-- | pecan/tests/test_scaffolds.py | 4 | ||||
-rw-r--r-- | pecan/tests/test_secure.py | 41 | ||||
-rw-r--r-- | pecan/tests/test_templating.py | 8 |
14 files changed, 342 insertions, 1063 deletions
diff --git a/pecan/tests/compat/__init__.py b/pecan/tests/compat/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/pecan/tests/compat/__init__.py +++ /dev/null diff --git a/pecan/tests/compat/test_dictconfig.py b/pecan/tests/compat/test_dictconfig.py deleted file mode 100644 index 7c31474..0000000 --- a/pecan/tests/compat/test_dictconfig.py +++ /dev/null @@ -1,733 +0,0 @@ -# Copyright 2009-2010 by Vinay Sajip. All Rights Reserved. -# -# Permission to use, copy, modify, and distribute this software and its -# documentation for any purpose and without fee is hereby granted, -# provided that the above copyright notice appear in all copies and that -# both that copyright notice and this permission notice appear in -# supporting documentation, and that the name of Vinay Sajip -# not be used in advertising or publicity pertaining to distribution -# of the software without specific, written prior permission. -# VINAY SAJIP DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING -# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL -# VINAY SAJIP BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR -# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER -# IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT -# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - -import sys -try: - from cStringIO import StringIO -except ImportError: - from io import StringIO # noqa - -from pecan.compat.dictconfig import dictConfig -import logging -import re -from test.test_support import captured_stdout -import unittest - - -class BaseTest(unittest.TestCase): - - """Base class for logging tests.""" - - log_format = "%(name)s -> %(levelname)s: %(message)s" - expected_log_pat = r"^([\w.]+) -> ([\w]+): ([\d]+)$" - message_num = 0 - - def setUp(self): - """Setup the default logging stream to an internal StringIO instance, - so that we can examine log output as we want.""" - logger_dict = logging.getLogger().manager.loggerDict - logging._acquireLock() - try: - self.saved_handlers = logging._handlers.copy() - self.saved_handler_list = logging._handlerList[:] - self.saved_loggers = logger_dict.copy() - self.saved_level_names = logging._levelNames.copy() - finally: - logging._releaseLock() - - self.root_logger = logging.getLogger("") - self.original_logging_level = self.root_logger.getEffectiveLevel() - - self.stream = StringIO() - self.root_logger.setLevel(logging.DEBUG) - self.root_hdlr = logging.StreamHandler(self.stream) - self.root_formatter = logging.Formatter(self.log_format) - self.root_hdlr.setFormatter(self.root_formatter) - self.root_logger.addHandler(self.root_hdlr) - - def tearDown(self): - """Remove our logging stream, and restore the original logging - level.""" - self.stream.close() - self.root_logger.removeHandler(self.root_hdlr) - self.root_logger.setLevel(self.original_logging_level) - logging._acquireLock() - try: - logging._levelNames.clear() - logging._levelNames.update(self.saved_level_names) - logging._handlers.clear() - logging._handlers.update(self.saved_handlers) - logging._handlerList[:] = self.saved_handler_list - loggerDict = logging.getLogger().manager.loggerDict - loggerDict.clear() - loggerDict.update(self.saved_loggers) - finally: - logging._releaseLock() - - def assert_log_lines(self, expected_values, stream=None): - """Match the collected log lines against the regular expression - self.expected_log_pat, and compare the extracted group values to - the expected_values list of tuples.""" - stream = stream or self.stream - pat = re.compile(self.expected_log_pat) - try: - stream.reset() - actual_lines = stream.readlines() - except AttributeError: - # StringIO.StringIO lacks a reset() method. - actual_lines = stream.getvalue().splitlines() - self.assertEquals(len(actual_lines), len(expected_values)) - for actual, expected in zip(actual_lines, expected_values): - match = pat.search(actual) - if not match: - self.fail("Log line does not match expected pattern:\n" + - actual) - self.assertEquals(tuple(match.groups()), expected) - s = stream.read() - if s: - self.fail("Remaining output at end of log stream:\n" + s) - - def next_message(self): - """Generate a message consisting solely of an auto-incrementing - integer.""" - self.message_num += 1 - return "%d" % self.message_num - - -class ExceptionFormatter(logging.Formatter): - """A special exception formatter.""" - def formatException(self, ei): - return "Got a [%s]" % ei[0].__name__ - - -def formatFunc(format, datefmt=None): - return logging.Formatter(format, datefmt) - - -def handlerFunc(): - return logging.StreamHandler() - - -class CustomHandler(logging.StreamHandler): - pass - - -class ConfigDictTest(BaseTest): - - """Reading logging config from a dictionary.""" - - expected_log_pat = r"^([\w]+) \+\+ ([\w]+)$" - - # config0 is a standard configuration. - config0 = { - 'version': 1, - 'formatters': { - 'form1': { - 'format': '%(levelname)s ++ %(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': 'logging.StreamHandler', - 'formatter': 'form1', - 'level': 'NOTSET', - 'stream': 'ext://sys.stdout', - }, - }, - 'root': { - 'level': 'WARNING', - 'handlers': ['hand1'], - }, - } - - # config1 adds a little to the standard configuration. - config1 = { - 'version': 1, - 'formatters': { - 'form1': { - 'format': '%(levelname)s ++ %(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': 'logging.StreamHandler', - 'formatter': 'form1', - 'level': 'NOTSET', - 'stream': 'ext://sys.stdout', - }, - }, - 'loggers': { - 'compiler.parser': { - 'level': 'DEBUG', - 'handlers': ['hand1'], - }, - }, - 'root': { - 'level': 'WARNING', - }, - } - - # config2 has a subtle configuration error that should be reported - config2 = { - 'formatters': { - 'form1': { - 'format': '%(levelname)s ++ %(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': 'logging.StreamHandler', - 'formatter': 'form1', - 'level': 'NOTSET', - 'stream': 'ext://sys.stdbout', - }, - }, - 'loggers': { - 'compiler.parser': { - 'level': 'DEBUG', - 'handlers': ['hand1'], - }, - }, - 'root': { - 'level': 'WARNING', - }, - } - - #As config1 but with a misspelt level on a handler - config2a = { - 'formatters': { - 'form1': { - 'format': '%(levelname)s ++ %(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': 'logging.StreamHandler', - 'formatter': 'form1', - 'level': 'NTOSET', - 'stream': 'ext://sys.stdout', - }, - }, - 'loggers': { - 'compiler.parser': { - 'level': 'DEBUG', - 'handlers': ['hand1'], - }, - }, - 'root': { - 'level': 'WARNING', - }, - } - - #As config1 but with a misspelt level on a logger - config2b = { - 'formatters': { - 'form1': { - 'format': '%(levelname)s ++ %(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': 'logging.StreamHandler', - 'formatter': 'form1', - 'level': 'NOTSET', - 'stream': 'ext://sys.stdout', - }, - }, - 'loggers': { - 'compiler.parser': { - 'level': 'DEBUG', - 'handlers': ['hand1'], - }, - }, - 'root': { - 'level': 'WRANING', - }, - } - - # config3 has a less subtle configuration error - config3 = { - 'formatters': { - 'form1': { - 'format': '%(levelname)s ++ %(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': 'logging.StreamHandler', - 'formatter': 'misspelled_name', - 'level': 'NOTSET', - 'stream': 'ext://sys.stdout', - }, - }, - 'loggers': { - 'compiler.parser': { - 'level': 'DEBUG', - 'handlers': ['hand1'], - }, - }, - 'root': { - 'level': 'WARNING', - }, - } - - # config4 specifies a custom formatter class to be loaded - config4 = { - 'version': 1, - 'formatters': { - 'form1': { - '()': __name__ + '.ExceptionFormatter', - 'format': '%(levelname)s:%(name)s:%(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': 'logging.StreamHandler', - 'formatter': 'form1', - 'level': 'NOTSET', - 'stream': 'ext://sys.stdout', - }, - }, - 'root': { - 'level': 'NOTSET', - 'handlers': ['hand1'], - }, - } - - # As config4 but using an actual callable rather than a string - config4a = { - 'version': 1, - 'formatters': { - 'form1': { - '()': ExceptionFormatter, - 'format': '%(levelname)s:%(name)s:%(message)s', - }, - 'form2': { - '()': __name__ + '.formatFunc', - 'format': '%(levelname)s:%(name)s:%(message)s', - }, - 'form3': { - '()': formatFunc, - 'format': '%(levelname)s:%(name)s:%(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': 'logging.StreamHandler', - 'formatter': 'form1', - 'level': 'NOTSET', - 'stream': 'ext://sys.stdout', - }, - 'hand2': { - '()': handlerFunc, - }, - }, - 'root': { - 'level': 'NOTSET', - 'handlers': ['hand1'], - }, - } - - # config5 specifies a custom handler class to be loaded - config5 = { - 'version': 1, - 'formatters': { - 'form1': { - 'format': '%(levelname)s ++ %(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': __name__ + '.CustomHandler', - 'formatter': 'form1', - 'level': 'NOTSET', - 'stream': 'ext://sys.stdout', - }, - }, - 'loggers': { - 'compiler.parser': { - 'level': 'DEBUG', - 'handlers': ['hand1'], - }, - }, - 'root': { - 'level': 'WARNING', - }, - } - - # config6 specifies a custom handler class to be loaded - # but has bad arguments - config6 = { - 'formatters': { - 'form1': { - 'format': '%(levelname)s ++ %(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': __name__ + '.CustomHandler', - 'formatter': 'form1', - 'level': 'NOTSET', - 'stream': 'ext://sys.stdout', - '9': 'invalid parameter name', - }, - }, - 'loggers': { - 'compiler.parser': { - 'level': 'DEBUG', - 'handlers': ['hand1'], - }, - }, - 'root': { - 'level': 'WARNING', - }, - } - - #config 7 does not define compiler.parser but defines compiler.lexer - #so compiler.parser should be disabled after applying it - config7 = { - 'version': 1, - 'formatters': { - 'form1': { - 'format': '%(levelname)s ++ %(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': 'logging.StreamHandler', - 'formatter': 'form1', - 'level': 'NOTSET', - 'stream': 'ext://sys.stdout', - }, - }, - 'loggers': { - 'compiler.lexer': { - 'level': 'DEBUG', - 'handlers': ['hand1'], - }, - }, - 'root': { - 'level': 'WARNING', - }, - } - - config8 = { - 'version': 1, - 'disable_existing_loggers': False, - 'formatters': { - 'form1': { - 'format': '%(levelname)s ++ %(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': 'logging.StreamHandler', - 'formatter': 'form1', - 'level': 'NOTSET', - 'stream': 'ext://sys.stdout', - }, - }, - 'loggers': { - 'compiler': { - 'level': 'DEBUG', - 'handlers': ['hand1'], - }, - 'compiler.lexer': { - }, - }, - 'root': { - 'level': 'WARNING', - }, - } - - config9 = { - 'version': 1, - 'formatters': { - 'form1': { - 'format': '%(levelname)s ++ %(message)s', - }, - }, - 'handlers': { - 'hand1': { - 'class': 'logging.StreamHandler', - 'formatter': 'form1', - 'level': 'WARNING', - 'stream': 'ext://sys.stdout', - }, - }, - 'loggers': { - 'compiler.parser': { - 'level': 'WARNING', - 'handlers': ['hand1'], - }, - }, - 'root': { - 'level': 'NOTSET', - }, - } - - config9a = { - 'version': 1, - 'incremental': True, - 'handlers': { - 'hand1': { - 'level': 'WARNING', - }, - }, - 'loggers': { - 'compiler.parser': { - 'level': 'INFO', - }, - }, - } - - config9b = { - 'version': 1, - 'incremental': True, - 'handlers': { - 'hand1': { - 'level': 'INFO', - }, - }, - 'loggers': { - 'compiler.parser': { - 'level': 'INFO', - }, - }, - } - - #As config1 but with a filter added - config10 = { - 'version': 1, - 'formatters': { - 'form1': { - 'format': '%(levelname)s ++ %(message)s', - }, - }, - 'filters': { - 'filt1': { - 'name': 'compiler.parser', - }, - }, - 'handlers': { - 'hand1': { - 'class': 'logging.StreamHandler', - 'formatter': 'form1', - 'level': 'NOTSET', - 'stream': 'ext://sys.stdout', - 'filters': ['filt1'], - }, - }, - 'loggers': { - 'compiler.parser': { - 'level': 'DEBUG', - 'filters': ['filt1'], - }, - }, - 'root': { - 'level': 'WARNING', - 'handlers': ['hand1'], - }, - } - - def apply_config(self, conf): - dictConfig(conf) - - def test_config0_ok(self): - # A simple config which overrides the default settings. - with captured_stdout() as output: - self.apply_config(self.config0) - logger = logging.getLogger() - # Won't output anything - logger.info(self.next_message()) - # Outputs a message - logger.error(self.next_message()) - self.assert_log_lines([ - ('ERROR', '2'), - ], stream=output) - # Original logger output is empty. - self.assert_log_lines([]) - - def test_config1_ok(self, config=config1): - # A config defining a sub-parser as well. - with captured_stdout() as output: - self.apply_config(config) - logger = logging.getLogger("compiler.parser") - # Both will output a message - logger.info(self.next_message()) - logger.error(self.next_message()) - self.assert_log_lines([ - ('INFO', '1'), - ('ERROR', '2'), - ], stream=output) - # Original logger output is empty. - self.assert_log_lines([]) - - def test_config2_failure(self): - # A simple config which overrides the default settings. - self.assertRaises(StandardError, self.apply_config, self.config2) - - def test_config2a_failure(self): - # A simple config which overrides the default settings. - self.assertRaises(StandardError, self.apply_config, self.config2a) - - def test_config2b_failure(self): - # A simple config which overrides the default settings. - self.assertRaises(StandardError, self.apply_config, self.config2b) - - def test_config3_failure(self): - # A simple config which overrides the default settings. - self.assertRaises(StandardError, self.apply_config, self.config3) - - def test_config4_ok(self): - # A config specifying a custom formatter class. - with captured_stdout() as output: - self.apply_config(self.config4) - #logger = logging.getLogger() - try: - raise RuntimeError() - except RuntimeError: - logging.exception("just testing") - sys.stdout.seek(0) - expected = "ERROR:root:just testing\nGot a [RuntimeError]\n" - self.assertEquals(output.getvalue(), expected) - # Original logger output is empty - self.assert_log_lines([]) - - def test_config4a_ok(self): - # A config specifying a custom formatter class. - with captured_stdout() as output: - self.apply_config(self.config4a) - #logger = logging.getLogger() - try: - raise RuntimeError() - except RuntimeError: - logging.exception("just testing") - sys.stdout.seek(0) - expected = "ERROR:root:just testing\nGot a [RuntimeError]\n" - self.assertEquals(output.getvalue(), expected) - # Original logger output is empty - self.assert_log_lines([]) - - def test_config5_ok(self): - self.test_config1_ok(config=self.config5) - - def test_config6_failure(self): - self.assertRaises(StandardError, self.apply_config, self.config6) - - def test_config7_ok(self): - with captured_stdout() as output: - self.apply_config(self.config1) - logger = logging.getLogger("compiler.parser") - # Both will output a message - logger.info(self.next_message()) - logger.error(self.next_message()) - self.assert_log_lines([ - ('INFO', '1'), - ('ERROR', '2'), - ], stream=output) - # Original logger output is empty. - self.assert_log_lines([]) - with captured_stdout() as output: - self.apply_config(self.config7) - logger = logging.getLogger("compiler.parser") - self.assertTrue(logger.disabled) - logger = logging.getLogger("compiler.lexer") - # Both will output a message - logger.info(self.next_message()) - logger.error(self.next_message()) - self.assert_log_lines([ - ('INFO', '3'), - ('ERROR', '4'), - ], stream=output) - # Original logger output is empty. - self.assert_log_lines([]) - - #Same as test_config_7_ok but don't disable old loggers. - def test_config_8_ok(self): - with captured_stdout() as output: - self.apply_config(self.config1) - logger = logging.getLogger("compiler.parser") - # Both will output a message - logger.info(self.next_message()) - logger.error(self.next_message()) - self.assert_log_lines([ - ('INFO', '1'), - ('ERROR', '2'), - ], stream=output) - # Original logger output is empty. - self.assert_log_lines([]) - with captured_stdout() as output: - self.apply_config(self.config8) - logger = logging.getLogger("compiler.parser") - self.assertFalse(logger.disabled) - # Both will output a message - logger.info(self.next_message()) - logger.error(self.next_message()) - logger = logging.getLogger("compiler.lexer") - # Both will output a message - logger.info(self.next_message()) - logger.error(self.next_message()) - self.assert_log_lines([ - ('INFO', '3'), - ('ERROR', '4'), - ('INFO', '5'), - ('ERROR', '6'), - ], stream=output) - # Original logger output is empty. - self.assert_log_lines([]) - - def test_config_9_ok(self): - with captured_stdout() as output: - self.apply_config(self.config9) - logger = logging.getLogger("compiler.parser") - # Nothing will be output since both handler and logger are - # set to WARNING - logger.info(self.next_message()) - self.assert_log_lines([], stream=output) - self.apply_config(self.config9a) - # Nothing will be output since both handler is still set - # to WARNING - logger.info(self.next_message()) - self.assert_log_lines([], stream=output) - self.apply_config(self.config9b) - # Message should now be output - logger.info(self.next_message()) - if sys.version_info[:2] == (2, 7): - self.assert_log_lines([ - ('INFO', '3'), - ], stream=output) - else: - self.assert_log_lines([], stream=output) - - def test_config_10_ok(self): - with captured_stdout() as output: - self.apply_config(self.config10) - logger = logging.getLogger("compiler.parser") - logger.warning(self.next_message()) - logger = logging.getLogger('compiler') - #Not output, because filtered - logger.warning(self.next_message()) - logger = logging.getLogger('compiler.lexer') - #Not output, because filtered - logger.warning(self.next_message()) - logger = logging.getLogger("compiler.parser.codegen") - #Output, as not filtered - logger.error(self.next_message()) - self.assert_log_lines([ - ('WARNING', '1'), - ('ERROR', '4'), - ], stream=output) diff --git a/pecan/tests/middleware/test_debug.py b/pecan/tests/middleware/test_debug.py index 5358333..de9d4cd 100644 --- a/pecan/tests/middleware/test_debug.py +++ b/pecan/tests/middleware/test_debug.py @@ -1,6 +1,7 @@ from wsgiref.util import setup_testing_defaults from webtest import TestApp +from six import b as b_ from pecan.middleware.debug import DebugMiddleware from pecan.tests import PecanTestCase @@ -25,7 +26,7 @@ class TestDebugMiddleware(PecanTestCase): if environ['PATH_INFO'] == '/error': assert 1 == 2 start_response("200 OK", [('Content-type', 'text/plain')]) - return ['requested page returned'] + return [b_('requested page returned')] self.app = TestApp(StripPasteVar(DebugMiddleware( conditional_error_app ))) @@ -33,12 +34,12 @@ class TestDebugMiddleware(PecanTestCase): def test_middleware_passes_through_when_no_exception_raised(self): r = self.app.get('/') assert r.status_int == 200 - assert r.body == 'requested page returned' + assert r.body == b_('requested page returned') def test_middleware_gives_stack_trace_on_errors(self): r = self.app.get('/error', expect_errors=True) assert r.status_int == 400 - assert 'AssertionError' in r.body + assert b_('AssertionError') in r.body def test_middleware_complains_in_multi_process_environment(self): diff --git a/pecan/tests/middleware/test_errordocument.py b/pecan/tests/middleware/test_errordocument.py index c4faa42..29f46e5 100644 --- a/pecan/tests/middleware/test_errordocument.py +++ b/pecan/tests/middleware/test_errordocument.py @@ -1,6 +1,7 @@ import json from webtest import TestApp +from six import b as b_ import pecan from pecan.middleware.errordocument import ErrorDocumentMiddleware @@ -16,7 +17,7 @@ def four_oh_four_app(environ, start_response): body = "Error: %s" % code if environ['QUERY_STRING']: body += "\nQS: %s" % environ['QUERY_STRING'] - return [body] + return [b_(body)] start_response("404 Not Found", [('Content-type', 'text/plain')]) return [] @@ -32,12 +33,12 @@ class TestErrorDocumentMiddleware(PecanTestCase): def test_hit_error_page(self): r = self.app.get('/error/404') assert r.status_int == 200 - assert r.body == 'Error: 404' + assert r.body == b_('Error: 404') def test_middleware_routes_to_404_message(self): r = self.app.get('/', expect_errors=True) assert r.status_int == 404 - assert r.body == 'Error: 404' + assert r.body == b_('Error: 404') def test_error_endpoint_with_query_string(self): app = TestApp(RecursiveMiddleware(ErrorDocumentMiddleware( @@ -45,7 +46,7 @@ class TestErrorDocumentMiddleware(PecanTestCase): ))) r = app.get('/', expect_errors=True) assert r.status_int == 404 - assert r.body == 'Error: 404\nQS: foo=bar' + assert r.body == b_('Error: 404\nQS: foo=bar') def test_error_with_recursion_loop(self): app = TestApp(RecursiveMiddleware(ErrorDocumentMiddleware( @@ -53,8 +54,9 @@ class TestErrorDocumentMiddleware(PecanTestCase): ))) r = app.get('/', expect_errors=True) assert r.status_int == 404 - assert r.body == ('Error: 404 Not Found. ' - '(Error page could not be fetched)') + assert r.body == b_( + 'Error: 404 Not Found. (Error page could not be fetched)' + ) def test_original_exception(self): @@ -85,6 +87,6 @@ class TestErrorDocumentMiddleware(PecanTestCase): r = app.get('/', expect_errors=405) assert r.status_int == 405 - resp = json.loads(r.body) + resp = json.loads(r.body.decode()) assert resp['status'] == 405 assert resp['reason'] == 'You have to POST, dummy!' diff --git a/pecan/tests/middleware/test_recursive.py b/pecan/tests/middleware/test_recursive.py index 8cd213c..ed95d50 100644 --- a/pecan/tests/middleware/test_recursive.py +++ b/pecan/tests/middleware/test_recursive.py @@ -1,4 +1,5 @@ from webtest import TestApp +from six import b as b_ from pecan.middleware.recursive import (RecursiveMiddleware, ForwardRequestException) @@ -7,16 +8,16 @@ from pecan.tests import PecanTestCase def simple_app(environ, start_response): start_response("200 OK", [('Content-type', 'text/plain')]) - return ['requested page returned'] + return [b_('requested page returned')] def error_docs_app(environ, start_response): if environ['PATH_INFO'] == '/not_found': start_response("404 Not found", [('Content-type', 'text/plain')]) - return ['Not found'] + return [b_('Not found')] elif environ['PATH_INFO'] == '/error': start_response("200 OK", [('Content-type', 'text/plain')]) - return ['Page not found'] + return [b_('Page not found')] elif environ['PATH_INFO'] == '/recurse': raise ForwardRequestException('/recurse') else: @@ -49,7 +50,7 @@ def forward(app): assert 'Page not found' in res try: res = app.get('/recurse') - except AssertionError, e: + except AssertionError as e: if str(e).startswith('Forwarding loop detected'): pass else: @@ -126,7 +127,7 @@ class TestRecursiveMiddleware(PecanTestCase): assert 'Page not found' in res try: res = app.get('/recurse') - except AssertionError, e: + except AssertionError as e: if str(e).startswith('Forwarding loop detected'): pass else: diff --git a/pecan/tests/scaffold_builder.py b/pecan/tests/scaffold_builder.py index f1ae50e..e00b6da 100644 --- a/pecan/tests/scaffold_builder.py +++ b/pecan/tests/scaffold_builder.py @@ -1,7 +1,6 @@ import os import sys import subprocess -import urllib2 import time @@ -10,6 +9,9 @@ if sys.version_info < (2, 7): else: import unittest # noqa +from six import b as b_ + +from pecan.compat import urlopen, URLError from pecan.tests import PecanTestCase @@ -55,10 +57,11 @@ if __name__ == '__main__': ) try: # ...and that it's serving (valid) content... - resp = urllib2.urlopen('http://localhost:8080/') + resp = urlopen('http://localhost:8080/') assert resp.getcode() == 200 - assert 'This is a sample Pecan project.' in resp.read() - except urllib2.URLError: + assert 'This is a sample Pecan project.' in \ + resp.read().decode() + except URLError: pass else: break @@ -81,11 +84,11 @@ if __name__ == '__main__': self.poll(proc) out, _ = proc.communicate( - '{"model" : model, "conf" : conf, "app" : app}' + b_('{"model" : model, "conf" : conf, "app" : app}') ) - assert 'testing123.model' in out, out - assert 'Config(' in out, out - assert 'webtest.app.TestApp' in out, out + assert 'testing123.model' in out.decode(), out + assert 'Config(' in out.decode(), out + assert 'webtest.app.TestApp' in out.decode(), out try: # just in case stdin doesn't close @@ -108,10 +111,11 @@ if __name__ == '__main__': ) try: # ...and that it's serving (valid) content... - resp = urllib2.urlopen('http://localhost:%d/' % port) + resp = urlopen('http://localhost:%d/' % port) assert resp.getcode() == 200 - assert 'This is a sample Pecan project.' in resp.read() - except urllib2.URLError: + assert 'This is a sample Pecan project.' in \ + resp.read().decode() + except URLError: pass else: break diff --git a/pecan/tests/test_base.py b/pecan/tests/test_base.py index d4b4371..37751b7 100644 --- a/pecan/tests/test_base.py +++ b/pecan/tests/test_base.py @@ -7,6 +7,9 @@ else: import unittest # pragma: nocover from webtest import TestApp +import six +from six import b as b_ +from six.moves import cStringIO as StringIO from pecan import ( Pecan, expose, request, response, redirect, abort, make_app, @@ -44,17 +47,17 @@ class TestIndexRouting(PecanTestCase): def test_empty_root(self): r = self.app_.get('/') assert r.status_int == 200 - assert r.body == 'Hello, World!' + assert r.body == b_('Hello, World!') def test_index(self): r = self.app_.get('/index') assert r.status_int == 200 - assert r.body == 'Hello, World!' + assert r.body == b_('Hello, World!') def test_index_html(self): r = self.app_.get('/index.html') assert r.status_int == 200 - assert r.body == 'Hello, World!' + assert r.body == b_('Hello, World!') class TestObjectDispatch(PecanTestCase): @@ -97,22 +100,22 @@ class TestObjectDispatch(PecanTestCase): def test_index(self): r = self.app_.get('/') assert r.status_int == 200 - assert r.body == '/' + assert r.body == b_('/') def test_one_level(self): r = self.app_.get('/deeper') assert r.status_int == 200 - assert r.body == '/deeper' + assert r.body == b_('/deeper') def test_one_level_with_trailing(self): r = self.app_.get('/sub/') assert r.status_int == 200 - assert r.body == '/sub/' + assert r.body == b_('/sub/') def test_two_levels(self): r = self.app_.get('/sub/deeper') assert r.status_int == 200 - assert r.body == '/sub/deeper' + assert r.body == b_('/sub/deeper') def test_two_levels_with_trailing(self): r = self.app_.get('/sub/sub/') @@ -121,7 +124,7 @@ class TestObjectDispatch(PecanTestCase): def test_three_levels(self): r = self.app_.get('/sub/sub/deeper') assert r.status_int == 200 - assert r.body == '/sub/sub/deeper' + assert r.body == b_('/sub/sub/deeper') class TestLookups(PecanTestCase): @@ -154,17 +157,17 @@ class TestLookups(PecanTestCase): def test_index(self): r = self.app_.get('/') assert r.status_int == 200 - assert r.body == '/' + assert r.body == b_('/') def test_lookup(self): r = self.app_.get('/100/') assert r.status_int == 200 - assert r.body == '/100' + assert r.body == b_('/100') def test_lookup_with_method(self): r = self.app_.get('/100/name') assert r.status_int == 200 - assert r.body == '/100/name' + assert r.body == b_('/100/name') def test_lookup_with_wrong_argspec(self): class RootController(object): @@ -245,19 +248,22 @@ class TestControllerArguments(PecanTestCase): try: r = self.app_.get('/') assert r.status_int != 200 # pragma: nocover - except Exception, ex: + except Exception as ex: assert type(ex) == TypeError - assert ex.args[0] == 'index() takes exactly 2 arguments (1 given)' + assert ex.args[0] in ( + "index() takes exactly 2 arguments (1 given)", + "index() missing 1 required positional argument: 'id'" + ) # this messaging changed in Python 3.3 def test_single_argument(self): r = self.app_.get('/1') assert r.status_int == 200 - assert r.body == 'index: 1' + assert r.body == b_('index: 1') def test_single_argument_with_encoded_url(self): r = self.app_.get('/This%20is%20a%20test%21') assert r.status_int == 200 - assert r.body == 'index: This is a test!' + assert r.body == b_('index: This is a test!') def test_two_arguments(self): r = self.app_.get('/1/dummy', status=404) @@ -266,90 +272,90 @@ class TestControllerArguments(PecanTestCase): def test_keyword_argument(self): r = self.app_.get('/?id=2') assert r.status_int == 200 - assert r.body == 'index: 2' + assert r.body == b_('index: 2') def test_keyword_argument_with_encoded_url(self): r = self.app_.get('/?id=This%20is%20a%20test%21') assert r.status_int == 200 - assert r.body == 'index: This is a test!' + assert r.body == b_('index: This is a test!') def test_argument_and_keyword_argument(self): r = self.app_.get('/3?id=three') assert r.status_int == 200 - assert r.body == 'index: 3' + assert r.body == b_('index: 3') def test_encoded_argument_and_keyword_argument(self): r = self.app_.get('/This%20is%20a%20test%21?id=three') assert r.status_int == 200 - assert r.body == 'index: This is a test!' + assert r.body == b_('index: This is a test!') def test_explicit_kwargs(self): r = self.app_.post('/', {'id': '4'}) assert r.status_int == 200 - assert r.body == 'index: 4' + assert r.body == b_('index: 4') def test_path_with_explicit_kwargs(self): r = self.app_.post('/4', {'id': 'four'}) assert r.status_int == 200 - assert r.body == 'index: 4' + assert r.body == b_('index: 4') def test_multiple_kwargs(self): r = self.app_.get('/?id=5&dummy=dummy') assert r.status_int == 200 - assert r.body == 'index: 5' + assert r.body == b_('index: 5') def test_kwargs_from_root(self): r = self.app_.post('/', {'id': '6', 'dummy': 'dummy'}) assert r.status_int == 200 - assert r.body == 'index: 6' + assert r.body == b_('index: 6') # multiple args def test_multiple_positional_arguments(self): r = self.app_.get('/multiple/one/two') assert r.status_int == 200 - assert r.body == 'multiple: one, two' + assert r.body == b_('multiple: one, two') def test_multiple_positional_arguments_with_url_encode(self): r = self.app_.get('/multiple/One%20/Two%21') assert r.status_int == 200 - assert r.body == 'multiple: One , Two!' + assert r.body == b_('multiple: One , Two!') def test_multiple_positional_arguments_with_kwargs(self): r = self.app_.get('/multiple?one=three&two=four') assert r.status_int == 200 - assert r.body == 'multiple: three, four' + assert r.body == b_('multiple: three, four') def test_multiple_positional_arguments_with_url_encoded_kwargs(self): r = self.app_.get('/multiple?one=Three%20&two=Four%20%21') assert r.status_int == 200 - assert r.body == 'multiple: Three , Four !' + assert r.body == b_('multiple: Three , Four !') def test_positional_args_with_dictionary_kwargs(self): r = self.app_.post('/multiple', {'one': 'five', 'two': 'six'}) assert r.status_int == 200 - assert r.body == 'multiple: five, six' + assert r.body == b_('multiple: five, six') def test_positional_args_with_url_encoded_dictionary_kwargs(self): r = self.app_.post('/multiple', {'one': 'Five%20', 'two': 'Six%20%21'}) assert r.status_int == 200 - assert r.body == 'multiple: Five%20, Six%20%21' + assert r.body == b_('multiple: Five%20, Six%20%21') # optional arg def test_optional_arg(self): r = self.app_.get('/optional') assert r.status_int == 200 - assert r.body == 'optional: None' + assert r.body == b_('optional: None') def test_multiple_optional(self): r = self.app_.get('/optional/1') assert r.status_int == 200 - assert r.body == 'optional: 1' + assert r.body == b_('optional: 1') def test_multiple_optional_url_encoded(self): r = self.app_.get('/optional/Some%20Number') assert r.status_int == 200 - assert r.body == 'optional: Some Number' + assert r.body == b_('optional: Some Number') def test_multiple_optional_missing(self): r = self.app_.get('/optional/2/dummy', status=404) @@ -358,57 +364,57 @@ class TestControllerArguments(PecanTestCase): def test_multiple_with_kwargs(self): r = self.app_.get('/optional?id=2') assert r.status_int == 200 - assert r.body == 'optional: 2' + assert r.body == b_('optional: 2') def test_multiple_with_url_encoded_kwargs(self): r = self.app_.get('/optional?id=Some%20Number') assert r.status_int == 200 - assert r.body == 'optional: Some Number' + assert r.body == b_('optional: Some Number') def test_multiple_args_with_url_encoded_kwargs(self): r = self.app_.get('/optional/3?id=three') assert r.status_int == 200 - assert r.body == 'optional: 3' + assert r.body == b_('optional: 3') def test_url_encoded_positional_args(self): r = self.app_.get('/optional/Some%20Number?id=three') assert r.status_int == 200 - assert r.body == 'optional: Some Number' + assert r.body == b_('optional: Some Number') def test_optional_arg_with_kwargs(self): r = self.app_.post('/optional', {'id': '4'}) assert r.status_int == 200 - assert r.body == 'optional: 4' + assert r.body == b_('optional: 4') def test_optional_arg_with_url_encoded_kwargs(self): r = self.app_.post('/optional', {'id': 'Some%20Number'}) assert r.status_int == 200 - assert r.body == 'optional: Some%20Number' + assert r.body == b_('optional: Some%20Number') def test_multiple_positional_arguments_with_dictionary_kwargs(self): r = self.app_.post('/optional/5', {'id': 'five'}) assert r.status_int == 200 - assert r.body == 'optional: 5' + assert r.body == b_('optional: 5') def test_multiple_positional_url_encoded_arguments_with_kwargs(self): r = self.app_.post('/optional/Some%20Number', {'id': 'five'}) assert r.status_int == 200 - assert r.body == 'optional: Some Number' + assert r.body == b_('optional: Some Number') def test_optional_arg_with_multiple_kwargs(self): r = self.app_.get('/optional?id=6&dummy=dummy') assert r.status_int == 200 - assert r.body == 'optional: 6' + assert r.body == b_('optional: 6') def test_optional_arg_with_multiple_url_encoded_kwargs(self): r = self.app_.get('/optional?id=Some%20Number&dummy=dummy') assert r.status_int == 200 - assert r.body == 'optional: Some Number' + assert r.body == b_('optional: Some Number') def test_optional_arg_with_multiple_dictionary_kwargs(self): r = self.app_.post('/optional', {'id': '7', 'dummy': 'dummy'}) assert r.status_int == 200 - assert r.body == 'optional: 7' + assert r.body == b_('optional: 7') def test_optional_arg_with_multiple_url_encoded_dictionary_kwargs(self): r = self.app_.post('/optional', { @@ -416,34 +422,34 @@ class TestControllerArguments(PecanTestCase): 'dummy': 'dummy' }) assert r.status_int == 200 - assert r.body == 'optional: Some%20Number' + assert r.body == b_('optional: Some%20Number') # multiple optional args def test_multiple_optional_positional_args(self): r = self.app_.get('/multiple_optional') assert r.status_int == 200 - assert r.body == 'multiple_optional: None, None, None' + assert r.body == b_('multiple_optional: None, None, None') def test_multiple_optional_positional_args_one_arg(self): r = self.app_.get('/multiple_optional/1') assert r.status_int == 200 - assert r.body == 'multiple_optional: 1, None, None' + assert r.body == b_('multiple_optional: 1, None, None') def test_multiple_optional_positional_args_one_url_encoded_arg(self): r = self.app_.get('/multiple_optional/One%21') assert r.status_int == 200 - assert r.body == 'multiple_optional: One!, None, None' + assert r.body == b_('multiple_optional: One!, None, None') def test_multiple_optional_positional_args_all_args(self): r = self.app_.get('/multiple_optional/1/2/3') assert r.status_int == 200 - assert r.body == 'multiple_optional: 1, 2, 3' + assert r.body == b_('multiple_optional: 1, 2, 3') def test_multiple_optional_positional_args_all_url_encoded_args(self): r = self.app_.get('/multiple_optional/One%21/Two%21/Three%21') assert r.status_int == 200 - assert r.body == 'multiple_optional: One!, Two!, Three!' + assert r.body == b_('multiple_optional: One!, Two!, Three!') def test_multiple_optional_positional_args_too_many_args(self): r = self.app_.get('/multiple_optional/1/2/3/dummy', status=404) @@ -452,54 +458,54 @@ class TestControllerArguments(PecanTestCase): def test_multiple_optional_positional_args_with_kwargs(self): r = self.app_.get('/multiple_optional?one=1') assert r.status_int == 200 - assert r.body == 'multiple_optional: 1, None, None' + assert r.body == b_('multiple_optional: 1, None, None') def test_multiple_optional_positional_args_with_url_encoded_kwargs(self): r = self.app_.get('/multiple_optional?one=One%21') assert r.status_int == 200 - assert r.body == 'multiple_optional: One!, None, None' + assert r.body == b_('multiple_optional: One!, None, None') def test_multiple_optional_positional_args_with_string_kwargs(self): r = self.app_.get('/multiple_optional/1?one=one') assert r.status_int == 200 - assert r.body == 'multiple_optional: 1, None, None' + assert r.body == b_('multiple_optional: 1, None, None') def test_multiple_optional_positional_args_with_encoded_str_kwargs(self): r = self.app_.get('/multiple_optional/One%21?one=one') assert r.status_int == 200 - assert r.body == 'multiple_optional: One!, None, None' + assert r.body == b_('multiple_optional: One!, None, None') def test_multiple_optional_positional_args_with_dict_kwargs(self): r = self.app_.post('/multiple_optional', {'one': '1'}) assert r.status_int == 200 - assert r.body == 'multiple_optional: 1, None, None' + assert r.body == b_('multiple_optional: 1, None, None') def test_multiple_optional_positional_args_with_encoded_dict_kwargs(self): r = self.app_.post('/multiple_optional', {'one': 'One%21'}) assert r.status_int == 200 - assert r.body == 'multiple_optional: One%21, None, None' + assert r.body == b_('multiple_optional: One%21, None, None') def test_multiple_optional_positional_args_and_dict_kwargs(self): r = self.app_.post('/multiple_optional/1', {'one': 'one'}) assert r.status_int == 200 - assert r.body == 'multiple_optional: 1, None, None' + assert r.body == b_('multiple_optional: 1, None, None') def test_multiple_optional_encoded_positional_args_and_dict_kwargs(self): r = self.app_.post('/multiple_optional/One%21', {'one': 'one'}) assert r.status_int == 200 - assert r.body == 'multiple_optional: One!, None, None' + assert r.body == b_('multiple_optional: One!, None, None') def test_multiple_optional_args_with_multiple_kwargs(self): r = self.app_.get('/multiple_optional?one=1&two=2&three=3&four=4') assert r.status_int == 200 - assert r.body == 'multiple_optional: 1, 2, 3' + assert r.body == b_('multiple_optional: 1, 2, 3') def test_multiple_optional_args_with_multiple_encoded_kwargs(self): r = self.app_.get( '/multiple_optional?one=One%21&two=Two%21&three=Three%21&four=4' ) assert r.status_int == 200 - assert r.body == 'multiple_optional: One!, Two!, Three!' + assert r.body == b_('multiple_optional: One!, Two!, Three!') def test_multiple_optional_args_with_multiple_dict_kwargs(self): r = self.app_.post( @@ -507,7 +513,7 @@ class TestControllerArguments(PecanTestCase): {'one': '1', 'two': '2', 'three': '3', 'four': '4'} ) assert r.status_int == 200 - assert r.body == 'multiple_optional: 1, 2, 3' + assert r.body == b_('multiple_optional: 1, 2, 3') def test_multiple_optional_args_with_multiple_encoded_dict_kwargs(self): r = self.app_.post( @@ -520,52 +526,52 @@ class TestControllerArguments(PecanTestCase): } ) assert r.status_int == 200 - assert r.body == 'multiple_optional: One%21, Two%21, Three%21' + assert r.body == b_('multiple_optional: One%21, Two%21, Three%21') def test_multiple_optional_args_with_last_kwarg(self): r = self.app_.get('/multiple_optional?three=3') assert r.status_int == 200 - assert r.body == 'multiple_optional: None, None, 3' + assert r.body == b_('multiple_optional: None, None, 3') def test_multiple_optional_args_with_last_encoded_kwarg(self): r = self.app_.get('/multiple_optional?three=Three%21') assert r.status_int == 200 - assert r.body == 'multiple_optional: None, None, Three!' + assert r.body == b_('multiple_optional: None, None, Three!') def test_multiple_optional_args_with_middle_arg(self): r = self.app_.get('/multiple_optional', {'two': '2'}) assert r.status_int == 200 - assert r.body == 'multiple_optional: None, 2, None' + assert r.body == b_('multiple_optional: None, 2, None') def test_variable_args(self): r = self.app_.get('/variable_args') assert r.status_int == 200 - assert r.body == 'variable_args: ' + assert r.body == b_('variable_args: ') def test_multiple_variable_args(self): r = self.app_.get('/variable_args/1/dummy') assert r.status_int == 200 - assert r.body == 'variable_args: 1, dummy' + assert r.body == b_('variable_args: 1, dummy') def test_multiple_encoded_variable_args(self): r = self.app_.get('/variable_args/Testing%20One%20Two/Three%21') assert r.status_int == 200 - assert r.body == 'variable_args: Testing One Two, Three!' + assert r.body == b_('variable_args: Testing One Two, Three!') def test_variable_args_with_kwargs(self): r = self.app_.get('/variable_args?id=2&dummy=dummy') assert r.status_int == 200 - assert r.body == 'variable_args: ' + assert r.body == b_('variable_args: ') def test_variable_args_with_dict_kwargs(self): r = self.app_.post('/variable_args', {'id': '3', 'dummy': 'dummy'}) assert r.status_int == 200 - assert r.body == 'variable_args: ' + assert r.body == b_('variable_args: ') def test_variable_kwargs(self): r = self.app_.get('/variable_kwargs') assert r.status_int == 200 - assert r.body == 'variable_kwargs: ' + assert r.body == b_('variable_kwargs: ') def test_multiple_variable_kwargs(self): r = self.app_.get('/variable_kwargs/1/dummy', status=404) @@ -574,19 +580,19 @@ class TestControllerArguments(PecanTestCase): def test_multiple_variable_kwargs_with_explicit_kwargs(self): r = self.app_.get('/variable_kwargs?id=2&dummy=dummy') assert r.status_int == 200 - assert r.body == 'variable_kwargs: dummy=dummy, id=2' + assert r.body == b_('variable_kwargs: dummy=dummy, id=2') def test_multiple_variable_kwargs_with_explicit_encoded_kwargs(self): r = self.app_.get( '/variable_kwargs?id=Two%21&dummy=This%20is%20a%20test' ) assert r.status_int == 200 - assert r.body == 'variable_kwargs: dummy=This is a test, id=Two!' + assert r.body == b_('variable_kwargs: dummy=This is a test, id=Two!') def test_multiple_variable_kwargs_with_dict_kwargs(self): r = self.app_.post('/variable_kwargs', {'id': '3', 'dummy': 'dummy'}) assert r.status_int == 200 - assert r.body == 'variable_kwargs: dummy=dummy, id=3' + assert r.body == b_('variable_kwargs: dummy=dummy, id=3') def test_multiple_variable_kwargs_with_encoded_dict_kwargs(self): r = self.app_.post( @@ -595,42 +601,42 @@ class TestControllerArguments(PecanTestCase): ) assert r.status_int == 200 result = 'variable_kwargs: dummy=This%20is%20a%20test, id=Three%21' - assert r.body == result + assert r.body == b_(result) def test_variable_all(self): r = self.app_.get('/variable_all') assert r.status_int == 200 - assert r.body == 'variable_all: ' + assert r.body == b_('variable_all: ') def test_variable_all_with_one_extra(self): r = self.app_.get('/variable_all/1') assert r.status_int == 200 - assert r.body == 'variable_all: 1' + assert r.body == b_('variable_all: 1') def test_variable_all_with_two_extras(self): r = self.app_.get('/variable_all/2/dummy') assert r.status_int == 200 - assert r.body == 'variable_all: 2, dummy' + assert r.body == b_('variable_all: 2, dummy') def test_variable_mixed(self): r = self.app_.get('/variable_all/3?month=1&day=12') assert r.status_int == 200 - assert r.body == 'variable_all: 3, day=12, month=1' + assert r.body == b_('variable_all: 3, day=12, month=1') def test_variable_mixed_explicit(self): r = self.app_.get('/variable_all/4?id=four&month=1&day=12') assert r.status_int == 200 - assert r.body == 'variable_all: 4, day=12, id=four, month=1' + assert r.body == b_('variable_all: 4, day=12, id=four, month=1') def test_variable_post(self): r = self.app_.post('/variable_all/5/dummy') assert r.status_int == 200 - assert r.body == 'variable_all: 5, dummy' + assert r.body == b_('variable_all: 5, dummy') def test_variable_post_with_kwargs(self): r = self.app_.post('/variable_all/6', {'month': '1', 'day': '12'}) assert r.status_int == 200 - assert r.body == 'variable_all: 6, day=12, month=1' + assert r.body == b_('variable_all: 6, day=12, month=1') def test_variable_post_mixed(self): r = self.app_.post( @@ -638,60 +644,63 @@ class TestControllerArguments(PecanTestCase): {'id': 'seven', 'month': '1', 'day': '12'} ) assert r.status_int == 200 - assert r.body == 'variable_all: 7, day=12, id=seven, month=1' + assert r.body == b_('variable_all: 7, day=12, id=seven, month=1') def test_no_remainder(self): try: r = self.app_.get('/eater') assert r.status_int != 200 # pragma: nocover - except Exception, ex: + except Exception as ex: assert type(ex) == TypeError - assert ex.args[0] == 'eater() takes at least 2 arguments (1 given)' + assert ex.args[0] in ( + "eater() takes at least 2 arguments (1 given)", + "eater() missing 1 required positional argument: 'id'" + ) # this messaging changed in Python 3.3 def test_one_remainder(self): r = self.app_.get('/eater/1') assert r.status_int == 200 - assert r.body == 'eater: 1, None, ' + assert r.body == b_('eater: 1, None, ') def test_two_remainders(self): r = self.app_.get('/eater/2/dummy') assert r.status_int == 200 - assert r.body == 'eater: 2, dummy, ' + assert r.body == b_('eater: 2, dummy, ') def test_many_remainders(self): r = self.app_.get('/eater/3/dummy/foo/bar') assert r.status_int == 200 - assert r.body == 'eater: 3, dummy, foo, bar' + assert r.body == b_('eater: 3, dummy, foo, bar') def test_remainder_with_kwargs(self): r = self.app_.get('/eater/4?month=1&day=12') assert r.status_int == 200 - assert r.body == 'eater: 4, None, day=12, month=1' + assert r.body == b_('eater: 4, None, day=12, month=1') def test_remainder_with_many_kwargs(self): r = self.app_.get('/eater/5?id=five&month=1&day=12&dummy=dummy') assert r.status_int == 200 - assert r.body == 'eater: 5, dummy, day=12, month=1' + assert r.body == b_('eater: 5, dummy, day=12, month=1') def test_post_remainder(self): r = self.app_.post('/eater/6') assert r.status_int == 200 - assert r.body == 'eater: 6, None, ' + assert r.body == b_('eater: 6, None, ') def test_post_three_remainders(self): r = self.app_.post('/eater/7/dummy') assert r.status_int == 200 - assert r.body == 'eater: 7, dummy, ' + assert r.body == b_('eater: 7, dummy, ') def test_post_many_remainders(self): r = self.app_.post('/eater/8/dummy/foo/bar') assert r.status_int == 200 - assert r.body == 'eater: 8, dummy, foo, bar' + assert r.body == b_('eater: 8, dummy, foo, bar') def test_post_remainder_with_kwargs(self): r = self.app_.post('/eater/9', {'month': '1', 'day': '12'}) assert r.status_int == 200 - assert r.body == 'eater: 9, None, day=12, month=1' + assert r.body == b_('eater: 9, None, day=12, month=1') def test_post_many_remainders_with_many_kwargs(self): r = self.app_.post( @@ -699,7 +708,7 @@ class TestControllerArguments(PecanTestCase): {'id': 'ten', 'month': '1', 'day': '12', 'dummy': 'dummy'} ) assert r.status_int == 200 - assert r.body == 'eater: 10, dummy, day=12, month=1' + assert r.body == b_('eater: 10, dummy, day=12, month=1') class TestAbort(PecanTestCase): @@ -774,12 +783,12 @@ class TestRedirect(PecanTestCase): assert r.status_int == 302 r = r.follow() assert r.status_int == 200 - assert r.body == 'it worked!' + assert r.body == b_('it worked!') def test_internal(self): r = self.app_.get('/internal') assert r.status_int == 200 - assert r.body == 'it worked!' + assert r.body == b_('it worked!') def test_internal_with_301(self): self.assertRaises(ValueError, self.app_.get, '/bad_internal') @@ -789,7 +798,7 @@ class TestRedirect(PecanTestCase): assert r.status_int == 301 r = r.follow() assert r.status_int == 200 - assert r.body == 'it worked!' + assert r.body == b_('it worked!') def test_x_forward_proto(self): class ChildController(object): @@ -821,14 +830,13 @@ class TestRedirect(PecanTestCase): class TestStreamedResponse(PecanTestCase): def test_streaming_response(self): - import StringIO class RootController(object): @expose(content_type='text/plain') def test(self, foo): if foo == 'stream': # mimic large file - contents = StringIO.StringIO('stream') + contents = six.BytesIO(b_('stream')) response.content_type = 'application/octet-stream' contents.seek(0, os.SEEK_END) response.content_length = contents.tell() @@ -841,11 +849,11 @@ class TestStreamedResponse(PecanTestCase): app = TestApp(Pecan(RootController())) r = app.get('/test/stream') assert r.content_type == 'application/octet-stream' - assert r.body == 'stream' + assert r.body == b_('stream') r = app.get('/test/plain') assert r.content_type == 'text/plain' - assert r.body == 'plain text' + assert r.body == b_('plain text') class TestThreadLocalState(PecanTestCase): @@ -865,7 +873,7 @@ class TestThreadLocalState(PecanTestCase): app = TestApp(Pecan(RootController())) r = app.get('/') assert r.status_int == 200 - assert r.body == '/' + assert r.body == b_('/') def test_request_state_cleanup(self): """ @@ -882,9 +890,9 @@ class TestThreadLocalState(PecanTestCase): app = TestApp(Pecan(RootController())) r = app.get('/') assert r.status_int == 200 - assert r.body == '/' + assert r.body == b_('/') - assert state.__dict__.keys() == ['app'] + assert list(state.__dict__.keys()) == ['app'] class TestFileTypeExtensions(PecanTestCase): @@ -908,22 +916,22 @@ class TestFileTypeExtensions(PecanTestCase): def test_html_extension(self): r = self.app_.get('/index.html') assert r.status_int == 200 - assert r.body == '.html' + assert r.body == b_('.html') def test_image_extension(self): r = self.app_.get('/image.png') assert r.status_int == 200 - assert r.body == '.png' + assert r.body == b_('.png') def test_hidden_file(self): r = self.app_.get('/.vimrc') assert r.status_int == 200 - assert r.body == '' + assert r.body == b_('') def test_multi_dot_extension(self): r = self.app_.get('/gradient.min.js') assert r.status_int == 200 - assert r.body == '.js' + assert r.body == b_('.js') def test_bad_content_type(self): class RootController(object): @@ -934,11 +942,11 @@ class TestFileTypeExtensions(PecanTestCase): app = TestApp(Pecan(RootController())) r = app.get('/') assert r.status_int == 200 - assert r.body == '/' + assert r.body == b_('/') r = app.get('/index.html', expect_errors=True) assert r.status_int == 200 - assert r.body == '/' + assert r.body == b_('/') with warnings.catch_warnings(): warnings.simplefilter("ignore") @@ -957,7 +965,7 @@ class TestFileTypeExtensions(PecanTestCase): r = app.get('/example:x.tiny') assert r.status_int == 200 - assert r.body == 'SOME VALUE' + assert r.body == b_('SOME VALUE') def test_guessing_disabled(self): class RootController(object): @@ -972,7 +980,7 @@ class TestFileTypeExtensions(PecanTestCase): r = app.get('/index.html') assert r.status_int == 200 - assert r.body == 'SOME VALUE' + assert r.body == b_('SOME VALUE') class TestContentTypeByAcceptHeaders(PecanTestCase): @@ -1057,12 +1065,12 @@ class TestCanonicalRouting(PecanTestCase): def test_root(self): r = self.app_.get('/') assert r.status_int == 200 - assert 'index' in r.body + assert b_('index') in r.body def test_index(self): r = self.app_.get('/index') assert r.status_int == 200 - assert 'index' in r.body + assert b_('index') in r.body def test_broken_clients(self): # for broken clients @@ -1073,7 +1081,7 @@ class TestCanonicalRouting(PecanTestCase): def test_sub_controller_with_trailing(self): r = self.app_.get('/sub/') assert r.status_int == 200 - assert 'subindex' in r.body + assert b_('subindex') in r.body def test_sub_controller_redirect(self): r = self.app_.get('/sub', status=302) @@ -1090,23 +1098,23 @@ class TestCanonicalRouting(PecanTestCase): try: self.app_.post('/sub', dict(foo=1)) raise Exception("Post should fail") # pragma: nocover - except Exception, e: + except Exception as e: assert isinstance(e, RuntimeError) def test_with_args(self): r = self.app_.get('/arg/index/foo') assert r.status_int == 200 - assert r.body == 'foo' + assert r.body == b_('foo') def test_accept_noncanonical(self): r = self.app_.get('/accept/') assert r.status_int == 200 - assert 'accept' == r.body + assert r.body == b_('accept') def test_accept_noncanonical_no_trailing_slash(self): r = self.app_.get('/accept') assert r.status_int == 200 - assert 'accept' == r.body + assert r.body == b_('accept') class TestNonCanonical(PecanTestCase): @@ -1143,22 +1151,22 @@ class TestNonCanonical(PecanTestCase): def test_index(self): r = self.app_.get('/') assert r.status_int == 200 - assert 'index' in r.body + assert b_('index') in r.body def test_subcontroller(self): r = self.app_.get('/sub') assert r.status_int == 200 - assert 'subindex' in r.body + assert b_('subindex') in r.body def test_subcontroller_with_kwargs(self): r = self.app_.post('/sub', dict(foo=1)) assert r.status_int == 200 - assert 'subindex' in r.body + assert b_('subindex') in r.body def test_sub_controller_with_trailing(self): r = self.app_.get('/sub/') assert r.status_int == 200 - assert 'subindex' in r.body + assert b_('subindex') in r.body def test_proxy(self): class RootController(object): @@ -1198,7 +1206,6 @@ class TestLogging(PecanTestCase): logging.getLogger('pecantesting').info('HELLO WORLD') return "HELLO WORLD" - from cStringIO import StringIO f = StringIO() app = TestApp(make_app(RootController(), logging={ @@ -1227,7 +1234,6 @@ class TestLogging(PecanTestCase): logging.getLogger('pecantesting').info('HELLO WORLD') return "HELLO WORLD" - from cStringIO import StringIO f = StringIO() from pecan.configuration import conf_from_dict @@ -1271,16 +1277,16 @@ class TestEngines(PecanTestCase): ) r = app.get('/') assert r.status_int == 200 - assert "<h1>Hello, Jonathan!</h1>" in r.body + assert b_("<h1>Hello, Jonathan!</h1>") in r.body r = app.get('/index.html?name=World') assert r.status_int == 200 - assert "<h1>Hello, World!</h1>" in r.body + assert b_("<h1>Hello, World!</h1>") in r.body error_msg = None try: r = app.get('/badtemplate.html') - except Exception, e: + except Exception as e: for error_f in error_formatters: error_msg = error_f(e) if error_msg: @@ -1300,11 +1306,11 @@ class TestEngines(PecanTestCase): ) r = app.get('/') assert r.status_int == 200 - assert "<h1>Hello, Jonathan!</h1>" in r.body + assert b_("<h1>Hello, Jonathan!</h1>") in r.body r = app.get('/index.html?name=World') assert r.status_int == 200 - assert "<h1>Hello, World!</h1>" in r.body + assert b_("<h1>Hello, World!</h1>") in r.body @unittest.skipIf('jinja' not in builtin_renderers, 'Jinja not installed') def test_jinja(self): @@ -1323,12 +1329,12 @@ class TestEngines(PecanTestCase): ) r = app.get('/') assert r.status_int == 200 - assert "<h1>Hello, Jonathan!</h1>" in r.body + assert b_("<h1>Hello, Jonathan!</h1>") in r.body error_msg = None try: r = app.get('/badtemplate.html') - except Exception, e: + except Exception as e: for error_f in error_formatters: error_msg = error_f(e) if error_msg: @@ -1352,16 +1358,16 @@ class TestEngines(PecanTestCase): ) r = app.get('/') assert r.status_int == 200 - assert "<h1>Hello, Jonathan!</h1>" in r.body + assert b_("<h1>Hello, Jonathan!</h1>") in r.body r = app.get('/index.html?name=World') assert r.status_int == 200 - assert "<h1>Hello, World!</h1>" in r.body + assert b_("<h1>Hello, World!</h1>") in r.body error_msg = None try: r = app.get('/badtemplate.html') - except Exception, e: + except Exception as e: for error_f in error_formatters: error_msg = error_f(e) if error_msg: @@ -1387,7 +1393,7 @@ class TestEngines(PecanTestCase): app = TestApp(Pecan(RootController())) r = app.get('/') assert r.status_int == 200 - result = dict(loads(r.body)) + result = dict(loads(r.body.decode())) assert result == expected_result def test_override_template(self): @@ -1400,7 +1406,7 @@ class TestEngines(PecanTestCase): app = TestApp(Pecan(RootController())) r = app.get('/') assert r.status_int == 200 - assert 'Override' in r.body + assert b_('Override') in r.body assert r.content_type == 'text/plain' def test_render(self): @@ -1414,4 +1420,4 @@ class TestEngines(PecanTestCase): ) r = app.get('/') assert r.status_int == 200 - assert "<h1>Hello, Jonathan!</h1>" in r.body + assert b_("<h1>Hello, Jonathan!</h1>") in r.body diff --git a/pecan/tests/test_generic.py b/pecan/tests/test_generic.py index 82a6ca4..879ad26 100644 --- a/pecan/tests/test_generic.py +++ b/pecan/tests/test_generic.py @@ -4,6 +4,8 @@ try: except: from json import dumps # noqa +from six import b as b_ + from pecan import Pecan, expose from pecan.tests import PecanTestCase @@ -27,11 +29,11 @@ class TestGeneric(PecanTestCase): app = TestApp(Pecan(RootController())) r = app.get('/') assert r.status_int == 200 - assert r.body == 'GET' + assert r.body == b_('GET') r = app.post('/') assert r.status_int == 200 - assert r.body == dumps(dict(result='POST')) + assert r.body == b_(dumps(dict(result='POST'))) r = app.get('/do_get', status=404) assert r.status_int == 404 diff --git a/pecan/tests/test_hooks.py b/pecan/tests/test_hooks.py index 8e103b2..66947ca 100644 --- a/pecan/tests/test_hooks.py +++ b/pecan/tests/test_hooks.py @@ -1,9 +1,8 @@ -from cStringIO import StringIO - from webtest import TestApp +from six import b as b_ +from six.moves import cStringIO as StringIO from pecan import make_app, expose, redirect, abort -from pecan.core import state from pecan.hooks import ( PecanHook, TransactionHook, HookController, RequestViewerHook ) @@ -39,7 +38,7 @@ class TestHooks(PecanTestCase): app = TestApp(make_app(RootController(), hooks=[SimpleHook()])) response = app.get('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert len(run_hook) == 4 assert run_hook[0] == 'on_route' @@ -77,7 +76,7 @@ class TestHooks(PecanTestCase): ])) response = app.get('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert len(run_hook) == 10 assert run_hook[0] == 'on_route1' @@ -118,7 +117,7 @@ class TestHooks(PecanTestCase): response = app.get('/') assert response.status_int == 200 - assert response.body == 'Hello World!' + assert response.body == b_('Hello World!') assert len(run_hook) == 2 assert run_hook[0] == 'on_route' @@ -127,7 +126,7 @@ class TestHooks(PecanTestCase): run_hook = [] try: response = app.get('/causeerror') - except Exception, e: + except Exception as e: assert isinstance(e, IndexError) assert len(run_hook) == 2 @@ -167,7 +166,7 @@ class TestHooks(PecanTestCase): app = TestApp(papp) response = app.get('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert len(run_hook) == 10 assert run_hook[0] == 'on_route3' @@ -224,7 +223,7 @@ class TestHooks(PecanTestCase): app = TestApp(make_app(RootController())) response = app.get('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert len(run_hook) == 1 assert run_hook[0] == 'inside' @@ -233,7 +232,7 @@ class TestHooks(PecanTestCase): response = app.get('/sub/') assert response.status_int == 200 - assert response.body == 'Inside here!' + assert response.body == b_('Inside here!') assert len(run_hook) == 3 assert run_hook[0] == 'before' @@ -243,7 +242,7 @@ class TestHooks(PecanTestCase): run_hook = [] response = app.get('/sub/sub/') assert response.status_int == 200 - assert response.body == 'Deep inside here!' + assert response.body == b_('Deep inside here!') assert len(run_hook) == 3 assert run_hook[0] == 'before' @@ -288,7 +287,7 @@ class TestHooks(PecanTestCase): app = TestApp(make_app(RootController(), hooks=[SimpleHook(1)])) response = app.get('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert len(run_hook) == 4 assert run_hook[0] == 'on_route1' @@ -300,7 +299,7 @@ class TestHooks(PecanTestCase): response = app.get('/sub/') assert response.status_int == 200 - assert response.body == 'Inside here!' + assert response.body == b_('Inside here!') assert len(run_hook) == 6 assert run_hook[0] == 'on_route1' @@ -344,7 +343,7 @@ class TestTransactionHook(PecanTestCase): response = app.get('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert len(run_hook) == 3 assert run_hook[0] == 'start_ro' @@ -355,7 +354,7 @@ class TestTransactionHook(PecanTestCase): response = app.post('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert len(run_hook) == 4 assert run_hook[0] == 'start' @@ -450,7 +449,7 @@ class TestTransactionHook(PecanTestCase): response = app.get('/') assert response.status_int == 200 - assert response.body == 'Index Method!' + assert response.body == b_('Index Method!') assert len(run_hook) == 3 assert run_hook[0] == 'start_ro' @@ -461,7 +460,7 @@ class TestTransactionHook(PecanTestCase): response = app.post('/') assert response.status_int == 200 - assert response.body == 'Index Method!' + assert response.body == b_('Index Method!') assert len(run_hook) == 5 assert run_hook[0] == 'start' @@ -474,7 +473,7 @@ class TestTransactionHook(PecanTestCase): response = app.get('/decorated') assert response.status_int == 200 - assert response.body == 'Decorated Method!' + assert response.body == b_('Decorated Method!') assert len(run_hook) == 7 assert run_hook[0] == 'start_ro' @@ -579,7 +578,7 @@ class TestTransactionHook(PecanTestCase): response = app.get('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert len(run_hook) == 3 assert run_hook[0] == 'start_ro' @@ -592,7 +591,7 @@ class TestTransactionHook(PecanTestCase): response = app.post('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert len(run_hook) == 4 assert run_hook[0] == 'start' @@ -822,7 +821,7 @@ class TestTransactionHook(PecanTestCase): response = app.get('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert len(run_hook) == 6 assert run_hook[0] == 'start_ro' @@ -838,7 +837,7 @@ class TestTransactionHook(PecanTestCase): response = app.post('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert len(run_hook) == 4 assert run_hook[0] == 'start' @@ -947,7 +946,7 @@ class TestTransactionHook(PecanTestCase): response = app.get('/generic') assert response.status_int == 200 - assert response.body == 'generic get' + assert response.body == b_('generic get') assert len(run_hook) == 6 assert run_hook[0] == 'start_ro' assert run_hook[1] == 'clear' @@ -965,7 +964,7 @@ class TestTransactionHook(PecanTestCase): response = app.post('/generic') assert response.status_int == 200 - assert response.body == 'generic post' + assert response.body == b_('generic post') assert len(run_hook) == 4 assert run_hook[0] == 'start' assert run_hook[1] == 'inside' @@ -1052,7 +1051,7 @@ class TestRequestViewerHook(PecanTestCase): out = _stdout.getvalue() assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert 'path' in out assert 'method' in out assert 'status' in out @@ -1117,7 +1116,7 @@ class TestRequestViewerHook(PecanTestCase): out = _stdout.getvalue() assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert '/' in out assert 'path' in out assert 'method' not in out @@ -1152,7 +1151,7 @@ class TestRequestViewerHook(PecanTestCase): out = _stdout.getvalue() assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert out == '' def test_item_not_in_defaults(self): @@ -1179,7 +1178,7 @@ class TestRequestViewerHook(PecanTestCase): out = _stdout.getvalue() assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') assert 'date' in out assert 'method' not in out assert 'status' not in out diff --git a/pecan/tests/test_jsonify.py b/pecan/tests/test_jsonify.py index 1c1c589..2dcd663 100644 --- a/pecan/tests/test_jsonify.py +++ b/pecan/tests/test_jsonify.py @@ -72,7 +72,7 @@ class TestJsonify(PecanTestCase): r = app.get('/') assert r.status_int == 200 - assert loads(r.body) == {'name': 'Jonathan LaCour'} + assert loads(r.body.decode()) == {'name': 'Jonathan LaCour'} class TestJsonifyGenericEncoder(PecanTestCase): @@ -203,8 +203,8 @@ class TestJsonifySQLAlchemyGenericEncoder(PecanTestCase): # add some dummy data user_table.insert().execute([ - {'first_name': u'Jonathan', 'last_name': u'LaCour'}, - {'first_name': u'Yoann', 'last_name': u'Roman'} + {'first_name': 'Jonathan', 'last_name': 'LaCour'}, + {'first_name': 'Yoann', 'last_name': 'Roman'} ]) # get the SA objects diff --git a/pecan/tests/test_rest.py b/pecan/tests/test_rest.py index d5e7f5e..c1f8acc 100644 --- a/pecan/tests/test_rest.py +++ b/pecan/tests/test_rest.py @@ -5,6 +5,8 @@ try: except: from json import dumps, loads # noqa +from six import b as b_ + from pecan import abort, expose, make_app, response from pecan.rest import RestController from pecan.tests import PecanTestCase @@ -103,38 +105,38 @@ class TestRestController(PecanTestCase): # test get_all r = app.get('/things') assert r.status_int == 200 - assert r.body == dumps(dict(items=ThingsController.data)) + assert r.body == b_(dumps(dict(items=ThingsController.data))) # test get_one for i, value in enumerate(ThingsController.data): r = app.get('/things/%d' % i) assert r.status_int == 200 - assert r.body == value + assert r.body == b_(value) # test post r = app.post('/things', {'value': 'four'}) assert r.status_int == 302 - assert r.body == 'CREATED' + assert r.body == b_('CREATED') # make sure it works r = app.get('/things/4') assert r.status_int == 200 - assert r.body == 'four' + assert r.body == b_('four') # test edit r = app.get('/things/3/edit') assert r.status_int == 200 - assert r.body == 'EDIT three' + assert r.body == b_('EDIT three') # test put r = app.put('/things/4', {'value': 'FOUR'}) assert r.status_int == 200 - assert r.body == 'UPDATED' + assert r.body == b_('UPDATED') # make sure it works r = app.get('/things/4') assert r.status_int == 200 - assert r.body == 'FOUR' + assert r.body == b_('FOUR') # test put with _method parameter and GET r = app.get('/things/4?_method=put', {'value': 'FOUR!'}, status=405) @@ -143,32 +145,32 @@ class TestRestController(PecanTestCase): # make sure it works r = app.get('/things/4') assert r.status_int == 200 - assert r.body == 'FOUR' + assert r.body == b_('FOUR') # test put with _method parameter and POST r = app.post('/things/4?_method=put', {'value': 'FOUR!'}) assert r.status_int == 200 - assert r.body == 'UPDATED' + assert r.body == b_('UPDATED') # make sure it works r = app.get('/things/4') assert r.status_int == 200 - assert r.body == 'FOUR!' + assert r.body == b_('FOUR!') # test get delete r = app.get('/things/4/delete') assert r.status_int == 200 - assert r.body == 'DELETE FOUR!' + assert r.body == b_('DELETE FOUR!') # test delete r = app.delete('/things/4') assert r.status_int == 200 - assert r.body == 'DELETED' + assert r.body == b_('DELETED') # make sure it works r = app.get('/things') assert r.status_int == 200 - assert len(loads(r.body)['items']) == 4 + assert len(loads(r.body.decode())['items']) == 4 # test delete with _method parameter and GET r = app.get('/things/3?_method=DELETE', status=405) @@ -177,37 +179,37 @@ class TestRestController(PecanTestCase): # make sure it works r = app.get('/things') assert r.status_int == 200 - assert len(loads(r.body)['items']) == 4 + assert len(loads(r.body.decode())['items']) == 4 # test delete with _method parameter and POST r = app.post('/things/3?_method=DELETE') assert r.status_int == 200 - assert r.body == 'DELETED' + assert r.body == b_('DELETED') # make sure it works r = app.get('/things') assert r.status_int == 200 - assert len(loads(r.body)['items']) == 3 + assert len(loads(r.body.decode())['items']) == 3 # test "RESET" custom action r = app.request('/things', method='RESET') assert r.status_int == 200 - assert r.body == 'RESET' + assert r.body == b_('RESET') # test "RESET" custom action with _method parameter r = app.get('/things?_method=RESET') assert r.status_int == 200 - assert r.body == 'RESET' + assert r.body == b_('RESET') # test the "OPTIONS" custom action r = app.request('/things', method='OPTIONS') assert r.status_int == 200 - assert r.body == 'OPTIONS' + assert r.body == b_('OPTIONS') # test the "OPTIONS" custom action with the _method parameter r = app.post('/things', {'_method': 'OPTIONS'}) assert r.status_int == 200 - assert r.body == 'OPTIONS' + assert r.body == b_('OPTIONS') # test the "other" custom action with warnings.catch_warnings(): @@ -224,7 +226,7 @@ class TestRestController(PecanTestCase): warnings.simplefilter("ignore") r = app.request('/things/others/', method='MISC') assert r.status_int == 200 - assert r.body == 'OTHERS' + assert r.body == b_('OTHERS') # test the "others" custom action missing trailing slash with warnings.catch_warnings(): @@ -235,7 +237,7 @@ class TestRestController(PecanTestCase): # test the "others" custom action with the _method parameter r = app.get('/things/others/?_method=MISC') assert r.status_int == 200 - assert r.body == 'OTHERS' + assert r.body == b_('OTHERS') # test an invalid custom action r = app.get('/things?_method=BAD', status=404) @@ -244,27 +246,27 @@ class TestRestController(PecanTestCase): # test custom "GET" request "count" r = app.get('/things/count') assert r.status_int == 200 - assert r.body == '3' + assert r.body == b_('3') # test custom "GET" request "length" r = app.get('/things/1/length') assert r.status_int == 200 - assert r.body == str(len('one')) + assert r.body == b_(str(len('one'))) # test custom "GET" request through subcontroller r = app.get('/things/others/echo?value=test') assert r.status_int == 200 - assert r.body == 'test' + assert r.body == b_('test') # test custom "POST" request "length" r = app.post('/things/1/length', {'value': 'test'}) assert r.status_int == 200 - assert r.body == str(len('onetest')) + assert r.body == b_(str(len('onetest'))) # test custom "POST" request through subcontroller r = app.post('/things/others/echo', {'value': 'test'}) assert r.status_int == 200 - assert r.body == 'test' + assert r.body == b_('test') def test_getall_with_trailing_slash(self): @@ -285,7 +287,7 @@ class TestRestController(PecanTestCase): # test get_all r = app.get('/things/') assert r.status_int == 200 - assert r.body == dumps(dict(items=ThingsController.data)) + assert r.body == b_(dumps(dict(items=ThingsController.data))) def test_simple_nested_rest(self): @@ -299,14 +301,6 @@ class TestRestController(PecanTestCase): def delete(self, id_): return "BAR-%s" % id_ - @expose() - def post(self): - return "BAR-POST" - - @expose() - def delete(self, id_): - return "BAR-%s" % id_ - class FooController(RestController): bar = BarController() @@ -327,19 +321,19 @@ class TestRestController(PecanTestCase): r = app.post('/foo') assert r.status_int == 200 - assert r.body == "FOO-POST" + assert r.body == b_("FOO-POST") r = app.delete('/foo/1') assert r.status_int == 200 - assert r.body == "FOO-1" + assert r.body == b_("FOO-1") r = app.post('/foo/bar') assert r.status_int == 200 - assert r.body == "BAR-POST" + assert r.body == b_("BAR-POST") r = app.delete('/foo/bar/2') assert r.status_int == 200 - assert r.body == "BAR-2" + assert r.body == b_("BAR-2") def test_complicated_nested_rest(self): @@ -437,75 +431,75 @@ class TestRestController(PecanTestCase): # test get_all r = app.get('/foos') assert r.status_int == 200 - assert r.body == dumps(dict(items=FoosController.data)) + assert r.body == b_(dumps(dict(items=FoosController.data))) # test nested get_all r = app.get('/foos/1/bars') assert r.status_int == 200 - assert r.body == dumps(dict(items=BarsController.data[1])) + assert r.body == b_(dumps(dict(items=BarsController.data[1]))) # test get_one for i, value in enumerate(FoosController.data): r = app.get('/foos/%d' % i) assert r.status_int == 200 - assert r.body == value + assert r.body == b_(value) # test nested get_one for i, value in enumerate(FoosController.data): for j, value in enumerate(BarsController.data[i]): r = app.get('/foos/%s/bars/%s' % (i, j)) assert r.status_int == 200 - assert r.body == value + assert r.body == b_(value) # test post r = app.post('/foos', {'value': 'two'}) assert r.status_int == 302 - assert r.body == 'CREATED' + assert r.body == b_('CREATED') # make sure it works r = app.get('/foos/2') assert r.status_int == 200 - assert r.body == 'two' + assert r.body == b_('two') # test nested post r = app.post('/foos/2/bars', {'value': 'two-zero'}) assert r.status_int == 302 - assert r.body == 'CREATED FOR 2' + assert r.body == b_('CREATED FOR 2') # make sure it works r = app.get('/foos/2/bars/0') assert r.status_int == 200 - assert r.body == 'two-zero' + assert r.body == b_('two-zero') # test edit r = app.get('/foos/1/edit') assert r.status_int == 200 - assert r.body == 'EDIT one' + assert r.body == b_('EDIT one') # test nested edit r = app.get('/foos/1/bars/1/edit') assert r.status_int == 200 - assert r.body == 'EDIT one-one' + assert r.body == b_('EDIT one-one') # test put r = app.put('/foos/2', {'value': 'TWO'}) assert r.status_int == 200 - assert r.body == 'UPDATED' + assert r.body == b_('UPDATED') # make sure it works r = app.get('/foos/2') assert r.status_int == 200 - assert r.body == 'TWO' + assert r.body == b_('TWO') # test nested put r = app.put('/foos/2/bars/0', {'value': 'TWO-ZERO'}) assert r.status_int == 200 - assert r.body == 'UPDATED' + assert r.body == b_('UPDATED') # make sure it works r = app.get('/foos/2/bars/0') assert r.status_int == 200 - assert r.body == 'TWO-ZERO' + assert r.body == b_('TWO-ZERO') # test put with _method parameter and GET r = app.get('/foos/2?_method=put', {'value': 'TWO!'}, status=405) @@ -514,7 +508,7 @@ class TestRestController(PecanTestCase): # make sure it works r = app.get('/foos/2') assert r.status_int == 200 - assert r.body == 'TWO' + assert r.body == b_('TWO') # test nested put with _method parameter and GET r = app.get( @@ -526,57 +520,57 @@ class TestRestController(PecanTestCase): # make sure it works r = app.get('/foos/2/bars/0') assert r.status_int == 200 - assert r.body == 'TWO-ZERO' + assert r.body == b_('TWO-ZERO') # test put with _method parameter and POST r = app.post('/foos/2?_method=put', {'value': 'TWO!'}) assert r.status_int == 200 - assert r.body == 'UPDATED' + assert r.body == b_('UPDATED') # make sure it works r = app.get('/foos/2') assert r.status_int == 200 - assert r.body == 'TWO!' + assert r.body == b_('TWO!') # test nested put with _method parameter and POST r = app.post('/foos/2/bars/0?_method=put', {'value': 'TWO-ZERO!'}) assert r.status_int == 200 - assert r.body == 'UPDATED' + assert r.body == b_('UPDATED') # make sure it works r = app.get('/foos/2/bars/0') assert r.status_int == 200 - assert r.body == 'TWO-ZERO!' + assert r.body == b_('TWO-ZERO!') # test get delete r = app.get('/foos/2/delete') assert r.status_int == 200 - assert r.body == 'DELETE TWO!' + assert r.body == b_('DELETE TWO!') # test nested get delete r = app.get('/foos/2/bars/0/delete') assert r.status_int == 200 - assert r.body == 'DELETE TWO-ZERO!' + assert r.body == b_('DELETE TWO-ZERO!') # test nested delete r = app.delete('/foos/2/bars/0') assert r.status_int == 200 - assert r.body == 'DELETED' + assert r.body == b_('DELETED') # make sure it works r = app.get('/foos/2/bars') assert r.status_int == 200 - assert len(loads(r.body)['items']) == 0 + assert len(loads(r.body.decode())['items']) == 0 # test delete r = app.delete('/foos/2') assert r.status_int == 200 - assert r.body == 'DELETED' + assert r.body == b_('DELETED') # make sure it works r = app.get('/foos') assert r.status_int == 200 - assert len(loads(r.body)['items']) == 2 + assert len(loads(r.body.decode())['items']) == 2 # test nested delete with _method parameter and GET r = app.get('/foos/1/bars/1?_method=DELETE', status=405) @@ -585,7 +579,7 @@ class TestRestController(PecanTestCase): # make sure it works r = app.get('/foos/1/bars') assert r.status_int == 200 - assert len(loads(r.body)['items']) == 2 + assert len(loads(r.body.decode())['items']) == 2 # test delete with _method parameter and GET r = app.get('/foos/1?_method=DELETE', status=405) @@ -594,27 +588,27 @@ class TestRestController(PecanTestCase): # make sure it works r = app.get('/foos') assert r.status_int == 200 - assert len(loads(r.body)['items']) == 2 + assert len(loads(r.body.decode())['items']) == 2 # test nested delete with _method parameter and POST r = app.post('/foos/1/bars/1?_method=DELETE') assert r.status_int == 200 - assert r.body == 'DELETED' + assert r.body == b_('DELETED') # make sure it works r = app.get('/foos/1/bars') assert r.status_int == 200 - assert len(loads(r.body)['items']) == 1 + assert len(loads(r.body.decode())['items']) == 1 # test delete with _method parameter and POST r = app.post('/foos/1?_method=DELETE') assert r.status_int == 200 - assert r.body == 'DELETED' + assert r.body == b_('DELETED') # make sure it works r = app.get('/foos') assert r.status_int == 200 - assert len(loads(r.body)['items']) == 1 + assert len(loads(r.body.decode())['items']) == 1 def test_bad_rest(self): @@ -717,7 +711,7 @@ class TestRestController(PecanTestCase): # test custom delete without ID r = app.delete('/things/others/') assert r.status_int == 200 - assert r.body == 'DELETE' + assert r.body == b_('DELETE') # test custom delete without ID with _method parameter and GET r = app.get('/things/others/?_method=delete', status=405) @@ -726,12 +720,12 @@ class TestRestController(PecanTestCase): # test custom delete without ID with _method parameter and POST r = app.post('/things/others/', {'_method': 'delete'}) assert r.status_int == 200 - assert r.body == 'DELETE' + assert r.body == b_('DELETE') # test custom delete with ID r = app.delete('/things/others/reset/1') assert r.status_int == 200 - assert r.body == '1' + assert r.body == b_('1') # test custom delete with ID with _method parameter and GET r = app.get('/things/others/reset/1?_method=delete', status=405) @@ -740,7 +734,7 @@ class TestRestController(PecanTestCase): # test custom delete with ID with _method parameter and POST r = app.post('/things/others/reset/1', {'_method': 'delete'}) assert r.status_int == 200 - assert r.body == '1' + assert r.body == b_('1') def test_get_with_var_args(self): @@ -767,12 +761,12 @@ class TestRestController(PecanTestCase): # test get request r = app.get('/things/one/two/three') assert r.status_int == 200 - assert r.body == 'one, two, three' + assert r.body == b_('one, two, three') # test nested get request r = app.get('/things/one/two/three/others/') assert r.status_int == 200 - assert r.body == 'NESTED: one, two, three' + assert r.body == b_('NESTED: one, two, three') def test_sub_nested_rest(self): @@ -813,7 +807,7 @@ class TestRestController(PecanTestCase): # test sub-nested get_one r = app.get('/foos/0/bars/0/bazs/0') assert r.status_int == 200 - assert r.body == 'zero-zero-zero' + assert r.body == b_('zero-zero-zero') def test_sub_nested_rest_with_overwrites(self): @@ -889,35 +883,35 @@ class TestRestController(PecanTestCase): r = app.post('/foos') assert r.status_int == 200 - assert r.body == 'POST' + assert r.body == b_('POST') r = app.put('/foos/0') assert r.status_int == 200 - assert r.body == 'PUT' + assert r.body == b_('PUT') r = app.post('/foos/bars') assert r.status_int == 200 - assert r.body == 'POST-CHILD' + assert r.body == b_('POST-CHILD') r = app.put('/foos/bars/0') assert r.status_int == 200 - assert r.body == 'PUT-CHILD' + assert r.body == b_('PUT-CHILD') r = app.post('/foos/bars/bazs') assert r.status_int == 200 - assert r.body == 'POST-GRAND-CHILD' + assert r.body == b_('POST-GRAND-CHILD') r = app.put('/foos/bars/bazs/0') assert r.status_int == 200 - assert r.body == 'PUT-GRAND-CHILD' + assert r.body == b_('PUT-GRAND-CHILD') r = app.get('/foos/bars/bazs/final/') assert r.status_int == 200 - assert r.body == 'FINAL' + assert r.body == b_('FINAL') r = app.get('/foos/bars/bazs/final/named') assert r.status_int == 200 - assert r.body == 'NAMED' + assert r.body == b_('NAMED') def test_post_with_kwargs_only(self): @@ -936,7 +930,7 @@ class TestRestController(PecanTestCase): r = app.get('/') assert r.status_int == 200 - assert r.body == 'INDEX' + assert r.body == b_('INDEX') kwargs = {'foo': 'bar', 'spam': 'eggs'} r = app.post('/', kwargs) @@ -1025,43 +1019,43 @@ class TestRestController(PecanTestCase): r = app.get('/foo') assert r.status_int == 200 - assert r.body == 'INDEX' + assert r.body == b_('INDEX') r = app.post('/foo') assert r.status_int == 200 - assert r.body == 'POST' + assert r.body == b_('POST') r = app.get('/foo/1') assert r.status_int == 200 - assert r.body == 'GET ONE' + assert r.body == b_('GET ONE') r = app.post('/foo/1') assert r.status_int == 200 - assert r.body == 'POST-LOOKUP-1' + assert r.body == b_('POST-LOOKUP-1') r = app.put('/foo/1') assert r.status_int == 200 - assert r.body == 'PUT-1' + assert r.body == b_('PUT-1') r = app.delete('/foo/1') assert r.status_int == 200 - assert r.body == 'DELETE-1' + assert r.body == b_('DELETE-1') r = app.put('/foo/1/2') assert r.status_int == 200 - assert r.body == 'PUT-LOOKUP-1-2' + assert r.body == b_('PUT-LOOKUP-1-2') r = app.delete('/foo/1/2') assert r.status_int == 200 - assert r.body == 'DELETE-LOOKUP-1-2' + assert r.body == b_('DELETE-LOOKUP-1-2') r = app.get('/foo/1/2') assert r.status_int == 200 - assert r.body == 'FINAL-2' + assert r.body == b_('FINAL-2') r = app.post('/foo/1/2') assert r.status_int == 200 - assert r.body == 'POST-2' + assert r.body == b_('POST-2') def test_dynamic_rest_lookup(self): class BarController(RestController): @@ -1125,40 +1119,40 @@ class TestRestController(PecanTestCase): r = app.get('/foos') assert r.status_int == 200 - assert r.body == 'FOOS' + assert r.body == b_('FOOS') r = app.post('/foos') assert r.status_int == 200 - assert r.body == 'POST_FOOS' + assert r.body == b_('POST_FOOS') r = app.get('/foos/foo') assert r.status_int == 200 - assert r.body == 'FOO' + assert r.body == b_('FOO') r = app.put('/foos/foo') assert r.status_int == 200 - assert r.body == 'PUT_FOO' + assert r.body == b_('PUT_FOO') r = app.delete('/foos/foo') assert r.status_int == 200 - assert r.body == 'DELETE_FOO' + assert r.body == b_('DELETE_FOO') r = app.get('/foos/foo/bars') assert r.status_int == 200 - assert r.body == 'BARS' + assert r.body == b_('BARS') r = app.post('/foos/foo/bars') assert r.status_int == 200 - assert r.body == 'POST_BARS' + assert r.body == b_('POST_BARS') r = app.get('/foos/foo/bars/bar') assert r.status_int == 200 - assert r.body == 'BAR' + assert r.body == b_('BAR') r = app.put('/foos/foo/bars/bar') assert r.status_int == 200 - assert r.body == 'PUT_BAR' + assert r.body == b_('PUT_BAR') r = app.delete('/foos/foo/bars/bar') assert r.status_int == 200 - assert r.body == 'DELETE_BAR' + assert r.body == b_('DELETE_BAR') diff --git a/pecan/tests/test_scaffolds.py b/pecan/tests/test_scaffolds.py index b0168e3..669aa7d 100644 --- a/pecan/tests/test_scaffolds.py +++ b/pecan/tests/test_scaffolds.py @@ -2,7 +2,8 @@ import os import sys import tempfile import shutil -from cStringIO import StringIO + +from six.moves import cStringIO as StringIO from pecan.tests import PecanTestCase @@ -94,7 +95,6 @@ class TestScaffoldUtils(PecanTestCase): def test_destination_directory_already_exists(self): from pecan.scaffolds import copy_dir - from cStringIO import StringIO f = StringIO() copy_dir( ( diff --git a/pecan/tests/test_secure.py b/pecan/tests/test_secure.py index 1fb1fd9..53a63ca 100644 --- a/pecan/tests/test_secure.py +++ b/pecan/tests/test_secure.py @@ -5,6 +5,7 @@ if sys.version_info < (2, 7): else: import unittest # noqa +from six import b as b_ from webtest import TestApp from pecan import expose, make_app @@ -59,11 +60,11 @@ class TestSecure(PecanTestCase): )) response = app.get('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') response = app.get('/unlocked') assert response.status_int == 200 - assert response.body == 'Sure thing' + assert response.body == b_('Sure thing') response = app.get('/locked', expect_errors=True) assert response.status_int == 401 @@ -73,7 +74,7 @@ class TestSecure(PecanTestCase): response = app.get('/secret/allowed') assert response.status_int == 200 - assert response.body == 'Allowed!' + assert response.body == b_('Allowed!') def test_unlocked_attribute(self): class AuthorizedSubController(object): @@ -121,11 +122,11 @@ class TestSecure(PecanTestCase): )) response = app.get('/') assert response.status_int == 200 - assert response.body == 'Hello, World!' + assert response.body == b_('Hello, World!') response = app.get('/unlocked') assert response.status_int == 200 - assert response.body == 'Sure thing' + assert response.body == b_('Sure thing') response = app.get('/locked', expect_errors=True) assert response.status_int == 401 @@ -135,15 +136,15 @@ class TestSecure(PecanTestCase): response = app.get('/secret/allowed') assert response.status_int == 200 - assert response.body == 'Allowed!' + assert response.body == b_('Allowed!') response = app.get('/secret/authorized/') assert response.status_int == 200 - assert response.body == 'Index' + assert response.body == b_('Index') response = app.get('/secret/authorized/allowed') assert response.status_int == 200 - assert response.body == 'Allowed!' + assert response.body == b_('Allowed!') def test_secure_attribute(self): authorized = False @@ -163,7 +164,7 @@ class TestSecure(PecanTestCase): app = TestApp(make_app(RootController())) response = app.get('/') assert response.status_int == 200 - assert response.body == 'Hello from root!' + assert response.body == b_('Hello from root!') response = app.get('/sub/', expect_errors=True) assert response.status_int == 401 @@ -171,7 +172,7 @@ class TestSecure(PecanTestCase): authorized = True response = app.get('/sub/') assert response.status_int == 200 - assert response.body == 'Hello from sub!' + assert response.body == b_('Hello from sub!') def test_state_attribute(self): from pecan.secure import Any, Protected @@ -187,7 +188,7 @@ class TestSecure(PecanTestCase): try: secure(Foo()) - except Exception, e: + except Exception as e: assert isinstance(e, TypeError) @@ -288,7 +289,7 @@ class TestObjectPathSecurity(PecanTestCase): def test_sub_of_both_not_secret(self): response = self.app.get('/notsecret/hi/') assert response.status_int == 200 - assert response.body == 'Index hi' + assert response.body == b_('Index hi') def test_protected_lookup(self): response = self.app.get('/secret/hi/', expect_errors=True) @@ -297,7 +298,7 @@ class TestObjectPathSecurity(PecanTestCase): self.secret_cls.authorized = True response = self.app.get('/secret/hi/') assert response.status_int == 200 - assert response.body == 'Index hi' + assert response.body == b_('Index hi') assert 'secretcontroller' in self.permissions_checked def test_secured_notfound_lookup(self): @@ -324,7 +325,7 @@ class TestObjectPathSecurity(PecanTestCase): self.deepsecret_cls.authorized = True response = self.app.get('/secret/hi/deepsecret/') assert response.status_int == 200 - assert response.body == 'Deep Secret' + assert response.body == b_('Deep Secret') assert 'secretcontroller' in self.permissions_checked assert 'deepsecret' in self.permissions_checked @@ -333,14 +334,14 @@ class TestObjectPathSecurity(PecanTestCase): self.deepsecret_cls.authorized = True response = self.app.get('/secret/1/deepsecret/2/deepsecret/') assert response.status_int == 200 - assert response.body == 'Deep Secret' + assert response.body == b_('Deep Secret') assert 'secretcontroller' in self.permissions_checked assert 'deepsecret' in self.permissions_checked def test_unlocked_lookup(self): response = self.app.get('/notsecret/1/deepsecret/2/') assert response.status_int == 200 - assert response.body == 'Index 2' + assert response.body == b_('Index 2') assert 'deepsecret' not in self.permissions_checked response = self.app.get( @@ -368,7 +369,7 @@ class TestObjectPathSecurity(PecanTestCase): self.secret_cls.independent_authorization = True response = self.app.get('/secret/independent') assert response.status_int == 200 - assert response.body == 'Independent Security' + assert response.body == b_('Independent Security') assert len(self.permissions_checked) == 1 assert 'independent' in self.permissions_checked @@ -383,7 +384,7 @@ class TestObjectPathSecurity(PecanTestCase): self.secret_cls.independent_authorization = True response = self.app.get('/secret/wrapped/') assert response.status_int == 200 - assert response.body == 'Index wrapped' + assert response.body == b_('Index wrapped') assert len(self.permissions_checked) == 1 assert 'independent' in self.permissions_checked @@ -392,7 +393,7 @@ class TestObjectPathSecurity(PecanTestCase): self.secret_cls.independent_authorization = True response = self.app.get('/secret/lookup_wrapped/') assert response.status_int == 200 - assert response.body == 'Index wrapped' + assert response.body == b_('Index wrapped') assert len(self.permissions_checked) == 2 assert 'independent' in self.permissions_checked assert 'secretcontroller' in self.permissions_checked @@ -400,7 +401,7 @@ class TestObjectPathSecurity(PecanTestCase): def test_unlocked_attribute_in_insecure(self): response = self.app.get('/notsecret/unlocked/') assert response.status_int == 200 - assert response.body == 'Index unlocked' + assert response.body == b_('Index unlocked') class SecureControllerSharedPermissionsRegression(PecanTestCase): diff --git a/pecan/tests/test_templating.py b/pecan/tests/test_templating.py index 90ef4fb..5fffb0c 100644 --- a/pecan/tests/test_templating.py +++ b/pecan/tests/test_templating.py @@ -1,8 +1,10 @@ +import tempfile + +from six import b as b_ + from pecan.templating import RendererFactory, format_line_context from pecan.tests import PecanTestCase -import tempfile - class TestTemplate(PecanTestCase): def setUp(self): @@ -42,7 +44,7 @@ class TestTemplateLineFormat(PecanTestCase): def test_format_line_context(self): for i in range(11): - self.f.write('Testing Line %d\n' % i) + self.f.write(b_('Testing Line %d\n' % i)) self.f.flush() assert format_line_context(self.f.name, 0).count('Testing Line') == 10 |