Fix cloud webhook body (#20739)
* Bugfix cloud webhooks text response * address comments * Fix lint
This commit is contained in:
parent
c812176e94
commit
29b64d56be
5 changed files with 40 additions and 35 deletions
|
@ -12,9 +12,10 @@ from homeassistant.components.alexa import smart_home as alexa
|
|||
from homeassistant.components.google_assistant import smart_home as ga
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.util.decorator import Registry
|
||||
from homeassistant.util.aiohttp import MockRequest, serialize_response
|
||||
from homeassistant.util.aiohttp import MockRequest
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from . import auth_api
|
||||
from . import utils
|
||||
from .const import MESSAGE_EXPIRATION, MESSAGE_AUTH_FAIL
|
||||
|
||||
HANDLERS = Registry()
|
||||
|
@ -360,10 +361,8 @@ async def async_handle_webhook(hass, cloud, payload):
|
|||
response = await hass.components.webhook.async_handle_webhook(
|
||||
found['webhook_id'], request)
|
||||
|
||||
response_dict = serialize_response(response)
|
||||
response_dict = utils.aiohttp_serialize_response(response)
|
||||
body = response_dict.get('body')
|
||||
if body:
|
||||
body = body.decode('utf-8')
|
||||
|
||||
return {
|
||||
'body': body,
|
||||
|
|
13
homeassistant/components/cloud/utils.py
Normal file
13
homeassistant/components/cloud/utils.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
"""Helper functions for cloud components."""
|
||||
from typing import Any, Dict
|
||||
|
||||
from aiohttp import web
|
||||
|
||||
|
||||
def aiohttp_serialize_response(response: web.Response) -> Dict[str, Any]:
|
||||
"""Serialize an aiohttp response to a dictionary."""
|
||||
return {
|
||||
'status': response.status,
|
||||
'body': response.text,
|
||||
'headers': dict(response.headers),
|
||||
}
|
|
@ -3,7 +3,6 @@ import json
|
|||
from urllib.parse import parse_qsl
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from aiohttp import web
|
||||
from multidict import CIMultiDict, MultiDict
|
||||
|
||||
|
||||
|
@ -42,12 +41,3 @@ class MockRequest:
|
|||
async def text(self) -> str:
|
||||
"""Return the body as text."""
|
||||
return self._text
|
||||
|
||||
|
||||
def serialize_response(response: web.Response) -> Dict[str, Any]:
|
||||
"""Serialize an aiohttp response to a dictionary."""
|
||||
return {
|
||||
'status': response.status,
|
||||
'body': response.body,
|
||||
'headers': dict(response.headers),
|
||||
}
|
||||
|
|
24
tests/components/cloud/test_utils.py
Normal file
24
tests/components/cloud/test_utils.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
"""Test aiohttp request helper."""
|
||||
from aiohttp import web
|
||||
|
||||
from homeassistant.components.cloud import utils
|
||||
|
||||
|
||||
def test_serialize_text():
|
||||
"""Test serializing a text response."""
|
||||
response = web.Response(status=201, text='Hello')
|
||||
assert utils.aiohttp_serialize_response(response) == {
|
||||
'status': 201,
|
||||
'body': 'Hello',
|
||||
'headers': {'Content-Type': 'text/plain; charset=utf-8'},
|
||||
}
|
||||
|
||||
|
||||
def test_serialize_json():
|
||||
"""Test serializing a JSON response."""
|
||||
response = web.json_response({"how": "what"})
|
||||
assert utils.aiohttp_serialize_response(response) == {
|
||||
'status': 200,
|
||||
'body': '{"how": "what"}',
|
||||
'headers': {'Content-Type': 'application/json; charset=utf-8'},
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
"""Test aiohttp request helper."""
|
||||
from aiohttp import web
|
||||
|
||||
from homeassistant.util import aiohttp
|
||||
|
||||
|
@ -32,23 +31,3 @@ async def test_request_post_query():
|
|||
assert request.query == {
|
||||
'get': 'true'
|
||||
}
|
||||
|
||||
|
||||
def test_serialize_text():
|
||||
"""Test serializing a text response."""
|
||||
response = web.Response(status=201, text='Hello')
|
||||
assert aiohttp.serialize_response(response) == {
|
||||
'status': 201,
|
||||
'body': b'Hello',
|
||||
'headers': {'Content-Type': 'text/plain; charset=utf-8'},
|
||||
}
|
||||
|
||||
|
||||
def test_serialize_json():
|
||||
"""Test serializing a JSON response."""
|
||||
response = web.json_response({"how": "what"})
|
||||
assert aiohttp.serialize_response(response) == {
|
||||
'status': 200,
|
||||
'body': b'{"how": "what"}',
|
||||
'headers': {'Content-Type': 'application/json; charset=utf-8'},
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue