diff --git a/rabbitmq_amqp_python_client/qpid/proton/_reactor.py b/rabbitmq_amqp_python_client/qpid/proton/_reactor.py index 89b2e7c..1a5c8d6 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 and url.path != "": + 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() 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..451e15e 100644 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -15,8 +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_all_connections, + delete_vhost, +) from .utils import token @@ -233,3 +240,34 @@ 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 + + +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