Beispiel #1
0
 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")
Beispiel #2
0
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
    )
Beispiel #3
0
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)
Beispiel #4
0
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
    )
Beispiel #5
0
 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,
     )
Beispiel #6
0
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,
    )
Beispiel #7
0
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 []))],
    )
Beispiel #8
0
 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))
Beispiel #9
0
 def return_value(self) -> None:
     sr = StubStartResponse()
     response = respond_with_html(sr, "<div>Test</div>")
     assert_equal(b"<div>Test</div>", b"".join(response))
Beispiel #10
0
 def content_length(self) -> None:
     sr = StubStartResponse()
     respond_with_html(sr, "<div>Test</div>")
     sr.assert_header_equals("Content-Length", "15")
Beispiel #11
0
 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")
Beispiel #12
0
 def custom_status(self) -> None:
     sr = StubStartResponse()
     respond_with_html(sr,
                       "<div>Test</div>",
                       status=HTTPStatus.NOT_ACCEPTABLE)
     sr.assert_status(HTTPStatus.NOT_ACCEPTABLE)
Beispiel #13
0
 def default_status(self) -> None:
     sr = StubStartResponse()
     respond_with_html(sr, "<div>Test</div>")
     sr.assert_status(HTTPStatus.OK)