def respond_with_content( self, content: bytes, *, status: HTTPStatus = HTTPStatus.OK, content_type: str = "application/octet-stream", extra_headers: Sequence[Header] = [], ) -> Iterable[bytes]: """Prepare an WSGI response. >>> def handler(start_response, request): ... return respond_with_content(start_response, b"content") The response will include a Content-Type and a Content-Length header. """ return respond_with_content( self.start_response, content, status=status, content_type=content_type, extra_headers=extra_headers, )
def custom_content_type(self) -> None: sr = StubStartResponse() respond_with_content(sr, b"", content_type="text/plain") sr.assert_header_equals("Content-Type", "text/plain")
def default_content_type(self) -> None: sr = StubStartResponse() respond_with_content(sr, b"") sr.assert_header_equals("Content-Type", "application/octet-stream")
def custom_status(self) -> None: sr = StubStartResponse() respond_with_content(sr, b"", status=HTTPStatus.NOT_ACCEPTABLE) sr.assert_status(HTTPStatus.NOT_ACCEPTABLE)
def default_status(self) -> None: sr = StubStartResponse() respond_with_content(sr, b"") sr.assert_status(HTTPStatus.OK)
def return_value(self) -> None: sr = StubStartResponse() response = respond_with_content(sr, b"foobar") assert_equal(b"foobar", b"".join(response))
def extra_headers(self) -> None: sr = StubStartResponse() respond_with_content(sr, b"", extra_headers=[("X-Custom-Header", "Foobar")]) sr.assert_header_equals("X-Custom-Header", "Foobar")
def content_length(self) -> None: sr = StubStartResponse() respond_with_content(sr, b"foobar") sr.assert_header_equals("Content-Length", "6")