def extra_headers(self) -> None: sr = StubStartResponse() respond_with_html( sr, "<div>Täst</div>", extra_headers=[("X-Custom-Header", "Foobar")], ) sr.assert_header_equals("X-Custom-Header", "Foobar")
def _respond_arguments_error( start_response: StartResponse, arguments: BadArgumentsDict ) -> Iterable[bytes]: html = bad_arguments_page(arguments) return respond_with_html( start_response, html, status=HTTPStatus.BAD_REQUEST )
def _respond_not_found( environment: WSGIEnvironment, start_response: StartResponse ) -> Iterable[bytes]: path = cast(str, environment.get("PATH_INFO", "")) message = "Path '{}' not found.".format(path) page = http_status_page(HTTPStatus.NOT_FOUND, message=message) return respond_with_html(start_response, page, status=HTTPStatus.NOT_FOUND)
def _respond_internal_server_error( start_response: StartResponse, ) -> Iterable[bytes]: html = http_status_page( HTTPStatus.INTERNAL_SERVER_ERROR, message="Internal server error." ) return respond_with_html( start_response, html, status=HTTPStatus.INTERNAL_SERVER_ERROR )
def respond_with_html( self, html: str, *, status: HTTPStatus = HTTPStatus.OK, extra_headers: Sequence[Header] = [], ) -> Iterable[bytes]: return respond_with_html( self.start_response, html, status=status, extra_headers=extra_headers, )
def _respond_http_exception( start_response: StartResponse, exception: HTTPException ) -> Iterable[bytes]: assert exception.code is not None status = HTTPStatus(exception.code) html = http_status_page(status, message=exception.description or "") headers = [ h for h in exception.get_headers() if h[0].lower() != "content-type" ] return respond_with_html( start_response, html, status=status, extra_headers=headers, )
def _respond_method_not_allowed( start_response: StartResponse, method: str, allowed_methods: Iterable[str] | None, ) -> Iterable[bytes]: method_string = " or ".join(allowed_methods or []) message = "Method '{}' not allowed. Please try {}.".format( method, method_string ) html = http_status_page(HTTPStatus.METHOD_NOT_ALLOWED, message=message) return respond_with_html( start_response, html, status=HTTPStatus.METHOD_NOT_ALLOWED, extra_headers=[("Allow", ", ".join(allowed_methods or []))], )
def return_value_encoding(self) -> None: sr = StubStartResponse() response = respond_with_html(sr, "<div>Täst</div>") assert_equal("<div>Täst</div>".encode("utf-8"), b"".join(response))
def return_value(self) -> None: sr = StubStartResponse() response = respond_with_html(sr, "<div>Test</div>") assert_equal(b"<div>Test</div>", b"".join(response))
def content_length(self) -> None: sr = StubStartResponse() respond_with_html(sr, "<div>Test</div>") sr.assert_header_equals("Content-Length", "15")
def content_type(self) -> None: sr = StubStartResponse() respond_with_html(sr, "<div>Test</div>") sr.assert_header_equals("Content-Type", "text/html; charset=utf-8")
def custom_status(self) -> None: sr = StubStartResponse() respond_with_html(sr, "<div>Test</div>", status=HTTPStatus.NOT_ACCEPTABLE) sr.assert_status(HTTPStatus.NOT_ACCEPTABLE)
def default_status(self) -> None: sr = StubStartResponse() respond_with_html(sr, "<div>Test</div>") sr.assert_status(HTTPStatus.OK)