Skip to content

Added support for connection URL encoding #395

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

Merged
merged 1 commit into from
Nov 1, 2016
Merged
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
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