Fix invalid option sslmode

This commit is contained in:
Jérémy Derussé 2020-07-24 12:11:03 +02:00
parent ea258254d0
commit afbd51b368
No known key found for this signature in database
GPG Key ID: 2083FA5758C473D2
2 changed files with 56 additions and 13 deletions

View File

@ -97,6 +97,24 @@ class ConnectionTest extends TestCase
);
}
public function testFromDsnWithSslMode()
{
$httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock();
$this->assertEquals(
new Connection(['queue_name' => 'queue'], new SqsClient(['region' => 'eu-west-1', 'endpoint' => 'http://localhost', 'accessKeyId' => null, 'accessKeySecret' => null], null, $httpClient)),
Connection::fromDsn('sqs://localhost/queue?sslmode=disable', [], $httpClient)
);
}
public function testFromDsnWithSslModeOnDefault()
{
$httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock();
$this->assertEquals(
new Connection(['queue_name' => 'queue'], new SqsClient(['region' => 'eu-west-1', 'accessKeyId' => null, 'accessKeySecret' => null], null, $httpClient)),
Connection::fromDsn('sqs://default/queue?sslmode=disable', [], $httpClient)
);
}
public function testFromDsnWithCustomEndpointAndPort()
{
$httpClient = $this->getMockBuilder(HttpClientInterface::class)->getMock();
@ -149,6 +167,30 @@ class ConnectionTest extends TestCase
);
}
public function testFromDsnWithInvalidQueryString()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown option found in DSN: [foo]. Allowed options are [buffer_size, wait_time, poll_timeout, visibility_timeout, auto_setup, access_key, secret_key, endpoint, region, queue_name, account, sslmode].');
Connection::fromDsn('sqs://default?foo=foo');
}
public function testFromDsnWithInvalidOption()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown option found: [bar]. Allowed options are [buffer_size, wait_time, poll_timeout, visibility_timeout, auto_setup, access_key, secret_key, endpoint, region, queue_name, account, sslmode].');
Connection::fromDsn('sqs://default', ['bar' => 'bar']);
}
public function testFromDsnWithInvalidQueryStringAndOption()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown option found: [bar]. Allowed options are [buffer_size, wait_time, poll_timeout, visibility_timeout, auto_setup, access_key, secret_key, endpoint, region, queue_name, account, sslmode].');
Connection::fromDsn('sqs://default?foo=foo', ['bar' => 'bar']);
}
public function testKeepGettingPendingMessages()
{
$client = $this->createMock(SqsClient::class);

View File

@ -44,6 +44,7 @@ class Connection
'region' => 'eu-west-1',
'queue_name' => 'messages',
'account' => null,
'sslmode' => null,
];
private $configuration;
@ -94,6 +95,19 @@ class Connection
if (isset($parsedUrl['query'])) {
parse_str($parsedUrl['query'], $query);
}
// check for extra keys in options
$optionsExtraKeys = array_diff(array_keys($options), array_keys(self::DEFAULT_OPTIONS));
if (0 < \count($optionsExtraKeys)) {
throw new InvalidArgumentException(sprintf('Unknown option found: [%s]. Allowed options are [%s].', implode(', ', $optionsExtraKeys), implode(', ', array_keys(self::DEFAULT_OPTIONS))));
}
// check for extra keys in options
$queryExtraKeys = array_diff(array_keys($query), array_keys(self::DEFAULT_OPTIONS));
if (0 < \count($queryExtraKeys)) {
throw new InvalidArgumentException(sprintf('Unknown option found in DSN: [%s]. Allowed options are [%s].', implode(', ', $queryExtraKeys), implode(', ', array_keys(self::DEFAULT_OPTIONS))));
}
$options = $query + $options + self::DEFAULT_OPTIONS;
$configuration = [
'buffer_size' => (int) $options['buffer_size'],
@ -116,7 +130,6 @@ class Connection
if (preg_match(';^sqs\.([^\.]++)\.amazonaws\.com$;', $parsedUrl['host'], $matches)) {
$clientConfiguration['region'] = $matches[1];
}
unset($query['sslmode']);
} elseif (self::DEFAULT_OPTIONS['endpoint'] !== $options['endpoint'] ?? self::DEFAULT_OPTIONS['endpoint']) {
$clientConfiguration['endpoint'] = $options['endpoint'];
}
@ -127,18 +140,6 @@ class Connection
}
$configuration['account'] = 2 === \count($parsedPath) ? $parsedPath[0] : $options['account'] ?? self::DEFAULT_OPTIONS['account'];
// check for extra keys in options
$optionsExtraKeys = array_diff(array_keys($options), array_keys(self::DEFAULT_OPTIONS));
if (0 < \count($optionsExtraKeys)) {
throw new InvalidArgumentException(sprintf('Unknown option found : [%s]. Allowed options are [%s].', implode(', ', $optionsExtraKeys), implode(', ', array_keys(self::DEFAULT_OPTIONS))));
}
// check for extra keys in options
$queryExtraKeys = array_diff(array_keys($query), array_keys(self::DEFAULT_OPTIONS));
if (0 < \count($queryExtraKeys)) {
throw new InvalidArgumentException(sprintf('Unknown option found in DSN: [%s]. Allowed options are [%s].', implode(', ', $queryExtraKeys), implode(', ', array_keys(self::DEFAULT_OPTIONS))));
}
return new self($configuration, new SqsClient($clientConfiguration, null, $client));
}