Skip to content

Commit 71bc439

Browse files
authored
Add vhost support (#70)
* Add vhost support * Fixes: #66
1 parent fe4e468 commit 71bc439

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

rabbitmq_amqp_python_client/qpid/proton/_reactor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import os
2424
import queue
2525
import re
26+
import urllib
2627
from typing import (
2728
TYPE_CHECKING,
2829
Any,
@@ -1098,8 +1099,12 @@ def __init__(self, connection: Connection) -> None:
10981099
def _connect(self, connection: Connection, url: Url) -> None:
10991100
connection.url = url
11001101
# if virtual-host not set, use host from address as default
1101-
if self.virtual_host is None:
1102+
if url.path is not None and url.path != "":
1103+
rabbitmq_vhost = urllib.parse.quote(url.path.replace("+", "%2B"))
1104+
connection.hostname = "vhost:{}".format(rabbitmq_vhost)
1105+
else:
11021106
connection.hostname = url.host
1107+
11031108
_logger.info("Connecting to %r..." % url)
11041109

11051110
transport = Transport()

tests/http_requests.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@ def get_connections_names() -> list:
1515
return connection_names
1616

1717

18+
# not used
19+
def get_vhosts() -> list:
20+
request = "http://localhost:15672/api/vhosts"
21+
responses = requests.get(request, auth=HTTPBasicAuth("guest", "guest"))
22+
responses.raise_for_status()
23+
vhosts = responses.json()
24+
vhosts_names = []
25+
for vhost in vhosts:
26+
vhosts_names.append(vhost["name"])
27+
return vhosts_names
28+
29+
30+
def create_vhost(vhost_name: str) -> None:
31+
request = "http://localhost:15672/api/vhosts/{}".format(vhost_name)
32+
responses = requests.put(request, auth=HTTPBasicAuth("guest", "guest"))
33+
responses.raise_for_status()
34+
35+
36+
def delete_vhost(vhost_name: str) -> None:
37+
request = "http://localhost:15672/api/vhosts/{}/".format(vhost_name)
38+
responses = requests.delete(request, auth=HTTPBasicAuth("guest", "guest"))
39+
responses.raise_for_status()
40+
41+
1842
def delete_connections(connection_names: []) -> None:
1943
for connection_name in connection_names:
2044
request = (

tests/test_connection.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,15 @@
1515
ValidationCodeException,
1616
WinSslConfigurationContext,
1717
)
18+
from rabbitmq_amqp_python_client.qpid.proton import (
19+
ConnectionException,
20+
)
1821

19-
from .http_requests import delete_all_connections
22+
from .http_requests import (
23+
create_vhost,
24+
delete_all_connections,
25+
delete_vhost,
26+
)
2027
from .utils import token
2128

2229

@@ -233,3 +240,34 @@ def test_reconnection_parameters() -> None:
233240
exception = True
234241

235242
assert exception is True
243+
244+
245+
def test_connection_vhost() -> None:
246+
vhost = "tmpVhost" + str(time.time())
247+
create_vhost(vhost)
248+
uri = "amqp://guest:guest@localhost:5672/{}".format(vhost)
249+
environment = Environment(uri=uri)
250+
connection = environment.connection()
251+
connection.dial()
252+
is_correct_vhost = connection._conn.conn.hostname == "vhost:{}".format(vhost)
253+
environment.close()
254+
delete_vhost(vhost)
255+
256+
assert is_correct_vhost is True
257+
258+
259+
def test_connection_vhost_not_exists() -> None:
260+
261+
exception = False
262+
263+
vhost = "tmpVhost" + str(time.time())
264+
uri = "amqp://guest:guest@localhost:5672/{}".format(vhost)
265+
266+
environment = Environment(uri=uri)
267+
try:
268+
connection = environment.connection()
269+
connection.dial()
270+
except ConnectionException:
271+
exception = True
272+
273+
assert exception is True

0 commit comments

Comments
 (0)