Make aiohttp mockers aware of the json loads kwarg (#73939)
This commit is contained in:
parent
d1708861db
commit
2f78faa718
2 changed files with 9 additions and 7 deletions
|
@ -3,13 +3,15 @@ from __future__ import annotations
|
||||||
|
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
import io
|
import io
|
||||||
import json
|
|
||||||
from typing import Any
|
from typing import Any
|
||||||
from urllib.parse import parse_qsl
|
from urllib.parse import parse_qsl
|
||||||
|
|
||||||
from aiohttp import payload, web
|
from aiohttp import payload, web
|
||||||
|
from aiohttp.typedefs import JSONDecoder
|
||||||
from multidict import CIMultiDict, MultiDict
|
from multidict import CIMultiDict, MultiDict
|
||||||
|
|
||||||
|
from homeassistant.helpers.json import json_loads
|
||||||
|
|
||||||
|
|
||||||
class MockStreamReader:
|
class MockStreamReader:
|
||||||
"""Small mock to imitate stream reader."""
|
"""Small mock to imitate stream reader."""
|
||||||
|
@ -64,9 +66,9 @@ class MockRequest:
|
||||||
"""Return the body as text."""
|
"""Return the body as text."""
|
||||||
return MockStreamReader(self._content)
|
return MockStreamReader(self._content)
|
||||||
|
|
||||||
async def json(self) -> Any:
|
async def json(self, loads: JSONDecoder = json_loads) -> Any:
|
||||||
"""Return the body as JSON."""
|
"""Return the body as JSON."""
|
||||||
return json.loads(self._text)
|
return loads(self._text)
|
||||||
|
|
||||||
async def post(self) -> MultiDict[str]:
|
async def post(self) -> MultiDict[str]:
|
||||||
"""Return POST parameters."""
|
"""Return POST parameters."""
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
import json as _json
|
|
||||||
import re
|
import re
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
from urllib.parse import parse_qs
|
from urllib.parse import parse_qs
|
||||||
|
@ -14,6 +13,7 @@ from multidict import CIMultiDict
|
||||||
from yarl import URL
|
from yarl import URL
|
||||||
|
|
||||||
from homeassistant.const import EVENT_HOMEASSISTANT_CLOSE
|
from homeassistant.const import EVENT_HOMEASSISTANT_CLOSE
|
||||||
|
from homeassistant.helpers.json import json_dumps, json_loads
|
||||||
|
|
||||||
RETYPE = type(re.compile(""))
|
RETYPE = type(re.compile(""))
|
||||||
|
|
||||||
|
@ -169,7 +169,7 @@ class AiohttpClientMockResponse:
|
||||||
):
|
):
|
||||||
"""Initialize a fake response."""
|
"""Initialize a fake response."""
|
||||||
if json is not None:
|
if json is not None:
|
||||||
text = _json.dumps(json)
|
text = json_dumps(json)
|
||||||
if text is not None:
|
if text is not None:
|
||||||
response = text.encode("utf-8")
|
response = text.encode("utf-8")
|
||||||
if response is None:
|
if response is None:
|
||||||
|
@ -252,9 +252,9 @@ class AiohttpClientMockResponse:
|
||||||
"""Return mock response as a string."""
|
"""Return mock response as a string."""
|
||||||
return self.response.decode(encoding, errors=errors)
|
return self.response.decode(encoding, errors=errors)
|
||||||
|
|
||||||
async def json(self, encoding="utf-8", content_type=None):
|
async def json(self, encoding="utf-8", content_type=None, loads=json_loads):
|
||||||
"""Return mock response as a json."""
|
"""Return mock response as a json."""
|
||||||
return _json.loads(self.response.decode(encoding))
|
return loads(self.response.decode(encoding))
|
||||||
|
|
||||||
def release(self):
|
def release(self):
|
||||||
"""Mock release."""
|
"""Mock release."""
|
||||||
|
|
Loading…
Add table
Reference in a new issue