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:
Joost Lekkerkerker 2023-09-26 17:52:29 +02:00 committed by GitHub
parent 785b46af22
commit 59207be5f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View file

@ -66,6 +66,11 @@ class MockRequest:
"""Return the body as text."""
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:
"""Return the body as JSON."""
return loads(self._text)

View file

@ -12,12 +12,19 @@ async def test_request_json() -> 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")
assert request.body_exists
assert request.status == 201
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:
"""Test a JSON request."""
request = aiohttp.MockRequest(