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: when / present in virtual host name and passing as uri #1979

Merged
merged 5 commits into from
Dec 15, 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
1 change: 1 addition & 0 deletions docs/docs/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ search:
- [build_message](api/faststream/rabbit/testing/build_message.md)
- utils
- [build_url](api/faststream/rabbit/utils/build_url.md)
- [build_virtual_host](api/faststream/rabbit/utils/build_virtual_host.md)
- [is_routing_exchange](api/faststream/rabbit/utils/is_routing_exchange.md)
- redis
- [ListSub](api/faststream/redis/ListSub.md)
Expand Down
11 changes: 11 additions & 0 deletions docs/docs/en/api/faststream/rabbit/utils/build_virtual_host.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
# 0.5 - API
# 2 - Release
# 3 - Contributing
# 5 - Template Page
# 10 - Default
search:
boost: 0.5
---

::: faststream.rabbit.utils.build_virtual_host
13 changes: 12 additions & 1 deletion faststream/rabbit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
from faststream.rabbit.schemas import RabbitExchange


def build_virtual_host(
url: Union[str, "URL", None], virtualhost: Optional[str], path: str
) -> str:
if (not url and not virtualhost) or virtualhost == "/":
return ""
elif virtualhost:
return virtualhost.replace("/", "", 1)
else:
return path.replace("/", "", 1)


def build_url(
url: Union[str, "URL", None] = None,
*,
Expand All @@ -36,7 +47,7 @@ def build_url(
port=port or original_url.port or default_port,
login=login or original_url.user or "guest",
password=password or original_url.password or "guest",
virtualhost=virtualhost or original_url.path.lstrip("/"),
virtualhost=build_virtual_host(url, virtualhost, original_url.path),
ssl=use_ssl,
ssl_options=ssl_options,
client_properties=client_properties,
Expand Down
19 changes: 3 additions & 16 deletions tests/brokers/rabbit/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def get_broker_args(self, settings):
return {"url": settings.url}

@pytest.mark.asyncio
async def test_init_connect_by_raw_data(self, settings):
async def test_connect_handover_config_to_init(self, settings):
broker = self.broker(
host=settings.host,
port=settings.port,
Expand All @@ -28,7 +28,7 @@ async def test_init_connect_by_raw_data(self, settings):
await broker.close()

@pytest.mark.asyncio
async def test_connection_by_params(self, settings):
async def test_connect_handover_config_to_connect(self, settings):
broker = self.broker()
assert await broker.connect(
host=settings.host,
Expand All @@ -41,20 +41,7 @@ async def test_connection_by_params(self, settings):
await broker.close()

@pytest.mark.asyncio
async def test_connect_merge_kwargs_with_priority(self, settings):
broker = self.broker(host="fake-host", port=5677) # kwargs will be ignored
assert await broker.connect(
host=settings.host,
port=settings.port,
security=SASLPlaintext(
username=settings.login,
password=settings.password,
),
)
await broker.close()

@pytest.mark.asyncio
async def test_connect_merge_args_and_kwargs_native(self, settings):
async def test_connect_handover_config_to_connect_override_init(self, settings):
broker = self.broker("fake-url") # will be ignored
assert await broker.connect(url=settings.url)
await broker.close()
38 changes: 38 additions & 0 deletions tests/brokers/rabbit/test_url_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from typing import Any, Dict

import pytest
from yarl import URL

from faststream.rabbit.utils import build_url


@pytest.mark.parametrize(
("url_kwargs", "expected_url"),
[
pytest.param(
{},
URL("amqp://guest:guest@localhost:5672/"), # pragma: allowlist secret
id="blank params use defaults",
),
pytest.param(
{"ssl": True},
URL("amqps://guest:guest@localhost:5672/"), # pragma: allowlist secret
id="ssl affects protocol",
),
pytest.param(
{"url": "fake", "virtualhost": "/", "host": "host"},
URL("amqp://guest:guest@host:5672/"), # pragma: allowlist secret
id="kwargs overrides url",
),
pytest.param(
{"virtualhost": "//test"}, # pragma: allowlist secret
URL(
"amqp://guest:guest@localhost:5672//test" # pragma: allowlist secret
),
id="exotic virtualhost",
),
],
)
def test_unpack_args(url_kwargs: Dict[str, Any], expected_url: URL) -> None:
url = build_url(**url_kwargs)
assert url == expected_url
Loading