1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
from __future__ import annotations
from typing import Any, Callable, Iterable
import pytest
from hypercorn.typing import Scope
from hypercorn.utils import (
build_and_validate_headers,
filter_pseudo_headers,
is_asgi,
suppress_body,
)
@pytest.mark.parametrize(
"method, status, expected", [("HEAD", 200, True), ("GET", 200, False), ("GET", 101, True)]
)
def test_suppress_body(method: str, status: int, expected: bool) -> None:
assert suppress_body(method, status) is expected
class ASGIClassInstance:
def __init__(self) -> None:
pass
async def __call__(self, scope: Scope, receive: Callable, send: Callable) -> None:
pass
async def asgi_callable(scope: Scope, receive: Callable, send: Callable) -> None:
pass
class WSGIClassInstance:
def __init__(self) -> None:
pass
def __call__(self, environ: dict, start_response: Callable) -> Iterable[bytes]:
pass
def wsgi_callable(environ: dict, start_response: Callable) -> Iterable[bytes]:
pass
@pytest.mark.parametrize(
"app, expected",
[
(WSGIClassInstance(), False),
(ASGIClassInstance(), True),
(wsgi_callable, False),
(asgi_callable, True),
],
)
def test_is_asgi(app: Any, expected: bool) -> None:
assert is_asgi(app) == expected
def test_build_and_validate_headers_validate() -> None:
with pytest.raises(TypeError):
build_and_validate_headers([("string", "string")]) # type: ignore
def test_build_and_validate_headers_pseudo() -> None:
with pytest.raises(ValueError):
build_and_validate_headers([(b":authority", b"quart")])
def test_filter_pseudo_headers() -> None:
result = filter_pseudo_headers(
[(b":authority", b"quart"), (b":path", b"/"), (b"user-agent", b"something")]
)
assert result == [(b"host", b"quart"), (b"user-agent", b"something")]
def test_filter_pseudo_headers_no_authority() -> None:
result = filter_pseudo_headers(
[(b"host", b"quart"), (b":path", b"/"), (b"user-agent", b"something")]
)
assert result == [(b"host", b"quart"), (b"user-agent", b"something")]
|