Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test_utils.make_mocked_request behaviour for empty payload #7168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/7167.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix make_mocked_request behavior for empty payload
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ Philipp A.
Pieter van Beek
Qiao Han
Rafael Viotti
Rahul Nahata
Raphael Bialon
Raúl Cumplido
Required Field
Expand Down
3 changes: 2 additions & 1 deletion aiohttp/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .client_ws import ClientWebSocketResponse
from .helpers import _SENTINEL, PY_38, sentinel
from .http import HttpVersion, RawRequestMessage
from .streams import EMPTY_PAYLOAD
from .web import (
Application,
AppRunner,
Expand Down Expand Up @@ -632,7 +633,7 @@ def make_mocked_request(
protocol.writer = writer

if payload is sentinel:
payload = mock.Mock()
payload = EMPTY_PAYLOAD
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels to me like everything here should be a mock. Maybe instead we should autospec it? That should ensure it gets an appropriate mock with the correct methods.

Suggested change
payload = EMPTY_PAYLOAD
payload = create_autospec(StreamResponse, spec_set=True, instance=True)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same should really be done to all the rest of this code... Would make the code safer and more maintainable.


req = Request(
message, payload, protocol, writer, task, loop, client_max_size=client_max_size
Expand Down
6 changes: 6 additions & 0 deletions tests/test_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import aiohttp
from aiohttp import web
from aiohttp.streams import EmptyStreamReader
from aiohttp.test_utils import (
AioHTTPTestCase,
RawTestServer as _RawTestServer,
Expand Down Expand Up @@ -214,6 +215,11 @@ def test_make_mocked_request_content() -> None:
assert req.content is payload


def test_make_mocked_request_empty_payload() -> None:
req = make_mocked_request("GET", "/")
assert isinstance(req.content, EmptyStreamReader)


def test_make_mocked_request_transport() -> None:
transport = mock.Mock()
req = make_mocked_request("GET", "/", transport=transport)
Expand Down