Skip to content

Commit

Permalink
Merge pull request #23137 from home-assistant/rc
Browse files Browse the repository at this point in the history
0.91.4
  • Loading branch information
pvizeli authored Apr 16, 2019
2 parents 536356c + c90219a commit 6bb95f6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 11 deletions.
6 changes: 4 additions & 2 deletions homeassistant/components/hassio/ingress.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ async def _handle(
post = _handle
put = _handle
delete = _handle
patch = _handle
options = _handle

async def _handle_websocket(
self, request: web.Request, token: str, path: str
Expand Down Expand Up @@ -209,8 +211,8 @@ def _is_websocket(request: web.Request) -> bool:
"""Return True if request is a websocket."""
headers = request.headers

if headers.get(hdrs.CONNECTION) == "Upgrade" and \
headers.get(hdrs.UPGRADE) == "websocket":
if "upgrade" in headers.get(hdrs.CONNECTION, "").lower() and \
headers.get(hdrs.UPGRADE, "").lower() == "websocket":
return True
return False

Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/http/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def register(self, app, router):
urls = [self.url] + self.extra_urls
routes = []

for method in ('get', 'post', 'delete', 'put'):
for method in ('get', 'post', 'delete', 'put', 'patch', 'head',
'options'):
handler = getattr(self, method, None)

if not handler:
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""Constants used by Home Assistant components."""
MAJOR_VERSION = 0
MINOR_VERSION = 91
PATCH_VERSION = '3'
PATCH_VERSION = '4'
__short_version__ = '{}.{}'.format(MAJOR_VERSION, MINOR_VERSION)
__version__ = '{}.{}'.format(__short_version__, PATCH_VERSION)
REQUIRED_PYTHON_VER = (3, 5, 3)
Expand Down
76 changes: 70 additions & 6 deletions tests/components/hassio/test_ingress.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""The tests for the hassio component."""

from aiohttp.hdrs import X_FORWARDED_FOR, X_FORWARDED_HOST, X_FORWARDED_PROTO
from aiohttp.client_exceptions import WSServerHandshakeError
import pytest


Expand Down Expand Up @@ -137,6 +136,72 @@ async def test_ingress_request_delete(
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_PROTO]


@pytest.mark.parametrize(
'build_type', [
("a3_vl", "test/beer/ping?index=1"), ("core", "index.html"),
("local", "panel/config"), ("jk_921", "editor.php?idx=3&ping=5"),
("fsadjf10312", "")
])
async def test_ingress_request_patch(
hassio_client, build_type, aioclient_mock):
"""Test no auth needed for ."""
aioclient_mock.patch("http://127.0.0.1/ingress/{}/{}".format(
build_type[0], build_type[1]), text="test")

resp = await hassio_client.patch(
'/api/hassio_ingress/{}/{}'.format(build_type[0], build_type[1]),
headers={"X-Test-Header": "beer"}
)

# Check we got right response
assert resp.status == 200
body = await resp.text()
assert body == "test"

# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456"
assert aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == \
"/api/hassio_ingress/{}".format(build_type[0])
assert aioclient_mock.mock_calls[-1][3]["X-Test-Header"] == "beer"
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_FOR]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_HOST]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_PROTO]


@pytest.mark.parametrize(
'build_type', [
("a3_vl", "test/beer/ping?index=1"), ("core", "index.html"),
("local", "panel/config"), ("jk_921", "editor.php?idx=3&ping=5"),
("fsadjf10312", "")
])
async def test_ingress_request_options(
hassio_client, build_type, aioclient_mock):
"""Test no auth needed for ."""
aioclient_mock.options("http://127.0.0.1/ingress/{}/{}".format(
build_type[0], build_type[1]), text="test")

resp = await hassio_client.options(
'/api/hassio_ingress/{}/{}'.format(build_type[0], build_type[1]),
headers={"X-Test-Header": "beer"}
)

# Check we got right response
assert resp.status == 200
body = await resp.text()
assert body == "test"

# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
assert aioclient_mock.mock_calls[-1][3]["X-Hassio-Key"] == "123456"
assert aioclient_mock.mock_calls[-1][3]["X-Ingress-Path"] == \
"/api/hassio_ingress/{}".format(build_type[0])
assert aioclient_mock.mock_calls[-1][3]["X-Test-Header"] == "beer"
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_FOR]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_HOST]
assert aioclient_mock.mock_calls[-1][3][X_FORWARDED_PROTO]


@pytest.mark.parametrize(
'build_type', [
("a3_vl", "test/beer/ws"), ("core", "ws.php"),
Expand All @@ -150,11 +215,10 @@ async def test_ingress_websocket(
build_type[0], build_type[1]))

# Ignore error because we can setup a full IO infrastructure
with pytest.raises(WSServerHandshakeError):
await hassio_client.ws_connect(
'/api/hassio_ingress/{}/{}'.format(build_type[0], build_type[1]),
headers={"X-Test-Header": "beer"}
)
await hassio_client.ws_connect(
'/api/hassio_ingress/{}/{}'.format(build_type[0], build_type[1]),
headers={"X-Test-Header": "beer"}
)

# Check we forwarded command
assert len(aioclient_mock.mock_calls) == 1
Expand Down
6 changes: 5 additions & 1 deletion tests/test_util/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def options(self, *args, **kwargs):
"""Register a mock options request."""
self.request('options', *args, **kwargs)

def patch(self, *args, **kwargs):
"""Register a mock patch request."""
self.request('patch', *args, **kwargs)

@property
def call_count(self):
"""Return the number of requests made."""
Expand All @@ -102,7 +106,7 @@ def create_session(self, loop):

async def match_request(self, method, url, *, data=None, auth=None,
params=None, headers=None, allow_redirects=None,
timeout=None, json=None, cookies=None):
timeout=None, json=None, cookies=None, **kwargs):
"""Match a request against pre-registered requests."""
data = data or json
url = URL(url)
Expand Down

0 comments on commit 6bb95f6

Please sign in to comment.