summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Petrello <lists@ryanpetrello.com>2013-04-30 15:06:20 -0400
committerRyan Petrello <lists@ryanpetrello.com>2013-04-30 16:51:57 -0400
commitb6b2dce997c12211f00ff622d14c994f86106ad3 (patch)
tree49e747cf5bd8db965908344d56a1d5d31c95002d
parente5f7e9dd8034d00dae57718c47b1b5894b887719 (diff)
downloadpecan-b6b2dce997c12211f00ff622d14c994f86106ad3.tar.gz
Cleanup and add a ton of bytes() compatability for tests.
-rw-r--r--pecan/compat/__init__.py7
-rw-r--r--pecan/middleware/errordocument.py4
-rw-r--r--pecan/scaffolds/__init__.py6
-rw-r--r--pecan/tests/middleware/test_debug.py7
-rw-r--r--pecan/tests/middleware/test_errordocument.py13
-rw-r--r--pecan/tests/middleware/test_recursive.py7
-rw-r--r--pecan/tests/test_base.py256
-rw-r--r--pecan/tests/test_generic.py6
-rw-r--r--pecan/tests/test_hooks.py49
-rw-r--r--pecan/tests/test_jsonify.py2
-rw-r--r--pecan/tests/test_rest.py162
-rw-r--r--pecan/tests/test_templating.py8
12 files changed, 268 insertions, 259 deletions
diff --git a/pecan/compat/__init__.py b/pecan/compat/__init__.py
index ac82f91..3869c12 100644
--- a/pecan/compat/__init__.py
+++ b/pecan/compat/__init__.py
@@ -9,13 +9,6 @@ else:
text_type = unicode
-def bytes_(s, encoding='latin-1', errors='strict'):
- """ If ``s`` is an instance of ``text_type``, return
- ``s.encode(encoding, errors)``, otherwise return ``s``"""
- if isinstance(s, text_type): # pragma: no cover
- return s.encode(encoding, errors)
- return s
-
if PY3: # pragma: no cover
def native_(s, encoding='latin-1', errors='strict'):
""" If ``s`` is an instance of ``text_type``, return
diff --git a/pecan/middleware/errordocument.py b/pecan/middleware/errordocument.py
index 82b2590..12a3c9e 100644
--- a/pecan/middleware/errordocument.py
+++ b/pecan/middleware/errordocument.py
@@ -1,6 +1,6 @@
import sys
-from pecan.compat import bytes_
+from six import b as b_
from .recursive import ForwardRequestException, RecursionLoop
@@ -32,7 +32,7 @@ class StatusPersist(object):
[('Content-type', 'text/plain')],
sys.exc_info()
)
- return [bytes_(
+ return [b_(
'Error: %s. (Error page could not be fetched)' % self.status
)]
diff --git a/pecan/scaffolds/__init__.py b/pecan/scaffolds/__init__.py
index cd816da..bbcd677 100644
--- a/pecan/scaffolds/__init__.py
+++ b/pecan/scaffolds/__init__.py
@@ -3,7 +3,10 @@ import os
import re
import pkg_resources
from string import Template
-from pecan.compat import native_, bytes_
+
+import six
+
+from pecan.compat import native_
DEFAULT_SCAFFOLD = 'base'
_bad_chars_re = re.compile('[^a-zA-Z0-9_]')
@@ -119,4 +122,5 @@ def render_template(content, variables):
"""
fsenc = sys.getfilesystemencoding()
content = native_(content, fsenc)
+ bytes_ = lambda s, enc: s.encode(enc) if six.PY3 else s
return bytes_(Template(content).substitute(variables), fsenc)
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..c37de2e 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,7 +54,7 @@ class TestErrorDocumentMiddleware(PecanTestCase):
)))
r = app.get('/', expect_errors=True)
assert r.status_int == 404
- assert r.body == ('Error: 404 Not Found. '
+ assert r.body == b_('Error: 404 Not Found. '
'(Error page could not be fetched)')
def test_original_exception(self):
@@ -85,6 +86,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 451155b..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:
diff --git a/pecan/tests/test_base.py b/pecan/tests/test_base.py
index 64eb3e4..29278f1 100644
--- a/pecan/tests/test_base.py
+++ b/pecan/tests/test_base.py
@@ -7,6 +7,8 @@ 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 (
@@ -45,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):
@@ -98,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/')
@@ -122,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):
@@ -155,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):
@@ -253,12 +255,12 @@ class TestControllerArguments(PecanTestCase):
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)
@@ -267,90 +269,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)
@@ -359,57 +361,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', {
@@ -417,34 +419,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)
@@ -453,54 +455,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(
@@ -508,7 +510,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(
@@ -521,52 +523,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)
@@ -575,19 +577,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(
@@ -596,42 +598,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(
@@ -639,7 +641,7 @@ 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:
@@ -652,47 +654,47 @@ class TestControllerArguments(PecanTestCase):
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(
@@ -700,7 +702,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):
@@ -775,12 +777,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')
@@ -790,7 +792,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):
@@ -828,7 +830,7 @@ class TestStreamedResponse(PecanTestCase):
def test(self, foo):
if foo == 'stream':
# mimic large file
- contents = 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 +843,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 +867,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 +884,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 +910,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 +936,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 +959,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 +974,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 +1059,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 +1075,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)
@@ -1096,17 +1098,17 @@ class TestCanonicalRouting(PecanTestCase):
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 +1145,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):
@@ -1269,11 +1271,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
error_msg = None
try:
@@ -1298,11 +1300,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):
@@ -1321,7 +1323,7 @@ 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:
@@ -1350,11 +1352,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
error_msg = None
try:
@@ -1385,7 +1387,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):
@@ -1398,7 +1400,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):
@@ -1412,4 +1414,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 9b7ce22..66947ca 100644
--- a/pecan/tests/test_hooks.py
+++ b/pecan/tests/test_hooks.py
@@ -1,4 +1,5 @@
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
@@ -37,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'
@@ -75,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'
@@ -116,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'
@@ -165,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'
@@ -222,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'
@@ -231,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'
@@ -241,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'
@@ -286,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'
@@ -298,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'
@@ -342,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'
@@ -353,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'
@@ -448,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'
@@ -459,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'
@@ -472,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'
@@ -577,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'
@@ -590,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'
@@ -820,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'
@@ -836,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'
@@ -945,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'
@@ -963,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'
@@ -1050,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
@@ -1115,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
@@ -1150,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):
@@ -1177,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 f87ae4a..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):
diff --git a/pecan/tests/test_rest.py b/pecan/tests/test_rest.py
index 6cd7bba..4d70f16 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):
@@ -327,19 +329,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 +439,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 +516,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 +528,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 +587,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 +596,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 +719,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 +728,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 +742,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 +769,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 +815,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 +891,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 +938,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)
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