1
mirror of https://github.com/home-assistant/core synced 2024-09-25 00:41:32 +02:00

Fix cors on the index view (#24283)

This commit is contained in:
Paulus Schoutsen 2019-06-03 11:43:13 -07:00 committed by GitHub
parent 4c88578371
commit 6f903db8c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -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:

View File

@ -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