Skip to content

Commit 7fdd7c5

Browse files
authored
Merge pull request #395 from dragosprotung/url_connection
Added support for connection URL encoding
2 parents a632e17 + 4518c77 commit 7fdd7c5

File tree

3 files changed

+69
-7
lines changed

3 files changed

+69
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ old_sound_rabbit_mq:
117117
# A different (unused) connection defined by an URL. One can omit all parts,
118118
# except the scheme (amqp:). If both segment in the URL and a key value (see above)
119119
# are given the value from the URL takes precedence.
120+
# See https://www.rabbitmq.com/uri-spec.html on how to encode values.
120121
url: 'amqp://guest:password@localhost:5672/vhost?lazy=1&connection_timeout=6'
121122
producers:
122123
upload_picture:

RabbitMq/AMQPConnectionFactory.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,21 @@ private function parseUrl($parameters)
7474
throw new InvalidConfigurationException('Malformed parameter "url".');
7575
}
7676

77+
// See https://www.rabbitmq.com/uri-spec.html
7778
if (isset($url['host'])) {
78-
$parameters['host'] = $url['host'];
79+
$parameters['host'] = urldecode($url['host']);
7980
}
8081
if (isset($url['port'])) {
81-
$parameters['port'] = $url['port'];
82+
$parameters['port'] = (int)$url['port'];
8283
}
8384
if (isset($url['user'])) {
84-
$parameters['user'] = $url['user'];
85+
$parameters['user'] = urldecode($url['user']);
8586
}
8687
if (isset($url['pass'])) {
87-
$parameters['password'] = $url['pass'];
88+
$parameters['password'] = urldecode($url['pass']);
8889
}
8990
if (isset($url['path'])) {
90-
$parameters['vhost'] = $url['path'];
91+
$parameters['vhost'] = urldecode(ltrim($url['path'], '/'));
9192
}
9293

9394
if (isset($url['query'])) {

Tests/RabbitMq/AMQPConnectionFactoryTest.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,79 @@ public function testSetConnectionParametersWithUrl()
9191
321, // port
9292
'bar_user', // user
9393
'bar_password', // password
94-
'/whost', // vhost
94+
'whost', // vhost
9595
false, // insist
9696
"AMQPLAIN", // login method
9797
null, // login response
9898
"en_US", // locale
9999
6, // connection timeout
100100
6, // read write timeout
101101
null, // context
102-
true, // keepalive
102+
true, // keepalive
103+
0, // heartbeat
104+
), $instance->constructParams);
105+
}
106+
107+
public function testSetConnectionParametersWithUrlEncoded()
108+
{
109+
$factory = new AMQPConnectionFactory(
110+
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
111+
array(
112+
'url' => 'amqp://user%61:%61pass@ho%61st:10000/v%2fhost?keepalive=1&connection_timeout=6&read_write_timeout=6',
113+
)
114+
);
115+
116+
/** @var AMQPConnection $instance */
117+
$instance = $factory->createConnection();
118+
$this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
119+
$this->assertEquals(array(
120+
'hoast', // host
121+
10000, // port
122+
'usera', // user
123+
'apass', // password
124+
'v/host', // vhost
125+
false, // insist
126+
"AMQPLAIN", // login method
127+
null, // login response
128+
"en_US", // locale
129+
6, // connection timeout
130+
6, // read write timeout
131+
null, // context
132+
true, // keepalive
103133
0, // heartbeat
104134
), $instance->constructParams);
105135
}
106136

137+
public function testSetConnectionParametersWithUrlWithoutVhost()
138+
{
139+
$factory = new AMQPConnectionFactory(
140+
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
141+
array(
142+
'url' => 'amqp://user:pass@host:321/?keepalive=1&connection_timeout=6&read_write_timeout=6',
143+
)
144+
);
145+
146+
/** @var AMQPConnection $instance */
147+
$instance = $factory->createConnection();
148+
$this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
149+
$this->assertEquals(array(
150+
'host', // host
151+
321, // port
152+
'user', // user
153+
'pass', // password
154+
'', // vhost
155+
false, // insist
156+
"AMQPLAIN", // login method
157+
null, // login response
158+
"en_US", // locale
159+
6, // connection timeout
160+
6, // read write timeout
161+
null, // context
162+
true, // keepalive
163+
0, // heartbeat
164+
), $instance->constructParams);
165+
}
166+
107167
public function testSSLConnectionParameters()
108168
{
109169
$factory = new AMQPConnectionFactory(

0 commit comments

Comments
 (0)