Example #1
0
    def start_response_not_called(self) -> None:
        def app(_: WSGIEnvironment, __: StartResponse) -> Iterable[bytes]:
            return []

        request = create_request("GET", "/foo/bar")
        with assert_raises(AssertionError):
            run_wsgi_test(app, request)
Example #2
0
    def start_response_called_after_output_written(self) -> None:
        def app(_: WSGIEnvironment, sr: StartResponse) -> Iterable[bytes]:
            writer = sr("200 OK", [])
            writer(b"abc")
            sr("404 OK", [], _get_exc_info())
            return []

        request = create_request("GET", "/foo/bar")
        with assert_raises(ValueError):
            run_wsgi_test(app, request)
Example #3
0
    def start_response_called_multiple_times_with_exc_info(self) -> None:
        assert_raised = False

        def app(_: WSGIEnvironment, sr: StartResponse) -> Iterable[bytes]:
            nonlocal assert_raised
            sr("200 OK", [])
            try:
                sr("404 Not Found", [], _get_exc_info())
            except AssertionError:
                assert_raised = True
            return []

        request = create_request("GET", "/foo/bar")
        run_wsgi_test(app, request)
        assert_false(assert_raised)
Example #4
0
    def response(self) -> None:
        def app(_: WSGIEnvironment, sr: StartResponse) -> Iterable[bytes]:
            sr("404 Not Found", [("X-Foo", "Bar")])
            return []

        request = create_request("GET", "/foo/bar")
        response = run_wsgi_test(app, request)
        response.assert_status(HTTPStatus.NOT_FOUND)
        response.assert_header_equal("X-Foo", "Bar")
Example #5
0
    def start_response_called_no_output_written(self) -> None:
        def app(_: WSGIEnvironment, sr: StartResponse) -> Iterable[bytes]:
            writer = sr("200 OK", [])
            writer(b"")
            sr("404 OK", [], _get_exc_info())
            return []

        request = create_request("GET", "/foo/bar")
        response = run_wsgi_test(app, request)
        response.assert_status(HTTPStatus.NOT_FOUND)
Example #6
0
    def response_body(self) -> None:
        def app(_: WSGIEnvironment, sr: StartResponse) -> Iterable[bytes]:
            writer = sr("200 OK", [])
            writer(b"Abc")
            writer(b"def")
            return [b"Foo", b"bar"]

        request = create_request("GET", "/foo/bar")
        response = run_wsgi_test(app, request)
        assert_equal(b"AbcdefFoobar", response.body)
Example #7
0
    def run_app(self) -> None:
        app_run = False
        env: WSGIEnvironment | None = None

        def app(
            environ: WSGIEnvironment, sr: StartResponse
        ) -> Iterable[bytes]:
            nonlocal app_run, env
            app_run = True
            env = environ
            sr("200 OK", [])
            return []

        request = create_request("GET", "/foo/bar")
        request.add_argument("foo", "bar")
        run_wsgi_test(app, request)
        assert_true(app_run, "app not run")
        assert env is not None
        assert_equal("foo=bar", env.get("QUERY_STRING"))