gunicorn/tests/support.py
David Lord 19cb68f4c3 load application from factory function (#2178)
* load application from factory function

Use `ast.parse` to validate that the string passed to the CLI is either
an attribute name or a function call. Use `ast.literal_eval` to parse
any positional and keyword arguments to the function. Call the function
to get the real application.

Co-authored-by: Connor Brinton <connor.brinton@gmail.com>

* test coverage for util.import_app

* document app factory pattern
2019-11-19 23:09:53 +01:00

64 lines
1.7 KiB
Python

import functools
import sys
import unittest
import platform
from wsgiref.validate import validator
HOST = "127.0.0.1"
def create_app(name="World", count=1):
message = (('Hello, %s!\n' % name) * count).encode("utf8")
length = str(len(message))
@validator
def app(environ, start_response):
"""Simplest possible application object"""
status = '200 OK'
response_headers = [
('Content-type', 'text/plain'),
('Content-Length', length),
]
start_response(status, response_headers)
return iter([message])
return app
app = application = create_app()
none_app = None
def error_factory():
raise TypeError("inner")
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.
For example, @requires_mac_ver(10, 5) raises SkipTest if the OS X version
is lesser than 10.5.
"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kw):
if sys.platform == 'darwin':
version_txt = platform.mac_ver()[0]
try:
version = tuple(map(int, version_txt.split('.')))
except ValueError:
pass
else:
if version < min_version:
min_version_txt = '.'.join(map(str, min_version))
raise unittest.SkipTest(
"Mac OS X %s or higher required, not %s"
% (min_version_txt, version_txt))
return func(*args, **kw)
wrapper.min_version = min_version
return wrapper
return decorator