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, )