Skip to content

Add vhost support #70

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

Open
wants to merge 3 commits into
base: main
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
7 changes: 6 additions & 1 deletion rabbitmq_amqp_python_client/qpid/proton/_reactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import os
import queue
import re
import urllib
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -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()
Expand Down
24 changes: 24 additions & 0 deletions tests/http_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down
17 changes: 16 additions & 1 deletion tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)

from .http_requests import delete_all_connections
from .http_requests import create_vhost, delete_vhost
from .utils import token


Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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