diff --git a/gunicorn/util.py b/gunicorn/util.py index 8aa441d0..df93e875 100644 --- a/gunicorn/util.py +++ b/gunicorn/util.py @@ -363,7 +363,7 @@ def import_app(module): except NameError: if is_debug: traceback.print_exception(*sys.exc_info()) - raise AppImportError("Failed to find application: %r" % module) + raise AppImportError("Failed to find application object %r in %r" % (obj, module)) if app is None: raise AppImportError("Failed to find application object: %r" % obj) diff --git a/tests/support.py b/tests/support.py index c01a9d81..1f082211 100644 --- a/tests/support.py +++ b/tests/support.py @@ -2,10 +2,26 @@ import functools import sys import unittest import platform +from wsgiref.validate import validator HOST = "127.0.0.1" +@validator +def app(environ, start_response): + """Simplest possible application object""" + + data = b'Hello, World!\n' + status = '200 OK' + + response_headers = [ + ('Content-type', 'text/plain'), + ('Content-Length', str(len(data))), + ] + start_response(status, response_headers) + return iter([data]) + + def requires_mac_ver(*min_version): """Decorator raising SkipTest if the OS is Mac OS X and the OS X version if less than min_version. diff --git a/tests/test_util.py b/tests/test_util.py index dc6c7ef4..6660f66e 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -6,6 +6,7 @@ import pytest from gunicorn import util +from gunicorn.errors import AppImportError @pytest.mark.parametrize('test_input, expected', [ @@ -45,3 +46,16 @@ def test_warn(capsys): util.warn('test warn') _, err = capsys.readouterr() assert '!!! WARNING: test warn' in err + + +def test_import_app(): + assert util.import_app('support:app') + + with pytest.raises(ImportError) as err: + util.import_app('a:app') + assert 'No module' in str(err) + + with pytest.raises(AppImportError) as err: + util.import_app('support:wrong_app') + msg = "Failed to find application object 'wrong_app' in 'support'" + assert msg in str(err)