diff options
Diffstat (limited to 'tests')
-rwxr-xr-x | tests/cgiapp_data/form.cgi | 59 | ||||
-rw-r--r-- | tests/test_cgiapp.py | 23 | ||||
-rw-r--r-- | tests/test_doctests.py | 5 | ||||
-rw-r--r-- | tests/test_template.txt | 8 | ||||
-rw-r--r-- | tests/test_wsgiwrappers.py | 2 |
5 files changed, 90 insertions, 7 deletions
diff --git a/tests/cgiapp_data/form.cgi b/tests/cgiapp_data/form.cgi index 2181998..c4c562d 100755 --- a/tests/cgiapp_data/form.cgi +++ b/tests/cgiapp_data/form.cgi @@ -1,11 +1,68 @@ #!/usr/bin/env python +from __future__ import print_function + import cgi +import six print('Content-type: text/plain') print('') -form = cgi.FieldStorage() +if six.PY3: + # Python 3: cgi.FieldStorage keeps some field names as unicode and some as + # the repr() of byte strings, duh. + + class FieldStorage(cgi.FieldStorage): + + def _key_candidates(self, key): + yield key + + try: + # assume bytes, coerce to str + try: + yield key.decode(self.encoding) + except UnicodeDecodeError: + pass + except AttributeError: + # assume str, coerce to bytes + try: + yield key.encode(self.encoding) + except UnicodeEncodeError: + pass + + def __getitem__(self, key): + + superobj = super(FieldStorage, self) + + error = None + + for candidate in self._key_candidates(key): + if isinstance(candidate, bytes): + # ouch + candidate = repr(candidate) + try: + return superobj.__getitem__(candidate) + except KeyError as e: + if error is None: + error = e + + # fall through, re-raise the first KeyError + raise error + + def __contains__(self, key): + superobj = super(FieldStorage, self) + + for candidate in self._key_candidates(key): + if superobj.__contains__(candidate): + return True + return False + +else: # PY2 + + FieldStorage = cgi.FieldStorage + + +form = FieldStorage() print('Filename: %s' % form['up'].filename) print('Name: %s' % form['name'].value) diff --git a/tests/test_cgiapp.py b/tests/test_cgiapp.py index 12cb2be..900e83e 100644 --- a/tests/test_cgiapp.py +++ b/tests/test_cgiapp.py @@ -8,6 +8,29 @@ data_dir = os.path.join(os.path.dirname(__file__), 'cgiapp_data') # these CGI scripts can't work on Windows or Jython if sys.platform != 'win32' and not sys.platform.startswith('java'): + + # Ensure the CGI scripts are called with the same python interpreter. Put a + # symlink to the interpreter executable into the path... + def setup_module(): + global oldpath, pyexelink + oldpath = os.environ.get('PATH', None) + os.environ['PATH'] = data_dir + os.path.pathsep + oldpath + pyexelink = os.path.join(data_dir, "python") + try: + os.unlink(pyexelink) + except OSError: + pass + os.symlink(sys.executable, pyexelink) + + # ... and clean up again. + def teardown_module(): + global oldpath, pyexelink + os.unlink(pyexelink) + if oldpath is not None: + os.environ['PATH'] = oldpath + else: + del os.environ['PATH'] + def test_ok(): app = TestApp(CGIApplication({}, script='ok.cgi', path=[data_dir])) res = app.get('') diff --git a/tests/test_doctests.py b/tests/test_doctests.py index 875fbc2..d59d666 100644 --- a/tests/test_doctests.py +++ b/tests/test_doctests.py @@ -1,3 +1,4 @@ +import six import doctest from paste.util.import_string import simple_import import os @@ -25,7 +26,9 @@ modules = [ 'paste.request', ] -options = doctest.ELLIPSIS|doctest.REPORT_ONLY_FIRST_FAILURE +options = doctest.ELLIPSIS | doctest.REPORT_ONLY_FIRST_FAILURE +if six.PY3: + options |= doctest.IGNORE_EXCEPTION_DETAIL def test_doctests(): for filename in filenames: diff --git a/tests/test_template.txt b/tests/test_template.txt index 45a85e2..1313d34 100644 --- a/tests/test_template.txt +++ b/tests/test_template.txt @@ -6,7 +6,7 @@ example:: 'Hi Ian' >>> Template('Hi {{repr(name)}}').substitute(name='Ian') "Hi 'Ian'" - >>> Template('Hi {{name+1}}').substitute(name='Ian') + >>> Template('Hi {{name+1}}').substitute(name='Ian') # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... TypeError: cannot concatenate 'str' and 'int' objects at line 1 column 6 @@ -97,8 +97,8 @@ in Python, but it's more useful in templates generally):: ... elif item == 'orange': ... assert loop.last ... if loop.first_group(lambda i: i[0].upper()): - ... print '%s:' % item[0].upper() - ... print loop.number, item + ... print('%s:' % item[0].upper()) + ... print("%s %s" % (loop.number, item)) A: 1 apple 2 asparagus @@ -125,7 +125,7 @@ for a variable, if no value is given:: >>> sub('{{default x=1}}{{x}}') '1' >>> # The normal case: - >>> sub('{{x}}') + >>> sub('{{x}}') # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... NameError: name 'x' is not defined at line 1 column 3 diff --git a/tests/test_wsgiwrappers.py b/tests/test_wsgiwrappers.py index 8719693..75d03ed 100644 --- a/tests/test_wsgiwrappers.py +++ b/tests/test_wsgiwrappers.py @@ -13,7 +13,7 @@ class AssertApp(object): def __call__(self, environ, start_response): start_response('200 OK', [('Content-type','text/plain')]) self.assertfunc(environ) - return ['Passed'] + return [b'Passed'] no_encoding = object() def valid_name(name, encoding=no_encoding, post=False): |