Skip to content

Commit 1fa45e7

Browse files
committed
support header plugins on client
1 parent 606de16 commit 1fa45e7

File tree

8 files changed

+153
-10
lines changed

8 files changed

+153
-10
lines changed

DependencyInjection/Configuration.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,56 @@ private function configureClientPlugins(ArrayNodeDefinition $pluginNode)
278278
->end()
279279
->end()
280280
->end()
281-
282-
// TODO add aditional plugins that are only usable on a specific client
281+
->arrayNode('header_append')
282+
->canBeEnabled()
283+
->info('Append headers to the request. If the header already exists the value will be appended to the current value.')
284+
->fixXmlConfig('header')
285+
->children()
286+
->arrayNode('headers')
287+
->info('Keys are the header names, values the header values')
288+
->normalizeKeys(false)
289+
->useAttributeAsKey('name')
290+
->prototype('scalar')->end()
291+
->end()
292+
->end()
293+
->end()
294+
->arrayNode('header_defaults')
295+
->canBeEnabled()
296+
->info('Set header to default value if it does not exist.')
297+
->fixXmlConfig('header')
298+
->children()
299+
->arrayNode('headers')
300+
->info('Keys are the header names, values the header values')
301+
->normalizeKeys(false)
302+
->useAttributeAsKey('name')
303+
->prototype('scalar')->end()
304+
->end()
305+
->end()
306+
->end()
307+
->arrayNode('header_set')
308+
->canBeEnabled()
309+
->info('Set headers to requests. If the header does not exist it wil be set, if the header already exists it will be replaced.')
310+
->fixXmlConfig('header')
311+
->children()
312+
->arrayNode('headers')
313+
->info('Keys are the header names, values the header values')
314+
->normalizeKeys(false)
315+
->useAttributeAsKey('name')
316+
->prototype('scalar')->end()
317+
->end()
318+
->end()
319+
->end()
320+
->arrayNode('header_remove')
321+
->canBeEnabled()
322+
->info('Remove headers from requests.')
323+
->fixXmlConfig('header')
324+
->children()
325+
->arrayNode('headers')
326+
->info('List of header names to remove')
327+
->prototype('scalar')->end()
328+
->end()
329+
->end()
330+
->end()
283331
->end()
284332
->end();
285333
}

DependencyInjection/HttplugExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ private function configurePluginByName($name, Definition $definition, array $con
187187
'replace' => $config['replace'],
188188
]);
189189
break;
190+
case 'header_append':
191+
case 'header_defaults':
192+
case 'header_set':
193+
case 'header_remove':
194+
$definition->replaceArgument(0, $config['headers']);
195+
break;
190196

191197
default:
192198
throw new \InvalidArgumentException(sprintf('Internal exception: Plugin %s is not handled', $name));

Resources/config/plugins.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@
3434
<argument/>
3535
<argument/>
3636
</service>
37+
<service id="httplug.plugin.header_append" class="Http\Client\Common\Plugin\HeaderAppendPlugin" public="false" abstract="true">
38+
<argument/>
39+
</service>
40+
<service id="httplug.plugin.header_defaults" class="Http\Client\Common\Plugin\HeaderDefaultsPlugin" public="false" abstract="true">
41+
<argument/>
42+
</service>
43+
<service id="httplug.plugin.header_set" class="Http\Client\Common\Plugin\HeaderSetPlugin" public="false" abstract="true">
44+
<argument/>
45+
</service>
46+
<service id="httplug.plugin.header_remove" class="Http\Client\Common\Plugin\HeaderRemovePlugin" public="false" abstract="true">
47+
<argument/>
48+
</service>
3749

3850
</services>
3951
</container>

Tests/Resources/Fixtures/config/full.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,20 @@
2323
'host' => 'http://localhost',
2424
],
2525
],
26+
[
27+
'header_set' => [
28+
'headers' => [
29+
'X-FOO' => 'bar',
30+
],
31+
],
32+
],
33+
[
34+
'header_remove' => [
35+
'headers' => [
36+
'X-FOO',
37+
],
38+
],
39+
],
2640
],
2741
],
2842
],

Tests/Resources/Fixtures/config/full.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
<plugin>
1919
<add-host host="http://localhost"/>
2020
</plugin>
21+
<plugin>
22+
<header-set>
23+
<header name="X-FOO">bar</header>
24+
</header-set>
25+
</plugin>
26+
<plugin>
27+
<header-remove>
28+
<header>X-FOO</header>
29+
</header-remove>
30+
</plugin>
2131
</client>
2232
<profiling enabled="true" formatter="my_toolbar_formatter" captured_body_length="0"/>
2333
<plugins>

Tests/Resources/Fixtures/config/full.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ httplug:
1414
factory: httplug.factory.guzzle6
1515
http_methods_client: true
1616
plugins:
17-
- add_host:
18-
host: http://localhost
17+
-
18+
add_host:
19+
host: http://localhost
20+
-
21+
header_set:
22+
headers:
23+
X-FOO: bar
24+
-
25+
header_remove:
26+
headers: [X-FOO]
27+
1928
profiling:
2029
enabled: true
2130
formatter: my_toolbar_formatter

Tests/Unit/DependencyInjection/ConfigurationTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ public function testSupportsAllConfigFormats()
129129
'replace' => false,
130130
],
131131
],
132+
[
133+
'header_set' => [
134+
'enabled' => true,
135+
'headers' => [
136+
'X-FOO' => 'bar',
137+
],
138+
],
139+
],
140+
[
141+
'header_remove' => [
142+
'enabled' => true,
143+
'headers' => [
144+
'X-FOO',
145+
],
146+
],
147+
],
132148
],
133149
'config' => [],
134150
],

Tests/Unit/DependencyInjection/HttplugExtensionTest.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,37 @@ public function testClientPlugins()
7070
'acme' => [
7171
'factory' => 'httplug.factory.curl',
7272
'plugins' => [
73-
['decoder' => [
74-
'use_content_encoding' => false,
75-
]],
73+
[
74+
'decoder' => [
75+
'use_content_encoding' => false,
76+
]
77+
],
7678
'httplug.plugin.redirect',
77-
['add_host' => [
78-
'host' => 'http://localhost:8000',
79-
]],
79+
[
80+
'add_host' => [
81+
'host' => 'http://localhost:8000',
82+
]
83+
],
84+
[
85+
'header_append' => [
86+
'headers' => ['X-FOO' => 'bar']
87+
]
88+
],
89+
[
90+
'header_defaults' => [
91+
'headers' => ['X-FOO' => 'bar']
92+
]
93+
],
94+
[
95+
'header_set' => [
96+
'headers' => ['X-FOO' => 'bar']
97+
]
98+
],
99+
[
100+
'header_remove' => [
101+
'headers' => ['X-FOO']
102+
]
103+
],
80104
],
81105
],
82106
],
@@ -87,6 +111,10 @@ public function testClientPlugins()
87111
'httplug.client.acme.plugin.decoder',
88112
'httplug.plugin.redirect',
89113
'httplug.client.acme.plugin.add_host',
114+
'httplug.client.acme.plugin.header_append',
115+
'httplug.client.acme.plugin.header_defaults',
116+
'httplug.client.acme.plugin.header_set',
117+
'httplug.client.acme.plugin.header_remove',
90118
];
91119
$pluginReferences = array_map(function ($id) {
92120
return new Reference($id);

0 commit comments

Comments
 (0)