Use async with to fetch HTTP streams in tests (#82788)

This commit is contained in:
uvjustin 2022-11-28 03:35:03 +08:00 committed by GitHub
parent 444ad52757
commit 31ad208500
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 21 deletions

View file

@ -327,35 +327,35 @@ async def test_stream(hass, mock_api_client):
"""Test the stream."""
listen_count = _listen_count(hass)
resp = await mock_api_client.get(const.URL_API_STREAM)
assert resp.status == HTTPStatus.OK
assert listen_count + 1 == _listen_count(hass)
async with mock_api_client.get(const.URL_API_STREAM) as resp:
assert resp.status == HTTPStatus.OK
assert listen_count + 1 == _listen_count(hass)
hass.bus.async_fire("test_event")
hass.bus.async_fire("test_event")
data = await _stream_next_event(resp.content)
data = await _stream_next_event(resp.content)
assert data["event_type"] == "test_event"
assert data["event_type"] == "test_event"
async def test_stream_with_restricted(hass, mock_api_client):
"""Test the stream with restrictions."""
listen_count = _listen_count(hass)
resp = await mock_api_client.get(
async with mock_api_client.get(
f"{const.URL_API_STREAM}?restrict=test_event1,test_event3"
)
assert resp.status == HTTPStatus.OK
assert listen_count + 1 == _listen_count(hass)
) as resp:
assert resp.status == HTTPStatus.OK
assert listen_count + 1 == _listen_count(hass)
hass.bus.async_fire("test_event1")
data = await _stream_next_event(resp.content)
assert data["event_type"] == "test_event1"
hass.bus.async_fire("test_event1")
data = await _stream_next_event(resp.content)
assert data["event_type"] == "test_event1"
hass.bus.async_fire("test_event2")
hass.bus.async_fire("test_event3")
data = await _stream_next_event(resp.content)
assert data["event_type"] == "test_event3"
hass.bus.async_fire("test_event2")
hass.bus.async_fire("test_event3")
data = await _stream_next_event(resp.content)
assert data["event_type"] == "test_event3"
async def _stream_next_event(stream):