File tree Expand file tree Collapse file tree 3 files changed +69
-2
lines changed
rabbitmq_amqp_python_client/qpid/proton Expand file tree Collapse file tree 3 files changed +69
-2
lines changed Original file line number Diff line number Diff line change 23
23
import os
24
24
import queue
25
25
import re
26
+ import urllib
26
27
from typing import (
27
28
TYPE_CHECKING ,
28
29
Any ,
@@ -1098,8 +1099,12 @@ def __init__(self, connection: Connection) -> None:
1098
1099
def _connect (self , connection : Connection , url : Url ) -> None :
1099
1100
connection .url = url
1100
1101
# 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 :
1102
1106
connection .hostname = url .host
1107
+
1103
1108
_logger .info ("Connecting to %r..." % url )
1104
1109
1105
1110
transport = Transport ()
Original file line number Diff line number Diff line change @@ -15,6 +15,30 @@ def get_connections_names() -> list:
15
15
return connection_names
16
16
17
17
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
+
18
42
def delete_connections (connection_names : []) -> None :
19
43
for connection_name in connection_names :
20
44
request = (
Original file line number Diff line number Diff line change 15
15
ValidationCodeException ,
16
16
WinSslConfigurationContext ,
17
17
)
18
+ from rabbitmq_amqp_python_client .qpid .proton import (
19
+ ConnectionException ,
20
+ )
18
21
19
- from .http_requests import delete_all_connections
22
+ from .http_requests import (
23
+ create_vhost ,
24
+ delete_all_connections ,
25
+ delete_vhost ,
26
+ )
20
27
from .utils import token
21
28
22
29
@@ -233,3 +240,34 @@ def test_reconnection_parameters() -> None:
233
240
exception = True
234
241
235
242
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
You can’t perform that action at this time.
0 commit comments