mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 06:07:17 +00:00
Use new session when fetching remote urls (#16093)
This commit is contained in:
parent
7bb5344942
commit
b26506ad4a
@ -4,6 +4,7 @@ from html.parser import HTMLParser
|
|||||||
from ipaddress import ip_address, ip_network
|
from ipaddress import ip_address, ip_network
|
||||||
from urllib.parse import urlparse, urljoin
|
from urllib.parse import urlparse, urljoin
|
||||||
|
|
||||||
|
import aiohttp
|
||||||
from aiohttp.client_exceptions import ClientError
|
from aiohttp.client_exceptions import ClientError
|
||||||
|
|
||||||
# IP addresses of loopback interfaces
|
# IP addresses of loopback interfaces
|
||||||
@ -76,12 +77,11 @@ async def fetch_redirect_uris(hass, url):
|
|||||||
|
|
||||||
We do not implement extracting redirect uris from headers.
|
We do not implement extracting redirect uris from headers.
|
||||||
"""
|
"""
|
||||||
session = hass.helpers.aiohttp_client.async_get_clientsession()
|
|
||||||
parser = LinkTagParser('redirect_uri')
|
parser = LinkTagParser('redirect_uri')
|
||||||
chunks = 0
|
chunks = 0
|
||||||
try:
|
try:
|
||||||
resp = await session.get(url, timeout=5)
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(url, timeout=5) as resp:
|
||||||
async for data in resp.content.iter_chunked(1024):
|
async for data in resp.content.iter_chunked(1024):
|
||||||
parser.feed(data.decode())
|
parser.feed(data.decode())
|
||||||
chunks += 1
|
chunks += 1
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
"""Tests for the client validator."""
|
"""Tests for the client validator."""
|
||||||
|
import asyncio
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@ -6,6 +7,18 @@ import pytest
|
|||||||
from homeassistant.components.auth import indieauth
|
from homeassistant.components.auth import indieauth
|
||||||
|
|
||||||
from tests.common import mock_coro
|
from tests.common import mock_coro
|
||||||
|
from tests.test_util.aiohttp import AiohttpClientMocker
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_session():
|
||||||
|
"""Mock aiohttp.ClientSession."""
|
||||||
|
mocker = AiohttpClientMocker()
|
||||||
|
|
||||||
|
with patch('aiohttp.ClientSession',
|
||||||
|
side_effect=lambda *args, **kwargs:
|
||||||
|
mocker.create_session(asyncio.get_event_loop())):
|
||||||
|
yield mocker
|
||||||
|
|
||||||
|
|
||||||
def test_client_id_scheme():
|
def test_client_id_scheme():
|
||||||
@ -120,9 +133,9 @@ async def test_verify_redirect_uri():
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_find_link_tag(hass, aioclient_mock):
|
async def test_find_link_tag(hass, mock_session):
|
||||||
"""Test finding link tag."""
|
"""Test finding link tag."""
|
||||||
aioclient_mock.get("http://127.0.0.1:8000", text="""
|
mock_session.get("http://127.0.0.1:8000", text="""
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
@ -142,11 +155,15 @@ async def test_find_link_tag(hass, aioclient_mock):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
async def test_find_link_tag_max_size(hass, aioclient_mock):
|
async def test_find_link_tag_max_size(hass, mock_session):
|
||||||
"""Test finding link tag."""
|
"""Test finding link tag."""
|
||||||
text = ("0" * 1024 * 10) + '<link rel="redirect_uri" href="/beer">'
|
text = ''.join([
|
||||||
aioclient_mock.get("http://127.0.0.1:8000", text=text)
|
'<link rel="redirect_uri" href="/wine">',
|
||||||
|
("0" * 1024 * 10),
|
||||||
|
'<link rel="redirect_uri" href="/beer">',
|
||||||
|
])
|
||||||
|
mock_session.get("http://127.0.0.1:8000", text=text)
|
||||||
redirect_uris = await indieauth.fetch_redirect_uris(
|
redirect_uris = await indieauth.fetch_redirect_uris(
|
||||||
hass, "http://127.0.0.1:8000")
|
hass, "http://127.0.0.1:8000")
|
||||||
|
|
||||||
assert redirect_uris == []
|
assert redirect_uris == ["http://127.0.0.1:8000/wine"]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user