mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Add body_exists to MockRequest in aiohttp util (#100932)
* Add body_exists to MockRequest in aiohttp util * Add body_exists to MockRequest in aiohttp util * Add body_exists to MockRequest in aiohttp util
This commit is contained in:
parent
785b46af22
commit
59207be5f8
@ -66,6 +66,11 @@ class MockRequest:
|
|||||||
"""Return the body as text."""
|
"""Return the body as text."""
|
||||||
return MockStreamReader(self._content)
|
return MockStreamReader(self._content)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def body_exists(self) -> bool:
|
||||||
|
"""Return True if request has HTTP BODY, False otherwise."""
|
||||||
|
return bool(self._text)
|
||||||
|
|
||||||
async def json(self, loads: JSONDecoder = json_loads) -> Any:
|
async def json(self, loads: JSONDecoder = json_loads) -> Any:
|
||||||
"""Return the body as JSON."""
|
"""Return the body as JSON."""
|
||||||
return loads(self._text)
|
return loads(self._text)
|
||||||
|
@ -12,12 +12,19 @@ async def test_request_json() -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def test_request_text() -> None:
|
async def test_request_text() -> None:
|
||||||
"""Test a JSON request."""
|
"""Test bytes in request."""
|
||||||
request = aiohttp.MockRequest(b"hello", status=201, mock_source="test")
|
request = aiohttp.MockRequest(b"hello", status=201, mock_source="test")
|
||||||
|
assert request.body_exists
|
||||||
assert request.status == 201
|
assert request.status == 201
|
||||||
assert await request.text() == "hello"
|
assert await request.text() == "hello"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_request_body_exists() -> None:
|
||||||
|
"""Test body exists."""
|
||||||
|
request = aiohttp.MockRequest(b"", mock_source="test")
|
||||||
|
assert not request.body_exists
|
||||||
|
|
||||||
|
|
||||||
async def test_request_post_query() -> None:
|
async def test_request_post_query() -> None:
|
||||||
"""Test a JSON request."""
|
"""Test a JSON request."""
|
||||||
request = aiohttp.MockRequest(
|
request = aiohttp.MockRequest(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user