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: serialize TestClient rpc output to mock the real message #1452

Merged
merged 1 commit into from
May 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion faststream/broker/core/usecase.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ async def publish(
**kwargs: Any,
) -> Optional[Any]:
"""Publish message directly."""
assert producer, NOT_CONNECTED_YET # nosec B101)
assert producer, NOT_CONNECTED_YET # nosec B101

publish: "AsyncFunc" = producer.publish
for m in self._middlewares:
Expand Down
6 changes: 5 additions & 1 deletion faststream/testing/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from unittest.mock import AsyncMock, MagicMock

from faststream.broker.core.usecase import BrokerUsecase
from faststream.broker.message import StreamMessage, decode_message, encode_message
from faststream.broker.middlewares.logging import CriticalLogMiddleware
from faststream.broker.wrapper.call import HandlerCallWrapper
from faststream.testing.app import TestApp
Expand Down Expand Up @@ -212,6 +213,9 @@ async def call_handler(
result = await handler.consume(message)

if rpc:
return result
message_body, content_type = encode_message(result)
msg_to_publish = StreamMessage(raw_message=None, body=message_body, content_type=content_type)
consumed_data = decode_message(msg_to_publish)
return consumed_data

return None