diff --git a/homeassistant/components/http/cors.py b/homeassistant/components/http/cors.py index 1ef70b5e022..419b62be2c6 100644 --- a/homeassistant/components/http/cors.py +++ b/homeassistant/components/http/cors.py @@ -1,4 +1,5 @@ """Provide CORS support for the HTTP component.""" +from aiohttp.web_urldispatcher import Resource, ResourceRoute from aiohttp.hdrs import ACCEPT, CONTENT_TYPE, ORIGIN, AUTHORIZATION from homeassistant.const import ( @@ -8,6 +9,7 @@ from homeassistant.core import callback ALLOWED_CORS_HEADERS = [ ORIGIN, ACCEPT, HTTP_HEADER_X_REQUESTED_WITH, CONTENT_TYPE, HTTP_HEADER_HA_AUTH, AUTHORIZATION] +VALID_CORS_TYPES = (Resource, ResourceRoute) @callback @@ -31,6 +33,9 @@ def setup_cors(app, origins): else: path = route + if not isinstance(path, VALID_CORS_TYPES): + return + path = path.canonical if path in cors_added: diff --git a/tests/components/http/test_cors.py b/tests/components/http/test_cors.py index e17fb105efe..d9fa6c11309 100644 --- a/tests/components/http/test_cors.py +++ b/tests/components/http/test_cors.py @@ -140,3 +140,15 @@ async def test_cors_middleware_with_cors_allowed_view(hass): hass.http.app._on_startup.freeze() await hass.http.app.startup() + + +async def test_cors_works_with_frontend(hass, hass_client): + """Test CORS works with the frontend.""" + assert await async_setup_component(hass, 'frontend', { + 'http': { + 'cors_allowed_origins': ['http://home-assistant.io'] + } + }) + client = await hass_client() + resp = await client.get('/') + assert resp.status == 200