1
0
mirror of https://github.com/home-assistant/core.git synced 2026-07-13 01:27:57 +01:00
Files
core/tests/helpers/test_http.py
T
2026-07-03 23:02:32 +02:00

41 lines
1.2 KiB
Python

"""Tests for the HTTP helpers."""
from http import HTTPStatus
from aiohttp import web
import pytest
from homeassistant.helpers.http import MIN_COMPRESSED_RESPONSE_SIZE, HomeAssistantView
from tests.typing import ClientSessionGenerator
@pytest.mark.parametrize(
("body_size", "expect_compression"),
[
pytest.param(8, False, id="small-body-not-compressed"),
pytest.param(
MIN_COMPRESSED_RESPONSE_SIZE * 2, True, id="large-body-compressed"
),
],
)
@pytest.mark.usefixtures("socket_enabled")
async def test_json_response_compression_threshold(
aiohttp_client: ClientSessionGenerator,
body_size: int,
expect_compression: bool,
) -> None:
"""Test HomeAssistantView.json only compresses bodies above the threshold."""
async def handler(request: web.Request) -> web.Response:
return HomeAssistantView.json({"data": "x" * body_size})
app = web.Application()
app.router.add_get("/", handler)
client = await aiohttp_client(app)
resp = await client.get("/", headers={"Accept-Encoding": "gzip, deflate"})
assert resp.status == HTTPStatus.OK
assert ("Content-Encoding" in resp.headers) is expect_compression