HTTP: Fix registering views after start (#4604)

This commit is contained in:
Paulus Schoutsen 2016-11-27 14:01:12 -08:00 committed by GitHub
parent ff4cb23f2a
commit 0d734303a4
2 changed files with 51 additions and 0 deletions

View File

@ -289,10 +289,21 @@ class HomeAssistantWSGI(object):
else:
context = None
# Aiohttp freezes apps after start so that no changes can be made.
# However in Home Assistant components can be discovered after boot.
# This will now raise a RunTimeError.
# To work around this we now fake that we are frozen.
# A more appropriate fix would be to create a new app and
# re-register all redirects, views, static paths.
self.app._frozen = True # pylint: disable=protected-access
self._handler = self.app.make_handler()
self.server = yield from self.hass.loop.create_server(
self._handler, self.server_host, self.server_port, ssl=context)
self.app._frozen = False # pylint: disable=protected-access
@asyncio.coroutine
def stop(self):
"""Stop the wsgi server."""

View File

@ -1,4 +1,5 @@
"""The tests for the Home Assistant HTTP component."""
import asyncio
import requests
from homeassistant import bootstrap, const
@ -109,3 +110,42 @@ class TestHttp:
assert req.headers.get(allow_origin) == HTTP_BASE_URL
assert req.headers.get(allow_headers) == \
const.HTTP_HEADER_HA_AUTH.upper()
class TestView(http.HomeAssistantView):
name = 'test'
url = '/hello'
@asyncio.coroutine
def get(self, request):
"""Return a get request."""
return 'hello'
@asyncio.coroutine
def test_registering_view_while_running(hass, test_client):
"""Test that we can register a view while the server is running."""
yield from bootstrap.async_setup_component(
hass, http.DOMAIN, {
http.DOMAIN: {
http.CONF_SERVER_PORT: get_test_instance_port(),
}
}
)
yield from bootstrap.async_setup_component(hass, 'api')
yield from hass.async_start()
yield from hass.async_block_till_done()
hass.http.register_view(TestView)
client = yield from test_client(hass.http.app)
resp = yield from client.get('/hello')
assert resp.status == 200
text = yield from resp.text()
assert text == 'hello'