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