Aiohttp client unittest (#5635)

* add test for cleanup

* add test for mjpeg stream
This commit is contained in:
Pascal Vizeli 2017-01-30 01:15:40 +01:00 committed by Paulus Schoutsen
parent 261ffbbfea
commit 847a5a064d
2 changed files with 104 additions and 4 deletions

View file

@ -1,10 +1,13 @@
"""Test the aiohttp client helper."""
import asyncio
import unittest
import aiohttp
from homeassistant.bootstrap import setup_component
import homeassistant.helpers.aiohttp_client as client
from homeassistant.util.async import run_callback_threadsafe
from homeassistant.util.async import (
run_callback_threadsafe, run_coroutine_threadsafe)
from tests.common import get_test_home_assistant
@ -79,3 +82,81 @@ class TestHelpersAiohttpClient(unittest.TestCase):
assert isinstance(
self.hass.data[client.DATA_CONNECTOR_NOTVERIFY],
aiohttp.TCPConnector)
def test_get_clientsession_cleanup(self):
"""Test init clientsession with ssl."""
run_callback_threadsafe(self.hass.loop, client.async_get_clientsession,
self.hass).result()
assert isinstance(
self.hass.data[client.DATA_CLIENTSESSION], aiohttp.ClientSession)
assert isinstance(
self.hass.data[client.DATA_CONNECTOR], aiohttp.TCPConnector)
run_coroutine_threadsafe(
client.async_cleanup_websession(self.hass), self.hass.loop
).result()
assert self.hass.data[client.DATA_CLIENTSESSION].closed
assert self.hass.data[client.DATA_CONNECTOR].closed
@asyncio.coroutine
def test_fetching_url(aioclient_mock, hass, test_client):
"""Test that it fetches the given url."""
aioclient_mock.get('http://example.com/mjpeg_stream', content=[
b'Frame1', b'Frame2', b'Frame3'
])
def setup_platform():
"""Setup the platform."""
assert setup_component(hass, 'camera', {
'camera': {
'name': 'config_test',
'platform': 'mjpeg',
'mjpeg_url': 'http://example.com/mjpeg_stream',
}})
yield from hass.loop.run_in_executor(None, setup_platform)
client = yield from test_client(hass.http.app)
resp = yield from client.get('/api/camera_proxy_stream/camera.config_test')
assert resp.status == 200
assert aioclient_mock.call_count == 1
body = yield from resp.text()
assert body == 'Frame3Frame2Frame1'
aioclient_mock.clear_requests()
aioclient_mock.get(
'http://example.com/mjpeg_stream', exc=asyncio.TimeoutError(),
content=[b'Frame1', b'Frame2', b'Frame3'])
resp = yield from client.get('/api/camera_proxy_stream/camera.config_test')
assert resp.status == 200
body = yield from resp.text()
assert body == ''
aioclient_mock.clear_requests()
aioclient_mock.get(
'http://example.com/mjpeg_stream', exc=asyncio.CancelledError(),
content=[b'Frame1', b'Frame2', b'Frame3'])
resp = yield from client.get('/api/camera_proxy_stream/camera.config_test')
assert resp.status == 200
body = yield from resp.text()
assert body == ''
aioclient_mock.clear_requests()
aioclient_mock.get(
'http://example.com/mjpeg_stream', exc=aiohttp.errors.ClientError(),
content=[b'Frame1', b'Frame2', b'Frame3'])
resp = yield from client.get('/api/camera_proxy_stream/camera.config_test')
assert resp.status == 200
body = yield from resp.text()
assert body == ''