diff options
Diffstat (limited to 'tests/common_wsgi.py')
-rw-r--r-- | tests/common_wsgi.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/common_wsgi.py b/tests/common_wsgi.py new file mode 100644 index 0000000..21e8deb --- /dev/null +++ b/tests/common_wsgi.py @@ -0,0 +1,50 @@ +from dataclasses import dataclass +from types import TracebackType +from typing import Optional, Union +from wsgiref.util import setup_testing_defaults + + +@dataclass +class Response: + data: bytes + status: str + headers: list[tuple[str, str]] + exc_info: Union[ + None, + tuple[type[BaseException], BaseException, TracebackType], + tuple[None, None, None], + ] + + +def call_app(app, environ={}): + def write(_): + raise AssertionError("write was called, but should not have been") + + status = None + headers = None + exc_info = None + + def start_response(s, h, e_i=None): + nonlocal status, headers, exc_info + if isinstance(headers, list) and e_i is None: + raise AssertionError("Start response called twice") + status = s + headers = h + exc_info = e_i + return write + + setup_testing_defaults(environ) + if "QUERY_STRING" not in environ: + environ["QUERY_STRING"] = "" + resp_iter = app(environ, start_response) + assert isinstance(status, str) + assert isinstance(headers, list) + resp = b"".join(resp_iter) + if hasattr(resp_iter, "close"): + resp_iter.close() + return Response( + resp, + status, + headers, + exc_info, + ) |