From 5b5d9335a8db050408bfb6313b23504ee49c6caf Mon Sep 17 00:00:00 2001 From: "cicciodb@hotmail.it" Date: Fri, 16 May 2025 14:49:47 +0200 Subject: [PATCH 1/5] vhost setting if url.path is not none --- rabbitmq_amqp_python_client/qpid/proton/_reactor.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/rabbitmq_amqp_python_client/qpid/proton/_reactor.py b/rabbitmq_amqp_python_client/qpid/proton/_reactor.py index 89b2e7c..16328e2 100644 --- a/rabbitmq_amqp_python_client/qpid/proton/_reactor.py +++ b/rabbitmq_amqp_python_client/qpid/proton/_reactor.py @@ -23,6 +23,7 @@ import os import queue import re +import urllib from typing import ( TYPE_CHECKING, Any, @@ -1098,8 +1099,12 @@ def __init__(self, connection: Connection) -> None: def _connect(self, connection: Connection, url: Url) -> None: connection.url = url # if virtual-host not set, use host from address as default - if self.virtual_host is None: + if url.path is not None: + rabbitmq_vhost = urllib.parse.quote(url.path.replace("+", "%2B")) + connection.hostname = "vhost:{}".format(rabbitmq_vhost) + else: connection.hostname = url.host + _logger.info("Connecting to %r..." % url) transport = Transport() From f96a18c160784366265dedb31aa3516f091ec413 Mon Sep 17 00:00:00 2001 From: "cicciodb@hotmail.it" Date: Tue, 20 May 2025 10:25:20 +0200 Subject: [PATCH 2/5] vhost setting if url.path is not empty --- rabbitmq_amqp_python_client/qpid/proton/_reactor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rabbitmq_amqp_python_client/qpid/proton/_reactor.py b/rabbitmq_amqp_python_client/qpid/proton/_reactor.py index 16328e2..316c800 100644 --- a/rabbitmq_amqp_python_client/qpid/proton/_reactor.py +++ b/rabbitmq_amqp_python_client/qpid/proton/_reactor.py @@ -1099,7 +1099,7 @@ def __init__(self, connection: Connection) -> None: def _connect(self, connection: Connection, url: Url) -> None: connection.url = url # if virtual-host not set, use host from address as default - if url.path is not None: + if url.path is not None and url.path != '': rabbitmq_vhost = urllib.parse.quote(url.path.replace("+", "%2B")) connection.hostname = "vhost:{}".format(rabbitmq_vhost) else: From bceb0651202681003987546bdea78555e0ac4d0a Mon Sep 17 00:00:00 2001 From: "cicciodb@hotmail.it" Date: Tue, 20 May 2025 10:25:54 +0200 Subject: [PATCH 3/5] vhost test --- tests/http_requests.py | 24 ++++++++++++++++++++++++ tests/test_connection.py | 17 ++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/tests/http_requests.py b/tests/http_requests.py index ed0445c..a4a63af 100644 --- a/tests/http_requests.py +++ b/tests/http_requests.py @@ -15,6 +15,30 @@ def get_connections_names() -> list: return connection_names +# not used +def get_vhosts() -> list: + request = "http://localhost:15672/api/vhosts" + responses = requests.get(request, auth=HTTPBasicAuth("guest", "guest")) + responses.raise_for_status() + vhosts = responses.json() + vhosts_names = [] + for vhost in vhosts: + vhosts_names.append(vhost["name"]) + return vhosts_names + + +def create_vhost(vhost_name: str) -> None: + request = "http://localhost:15672/api/vhosts/{}".format(vhost_name) + responses = requests.put(request, auth=HTTPBasicAuth("guest", "guest")) + responses.raise_for_status() + + +def delete_vhost(vhost_name: str) -> None: + request = "http://localhost:15672/api/vhosts/{}/".format(vhost_name) + responses = requests.delete(request, auth=HTTPBasicAuth("guest", "guest")) + responses.raise_for_status() + + def delete_connections(connection_names: []) -> None: for connection_name in connection_names: request = ( diff --git a/tests/test_connection.py b/tests/test_connection.py index ef262e0..4f7dee6 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -17,6 +17,7 @@ ) from .http_requests import delete_all_connections +from .http_requests import create_vhost, delete_vhost from .utils import token @@ -126,7 +127,7 @@ def test_connection_oauth_refresh_token(environment_auth: Environment) -> None: def test_connection_oauth_refresh_token_with_disconnection( - environment_auth: Environment, + environment_auth: Environment ) -> None: connection = environment_auth.connection() @@ -233,3 +234,17 @@ def test_reconnection_parameters() -> None: exception = True assert exception is True + + +def test_connection_vhost() -> None: + vhost = "tmpVhost" + str(time.time()) + create_vhost(vhost) + uri = "amqp://guest:guest@localhost:5672/{}".format(vhost) + environment = Environment(uri=uri) + connection = environment.connection() + connection.dial() + is_correct_vhost = connection._conn.conn.hostname == 'vhost:{}'.format(vhost) + environment.close() + delete_vhost(vhost) + + assert is_correct_vhost is True \ No newline at end of file From 6abf9a9664ef14c29233bf7eb43f17c78bba80ba Mon Sep 17 00:00:00 2001 From: "cicciodb@hotmail.it" Date: Wed, 21 May 2025 09:23:56 +0200 Subject: [PATCH 4/5] vhost not exists test --- tests/test_connection.py | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/tests/test_connection.py b/tests/test_connection.py index 4f7dee6..451e15e 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -15,9 +15,15 @@ ValidationCodeException, WinSslConfigurationContext, ) +from rabbitmq_amqp_python_client.qpid.proton import ( + ConnectionException, +) -from .http_requests import delete_all_connections -from .http_requests import create_vhost, delete_vhost +from .http_requests import ( + create_vhost, + delete_all_connections, + delete_vhost, +) from .utils import token @@ -127,7 +133,7 @@ def test_connection_oauth_refresh_token(environment_auth: Environment) -> None: def test_connection_oauth_refresh_token_with_disconnection( - environment_auth: Environment + environment_auth: Environment, ) -> None: connection = environment_auth.connection() @@ -243,8 +249,25 @@ def test_connection_vhost() -> None: environment = Environment(uri=uri) connection = environment.connection() connection.dial() - is_correct_vhost = connection._conn.conn.hostname == 'vhost:{}'.format(vhost) + is_correct_vhost = connection._conn.conn.hostname == "vhost:{}".format(vhost) environment.close() delete_vhost(vhost) - assert is_correct_vhost is True \ No newline at end of file + assert is_correct_vhost is True + + +def test_connection_vhost_not_exists() -> None: + + exception = False + + vhost = "tmpVhost" + str(time.time()) + uri = "amqp://guest:guest@localhost:5672/{}".format(vhost) + + environment = Environment(uri=uri) + try: + connection = environment.connection() + connection.dial() + except ConnectionException: + exception = True + + assert exception is True From 7e6e6cf94c21c3f96ba0a1b15ee6e20d089289f8 Mon Sep 17 00:00:00 2001 From: "cicciodb@hotmail.it" Date: Wed, 21 May 2025 09:24:20 +0200 Subject: [PATCH 5/5] formatted correctly --- rabbitmq_amqp_python_client/qpid/proton/_reactor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rabbitmq_amqp_python_client/qpid/proton/_reactor.py b/rabbitmq_amqp_python_client/qpid/proton/_reactor.py index 316c800..1a5c8d6 100644 --- a/rabbitmq_amqp_python_client/qpid/proton/_reactor.py +++ b/rabbitmq_amqp_python_client/qpid/proton/_reactor.py @@ -1099,7 +1099,7 @@ def __init__(self, connection: Connection) -> None: def _connect(self, connection: Connection, url: Url) -> None: connection.url = url # if virtual-host not set, use host from address as default - if url.path is not None and url.path != '': + if url.path is not None and url.path != "": rabbitmq_vhost = urllib.parse.quote(url.path.replace("+", "%2B")) connection.hostname = "vhost:{}".format(rabbitmq_vhost) else: