Skip to content

allow to configure content type plugin #316

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
Mar 5, 2019
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
- Autowiring support for FlexibleClient, HttpMethodsClientInterface and
BatchClientInterface if they are enabled on the default/first client.
(Only available with Httplug 2)
- Configuration for the content_type plugin

### Changed

Expand Down
13 changes: 13 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,19 @@ private function createClientPluginNode()
->end()
->end()
->end()
->arrayNode('content_type')
->canBeEnabled()
->info('Detect the content type of a request body and set the Content-Type header if it is not already set.')
->children()
->booleanNode('skip_detection')
->info('Whether to skip detection when request body is larger than size_limit')
->defaultFalse()
->end()
->scalarNode('size_limit')
->info('Skip content type detection if request body is larger than size_limit bytes')
->end()
->end()
->end()
->arrayNode('header_append')
->canBeEnabled()
->info('Append headers to the request. If the header already exists the value will be appended to the current value.')
Expand Down
14 changes: 14 additions & 0 deletions src/DependencyInjection/HttplugExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,40 +186,47 @@ private function configurePluginByName($name, Definition $definition, array $con
->replaceArgument(2, $options);

break;

case 'cookie':
$definition->replaceArgument(0, new Reference($config['cookie_jar']));

break;

case 'decoder':
$definition->addArgument([
'use_content_encoding' => $config['use_content_encoding'],
]);

break;

case 'history':
$definition->replaceArgument(0, new Reference($config['journal']));

break;

case 'logger':
$definition->replaceArgument(0, new Reference($config['logger']));
if (!empty($config['formatter'])) {
$definition->replaceArgument(1, new Reference($config['formatter']));
}

break;

case 'redirect':
$definition->addArgument([
'preserve_header' => $config['preserve_header'],
'use_default_for_multiple' => $config['use_default_for_multiple'],
]);

break;

case 'retry':
$definition->addArgument([
'retries' => $config['retry'],
]);

break;

case 'stopwatch':
$definition->replaceArgument(0, new Reference($config['stopwatch']));

Expand All @@ -236,12 +243,14 @@ private function configurePluginByName($name, Definition $definition, array $con
]);

break;

case 'add_path':
$pathUriService = $serviceId.'.path_uri';
$this->createUri($container, $pathUriService, $config['path']);
$definition->replaceArgument(0, new Reference($pathUriService));

break;

case 'base_uri':
$baseUriService = $serviceId.'.base_uri';
$this->createUri($container, $baseUriService, $config['uri']);
Expand All @@ -251,6 +260,11 @@ private function configurePluginByName($name, Definition $definition, array $con
]);

break;

case 'content_type':
$definition->replaceArgument(0, $config);
break;

case 'header_append':
case 'header_defaults':
case 'header_set':
Expand Down
3 changes: 3 additions & 0 deletions src/Resources/config/plugins.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<argument/>
<argument/>
</service>
<service id="httplug.plugin.content_type" class="Http\Client\Common\Plugin\ContentTypePlugin" public="false" abstract="true">
<argument/>
</service>
<service id="httplug.plugin.header_append" class="Http\Client\Common\Plugin\HeaderAppendPlugin" public="false" abstract="true">
<argument/>
</service>
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/DependencyInjection/HttplugExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public function testClientPlugins()
'host' => 'http://localhost:8000',
],
],
[
'content_type' => [
'skip_detection' => true,
],
],
[
'header_append' => [
'headers' => ['X-FOO' => 'bar'],
Expand Down Expand Up @@ -131,6 +136,7 @@ public function testClientPlugins()
'httplug.client.acme.plugin.decoder',
'httplug.plugin.redirect',
'httplug.client.acme.plugin.add_host',
'httplug.client.acme.plugin.content_type',
'httplug.client.acme.plugin.header_append',
'httplug.client.acme.plugin.header_defaults',
'httplug.client.acme.plugin.header_set',
Expand Down