Skip to content

Commit

Permalink
Merge pull request #395 from dragosprotung/url_connection
Browse files Browse the repository at this point in the history
Added support for connection URL encoding
  • Loading branch information
stloyd authored Nov 1, 2016
2 parents a632e17 + 4518c77 commit 7fdd7c5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ old_sound_rabbit_mq:
# A different (unused) connection defined by an URL. One can omit all parts,
# except the scheme (amqp:). If both segment in the URL and a key value (see above)
# are given the value from the URL takes precedence.
# See https://www.rabbitmq.com/uri-spec.html on how to encode values.
url: 'amqp://guest:password@localhost:5672/vhost?lazy=1&connection_timeout=6'
producers:
upload_picture:
Expand Down
11 changes: 6 additions & 5 deletions RabbitMq/AMQPConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,21 @@ private function parseUrl($parameters)
throw new InvalidConfigurationException('Malformed parameter "url".');
}

// See https://www.rabbitmq.com/uri-spec.html
if (isset($url['host'])) {
$parameters['host'] = $url['host'];
$parameters['host'] = urldecode($url['host']);
}
if (isset($url['port'])) {
$parameters['port'] = $url['port'];
$parameters['port'] = (int)$url['port'];
}
if (isset($url['user'])) {
$parameters['user'] = $url['user'];
$parameters['user'] = urldecode($url['user']);
}
if (isset($url['pass'])) {
$parameters['password'] = $url['pass'];
$parameters['password'] = urldecode($url['pass']);
}
if (isset($url['path'])) {
$parameters['vhost'] = $url['path'];
$parameters['vhost'] = urldecode(ltrim($url['path'], '/'));
}

if (isset($url['query'])) {
Expand Down
64 changes: 62 additions & 2 deletions Tests/RabbitMq/AMQPConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,79 @@ public function testSetConnectionParametersWithUrl()
321, // port
'bar_user', // user
'bar_password', // password
'/whost', // vhost
'whost', // vhost
false, // insist
"AMQPLAIN", // login method
null, // login response
"en_US", // locale
6, // connection timeout
6, // read write timeout
null, // context
true, // keepalive
true, // keepalive
0, // heartbeat
), $instance->constructParams);
}

public function testSetConnectionParametersWithUrlEncoded()
{
$factory = new AMQPConnectionFactory(
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
array(
'url' => 'amqp://user%61:%61pass@ho%61st:10000/v%2fhost?keepalive=1&connection_timeout=6&read_write_timeout=6',
)
);

/** @var AMQPConnection $instance */
$instance = $factory->createConnection();
$this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
$this->assertEquals(array(
'hoast', // host
10000, // port
'usera', // user
'apass', // password
'v/host', // vhost
false, // insist
"AMQPLAIN", // login method
null, // login response
"en_US", // locale
6, // connection timeout
6, // read write timeout
null, // context
true, // keepalive
0, // heartbeat
), $instance->constructParams);
}

public function testSetConnectionParametersWithUrlWithoutVhost()
{
$factory = new AMQPConnectionFactory(
'OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection',
array(
'url' => 'amqp://user:pass@host:321/?keepalive=1&connection_timeout=6&read_write_timeout=6',
)
);

/** @var AMQPConnection $instance */
$instance = $factory->createConnection();
$this->assertInstanceOf('OldSound\RabbitMqBundle\Tests\RabbitMq\Fixtures\AMQPConnection', $instance);
$this->assertEquals(array(
'host', // host
321, // port
'user', // user
'pass', // password
'', // vhost
false, // insist
"AMQPLAIN", // login method
null, // login response
"en_US", // locale
6, // connection timeout
6, // read write timeout
null, // context
true, // keepalive
0, // heartbeat
), $instance->constructParams);
}

public function testSSLConnectionParameters()
{
$factory = new AMQPConnectionFactory(
Expand Down

0 comments on commit 7fdd7c5

Please sign in to comment.