2018-10-15 11:08:10 +00:00
|
|
|
from starlette.applications import Starlette
|
2022-02-10 09:25:00 +00:00
|
|
|
from starlette.middleware import Middleware
|
2018-10-15 11:08:10 +00:00
|
|
|
from starlette.middleware.gzip import GZipMiddleware
|
2024-02-05 10:46:34 +00:00
|
|
|
from starlette.requests import Request
|
|
|
|
from starlette.responses import ContentStream, PlainTextResponse, StreamingResponse
|
2022-02-10 09:25:00 +00:00
|
|
|
from starlette.routing import Route
|
2024-07-27 09:31:16 +00:00
|
|
|
from tests.types import TestClientFactory
|
2018-10-15 11:08:10 +00:00
|
|
|
|
|
|
|
|
2024-02-05 10:46:34 +00:00
|
|
|
def test_gzip_responses(test_client_factory: TestClientFactory) -> None:
|
|
|
|
def homepage(request: Request) -> PlainTextResponse:
|
2018-10-15 11:08:10 +00:00
|
|
|
return PlainTextResponse("x" * 4000, status_code=200)
|
|
|
|
|
2022-02-10 09:25:00 +00:00
|
|
|
app = Starlette(
|
|
|
|
routes=[Route("/", endpoint=homepage)],
|
|
|
|
middleware=[Middleware(GZipMiddleware)],
|
|
|
|
)
|
|
|
|
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2018-10-15 11:08:10 +00:00
|
|
|
response = client.get("/", headers={"accept-encoding": "gzip"})
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.text == "x" * 4000
|
|
|
|
assert response.headers["Content-Encoding"] == "gzip"
|
|
|
|
assert int(response.headers["Content-Length"]) < 4000
|
|
|
|
|
|
|
|
|
2024-02-05 10:46:34 +00:00
|
|
|
def test_gzip_not_in_accept_encoding(test_client_factory: TestClientFactory) -> None:
|
|
|
|
def homepage(request: Request) -> PlainTextResponse:
|
2018-10-15 11:08:10 +00:00
|
|
|
return PlainTextResponse("x" * 4000, status_code=200)
|
|
|
|
|
2022-02-10 09:25:00 +00:00
|
|
|
app = Starlette(
|
|
|
|
routes=[Route("/", endpoint=homepage)],
|
|
|
|
middleware=[Middleware(GZipMiddleware)],
|
|
|
|
)
|
|
|
|
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2018-10-15 11:08:10 +00:00
|
|
|
response = client.get("/", headers={"accept-encoding": "identity"})
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.text == "x" * 4000
|
|
|
|
assert "Content-Encoding" not in response.headers
|
|
|
|
assert int(response.headers["Content-Length"]) == 4000
|
|
|
|
|
|
|
|
|
2024-02-05 10:46:34 +00:00
|
|
|
def test_gzip_ignored_for_small_responses(
|
|
|
|
test_client_factory: TestClientFactory,
|
|
|
|
) -> None:
|
|
|
|
def homepage(request: Request) -> PlainTextResponse:
|
2018-10-15 11:08:10 +00:00
|
|
|
return PlainTextResponse("OK", status_code=200)
|
|
|
|
|
2022-02-10 09:25:00 +00:00
|
|
|
app = Starlette(
|
|
|
|
routes=[Route("/", endpoint=homepage)],
|
|
|
|
middleware=[Middleware(GZipMiddleware)],
|
|
|
|
)
|
|
|
|
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2018-10-15 11:08:10 +00:00
|
|
|
response = client.get("/", headers={"accept-encoding": "gzip"})
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.text == "OK"
|
|
|
|
assert "Content-Encoding" not in response.headers
|
|
|
|
assert int(response.headers["Content-Length"]) == 2
|
|
|
|
|
|
|
|
|
2024-02-05 10:46:34 +00:00
|
|
|
def test_gzip_streaming_response(test_client_factory: TestClientFactory) -> None:
|
|
|
|
def homepage(request: Request) -> StreamingResponse:
|
|
|
|
async def generator(bytes: bytes, count: int) -> ContentStream:
|
2018-10-15 11:08:10 +00:00
|
|
|
for index in range(count):
|
|
|
|
yield bytes
|
|
|
|
|
|
|
|
streaming = generator(bytes=b"x" * 400, count=10)
|
|
|
|
return StreamingResponse(streaming, status_code=200)
|
|
|
|
|
2022-02-10 09:25:00 +00:00
|
|
|
app = Starlette(
|
|
|
|
routes=[Route("/", endpoint=homepage)],
|
|
|
|
middleware=[Middleware(GZipMiddleware)],
|
|
|
|
)
|
|
|
|
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2018-10-15 11:08:10 +00:00
|
|
|
response = client.get("/", headers={"accept-encoding": "gzip"})
|
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.text == "x" * 4000
|
|
|
|
assert response.headers["Content-Encoding"] == "gzip"
|
|
|
|
assert "Content-Length" not in response.headers
|
2022-10-12 14:15:29 +00:00
|
|
|
|
|
|
|
|
2024-02-05 10:46:34 +00:00
|
|
|
def test_gzip_ignored_for_responses_with_encoding_set(
|
|
|
|
test_client_factory: TestClientFactory,
|
|
|
|
) -> None:
|
|
|
|
def homepage(request: Request) -> StreamingResponse:
|
|
|
|
async def generator(bytes: bytes, count: int) -> ContentStream:
|
2022-10-12 14:15:29 +00:00
|
|
|
for index in range(count):
|
|
|
|
yield bytes
|
|
|
|
|
|
|
|
streaming = generator(bytes=b"x" * 400, count=10)
|
2024-09-01 13:11:01 +00:00
|
|
|
return StreamingResponse(streaming, status_code=200, headers={"Content-Encoding": "text"})
|
2022-10-12 14:15:29 +00:00
|
|
|
|
|
|
|
app = Starlette(
|
|
|
|
routes=[Route("/", endpoint=homepage)],
|
|
|
|
middleware=[Middleware(GZipMiddleware)],
|
|
|
|
)
|
|
|
|
|
|
|
|
client = test_client_factory(app)
|
2022-11-23 08:02:53 +00:00
|
|
|
response = client.get("/", headers={"accept-encoding": "gzip, text"})
|
2022-10-12 14:15:29 +00:00
|
|
|
assert response.status_code == 200
|
|
|
|
assert response.text == "x" * 4000
|
2022-11-23 08:02:53 +00:00
|
|
|
assert response.headers["Content-Encoding"] == "text"
|
2022-10-12 14:15:29 +00:00
|
|
|
assert "Content-Length" not in response.headers
|