mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Patch aiohttp server app router freeze in tests (#105555)
* Add test for registering a http view late * Patch aiohttp server app router freeze * Correct language
This commit is contained in:
parent
fb615817b4
commit
2631fde0f7
@ -487,6 +487,8 @@ def aiohttp_client(
|
|||||||
if isinstance(__param, Application):
|
if isinstance(__param, Application):
|
||||||
server_kwargs = server_kwargs or {}
|
server_kwargs = server_kwargs or {}
|
||||||
server = TestServer(__param, loop=loop, **server_kwargs)
|
server = TestServer(__param, loop=loop, **server_kwargs)
|
||||||
|
# Registering a view after starting the server should still work.
|
||||||
|
server.app._router.freeze = lambda: None
|
||||||
client = CoalescingClient(server, loop=loop, **kwargs)
|
client = CoalescingClient(server, loop=loop, **kwargs)
|
||||||
elif isinstance(__param, BaseTestServer):
|
elif isinstance(__param, BaseTestServer):
|
||||||
client = TestClient(__param, loop=loop, **kwargs)
|
client = TestClient(__param, loop=loop, **kwargs)
|
||||||
|
@ -1,10 +1,16 @@
|
|||||||
"""Test test fixture configuration."""
|
"""Test test fixture configuration."""
|
||||||
|
from http import HTTPStatus
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
|
from aiohttp import web
|
||||||
import pytest
|
import pytest
|
||||||
import pytest_socket
|
import pytest_socket
|
||||||
|
|
||||||
|
from homeassistant.components.http import HomeAssistantView
|
||||||
from homeassistant.core import HomeAssistant, async_get_hass
|
from homeassistant.core import HomeAssistant, async_get_hass
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
from .typing import ClientSessionGenerator
|
||||||
|
|
||||||
|
|
||||||
def test_sockets_disabled() -> None:
|
def test_sockets_disabled() -> None:
|
||||||
@ -27,3 +33,38 @@ async def test_hass_cv(hass: HomeAssistant) -> None:
|
|||||||
in the fixture and that async_get_hass() works correctly.
|
in the fixture and that async_get_hass() works correctly.
|
||||||
"""
|
"""
|
||||||
assert async_get_hass() is hass
|
assert async_get_hass() is hass
|
||||||
|
|
||||||
|
|
||||||
|
def register_view(hass: HomeAssistant) -> None:
|
||||||
|
"""Register a view."""
|
||||||
|
|
||||||
|
class TestView(HomeAssistantView):
|
||||||
|
"""Test view to serve the test."""
|
||||||
|
|
||||||
|
requires_auth = False
|
||||||
|
url = "/api/test"
|
||||||
|
name = "api:test"
|
||||||
|
|
||||||
|
async def get(self, request: web.Request) -> web.Response:
|
||||||
|
"""Return a test result."""
|
||||||
|
return self.json({"test": True})
|
||||||
|
|
||||||
|
hass.http.register_view(TestView())
|
||||||
|
|
||||||
|
|
||||||
|
async def test_aiohttp_client_frozen_router_view(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
hass_client: ClientSessionGenerator,
|
||||||
|
) -> None:
|
||||||
|
"""Test aiohttp_client fixture patches frozen router for views."""
|
||||||
|
assert await async_setup_component(hass, "http", {})
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
# Registering the view after starting the server should still work.
|
||||||
|
client = await hass_client()
|
||||||
|
register_view(hass)
|
||||||
|
|
||||||
|
response = await client.get("/api/test")
|
||||||
|
assert response.status == HTTPStatus.OK
|
||||||
|
result = await response.json()
|
||||||
|
assert result["test"] is True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user