Skip to content

validate type specific options #48

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
Feb 28, 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
44 changes: 32 additions & 12 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,24 +243,16 @@ private function addAuthenticationPluiginNode()
->then(function ($config) {
switch ($config['type']) {
case 'basic':
if (empty($config['username']) || empty($config['password'])) {
throw new InvalidConfigurationException('Authentication "basic" requires both "username" and "password".');
}
$this->validateAuthenticationType(['username', 'password'], $config, 'basic');
break;
case 'bearer':
if (empty($config['token'])) {
throw new InvalidConfigurationException('Authentication "bearer" requires a "token".');
}
$this->validateAuthenticationType(['token'], $config, 'bearer');
break;
case 'service':
if (empty($config['service'])) {
throw new InvalidConfigurationException('Authentication "service" requires a "service".');
}
$this->validateAuthenticationType(['service'], $config, 'service');
break;
case 'wsse':
if (empty($config['username']) || empty($config['password'])) {
throw new InvalidConfigurationException('Authentication "wsse" requires both "username" and "password".');
}
$this->validateAuthenticationType(['username', 'password'], $config, 'wsse');
break;
}

Expand All @@ -283,4 +275,32 @@ private function addAuthenticationPluiginNode()

return $node;
}

/**
* Validate that the configuration fragment has the specified keys and none other.
*
* @param array $expected Fields that must exist
* @param array $actual Actual configuration hashmap
* @param string $authName Name of authentication method for error messages
*
* @throws InvalidConfigurationException If $actual does not have exactly the keys specified in $expected (plus 'type')
*/
private function validateAuthenticationType(array $expected, array $actual, $authName)
{
unset($actual['type']);
$actual = array_keys($actual);
sort($actual);
sort($expected);

if ($expected === $actual) {
return;
}

throw new InvalidConfigurationException(sprintf(
'Authentication "%s" requires %s but got %s',
$authName,
implode(', ', $expected),
implode(', ', $actual)
));
}
}
54 changes: 54 additions & 0 deletions Tests/Resources/Fixtures/config/full.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,58 @@
'uri_factory' => 'Http\Message\UriFactory\GuzzleUriFactory',
'stream_factory' => 'Http\Message\StreamFactory\GuzzleStreamFactory',
],
'toolbar' => [
'enabled' => true,
'formatter' => 'my_toolbar_formatter',
],
'plugins' => [
'authentication' => [
'my_basic' => [
'type' => 'basic',
'username' => 'foo',
'password' => 'bar',
],
'my_wsse' => [
'type' => 'wsse',
'username' => 'foo',
'password' => 'bar',
],
'my_brearer' => [
'type' => 'bearer',
'token' => 'foo',
],
'my_service' => [
'type' => 'service',
'service' => 'my_auth_serivce',
],
],
'cache' => [
'stream_factory' => 'my_other_stream_factory',
'config' => [
'default_ttl' => 42,
'respect_cache_headers' => false,
],
],
'cookie' => [
'cookie_jar' => 'my_cookie_jar',
],
'decoder' => [
'enabled' => false,
],
'history' => [
'journal' => 'my_journal',
],
'logger' => [
'enabled' => false,
],
'redirect' => [
'enabled' => false,
],
'retry' => [
'enabled' => false,
],
'stopwatch' => [
'enabled' => false,
],
],
]);
19 changes: 19 additions & 0 deletions Tests/Resources/Fixtures/config/full.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@
<uri-factory>Http\Message\UriFactory\GuzzleUriFactory</uri-factory>
<stream-factory>Http\Message\StreamFactory\GuzzleStreamFactory</stream-factory>
</classes>
<toolbar enabled="true" formatter="my_toolbar_formatter"/>
<plugins>
<authentication>
<my_basic type="basic" username="foo" password="bar"/>
<my_wsse type="wsse" username="foo" password="bar"/>
<my_bearer type="bearer" token="foo"/>
<my_service type="service" service="my_auth_service"/>
</authentication>
<cache cache-pool="my_cache_pool" stream-factory="my_other_stream_factory">
<config default-ttl="42" respect-cache-headers="false"/>
</cache>
<cookie cookie-jar="my_cookie_jar"/>
<decoder enabled="false"/>
<history journal="my_journal"/>
<logger enabled="false"/>
<redirect enabled="false"/>
<retry enabled="false"/>
<stopwatch enabled="false"/>
</plugins>
</config>

</container>
41 changes: 40 additions & 1 deletion Tests/Resources/Fixtures/config/full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,43 @@ httplug:
client: Http\Adapter\Guzzle6\Client
message_factory: Http\Message\MessageFactory\GuzzleMessageFactory
uri_factory: Http\Message\UriFactory\GuzzleUriFactory
stream_factory: Http\Message\StreamFactory\GuzzleStreamFactory
stream_factory: Http\Message\StreamFactory\GuzzleStreamFactory
toolbar:
enabled: true
formatter: my_toolbar_formatter
plugins:
authentication:
my_basic:
type: basic
username: foo
password: bar
my_wsse:
type: wsse
username: foo
password: bar
my_brearer:
type: bearer
token: foo
my_service:
type: service
service: my_auth_serivce
cache:
cache_pool: my_cache_pool
stream_factory: my_other_stream_factory
config:
default_ttl: 42
respect_cache_headers: false
cookie:
cookie_jar: my_cookie_jar
decoder:
enabled: false
history:
journal: my_journal
logger:
enabled: false
redirect:
enabled: false
retry:
enabled: false
stopwatch:
enabled: false
8 changes: 8 additions & 0 deletions Tests/Resources/Fixtures/config/invalid_auth.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
httplug:
plugins:
authentication:
my_auth:
type: service
service: foobar
username: user
password: pass
62 changes: 47 additions & 15 deletions Tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,45 +112,67 @@ public function testSupportsAllConfigFormats()
],
'clients' => [],
'toolbar' => [
'enabled' => 'auto',
'formatter' => null,
'enabled' => true,
'formatter' => 'my_toolbar_formatter',
],
'plugins' => [
'authentication' => [],
'authentication' => [
'my_basic' => [
'type' => 'basic',
'username' => 'foo',
'password' => 'bar',
],
'my_wsse' => [
'type' => 'wsse',
'username' => 'foo',
'password' => 'bar',
],
'my_brearer' => [
'type' => 'bearer',
'token' => 'foo',
],
'my_service' => [
'type' => 'service',
'service' => 'my_auth_serivce',
],
],
'cache' => [
'enabled' => false,
'stream_factory' => 'httplug.stream_factory',
'enabled' => true,
'cache_pool' => 'my_cache_pool',
'stream_factory' => 'my_other_stream_factory',
'config' => [
'default_ttl' => null,
'respect_cache_headers' => true,
'default_ttl' => 42,
'respect_cache_headers' => false,
],
],
'cookie' => [
'enabled' => false,
'enabled' => true,
'cookie_jar' => 'my_cookie_jar',
],
'decoder' => [
'enabled' => true,
'enabled' => false,
'use_content_encoding' => true,
],
'history' => [
'enabled' => false,
'enabled' => true,
'journal' => 'my_journal',
],
'logger' => [
'enabled' => true,
'enabled' => false,
'logger' => 'logger',
'formatter' => null,
],
'redirect' => [
'enabled' => true,
'enabled' => false,
'preserve_header' => true,
'use_default_for_multiple' => true,
],
'retry' => [
'enabled' => true,
'enabled' => false,
'retry' => 1,
],
'stopwatch' => [
'enabled' => true,
'enabled' => false,
'stopwatch' => 'debug.stopwatch',
],
],
Expand All @@ -175,7 +197,17 @@ public function testSupportsAllConfigFormats()
*/
public function testMissingClass()
{
$file = __DIR__.'/../../Resources/Fixtures/config/invalid.yml';
$file = __DIR__.'/../../Resources/Fixtures/config/invalid_class.yml';
$this->assertProcessedConfigurationEquals([], [$file]);
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
* @expectedExceptionMessage password, service, username
*/
public function testInvalidAuthentication()
{
$file = __DIR__.'/../../Resources/Fixtures/config/invalid_auth.yml';
$this->assertProcessedConfigurationEquals([], [$file]);
}
}