Skip to content

Commit 7431044

Browse files
committed
Merge pull request #44 from gsouf/feature-header-plugin
Header Plugin
2 parents 789cc6d + bfcbe24 commit 7431044

8 files changed

+302
-0
lines changed

spec/HeaderAppendPluginSpec.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Plugin;
4+
5+
use PhpSpec\Exception\Example\SkippingException;
6+
use PhpSpec\ObjectBehavior;
7+
use Prophecy\Argument;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\StreamInterface;
10+
11+
class HeaderAppendPluginSpec extends ObjectBehavior
12+
{
13+
public function it_is_initializable()
14+
{
15+
$this->beConstructedWith([]);
16+
$this->shouldHaveType('Http\Client\Plugin\HeaderAppendPlugin');
17+
}
18+
19+
public function it_is_a_plugin()
20+
{
21+
$this->beConstructedWith([]);
22+
$this->shouldImplement('Http\Client\Plugin\Plugin');
23+
}
24+
25+
public function it_appends_the_header(RequestInterface $request)
26+
{
27+
$this->beConstructedWith([
28+
'foo'=>'bar',
29+
'baz'=>'qux'
30+
]);
31+
32+
$request->withAddedHeader('foo', 'bar')->shouldBeCalled()->willReturn($request);
33+
$request->withAddedHeader('baz', 'qux')->shouldBeCalled()->willReturn($request);
34+
35+
$this->handleRequest($request, function () {}, function () {});
36+
}
37+
}

spec/HeaderDefaultsPluginSpec.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Plugin;
4+
5+
use PhpSpec\Exception\Example\SkippingException;
6+
use PhpSpec\ObjectBehavior;
7+
use Prophecy\Argument;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\StreamInterface;
10+
11+
class HeaderDefaultsPluginSpec extends ObjectBehavior
12+
{
13+
public function it_is_initializable()
14+
{
15+
$this->beConstructedWith([]);
16+
$this->shouldHaveType('Http\Client\Plugin\HeaderDefaultsPlugin');
17+
}
18+
19+
public function it_is_a_plugin()
20+
{
21+
$this->beConstructedWith([]);
22+
$this->shouldImplement('Http\Client\Plugin\Plugin');
23+
}
24+
25+
public function it_sets_the_default_header(RequestInterface $request)
26+
{
27+
$this->beConstructedWith([
28+
'foo' => 'bar',
29+
'baz' => 'qux'
30+
]);
31+
32+
$request->hasHeader('foo')->shouldBeCalled()->willReturn(false);
33+
$request->withHeader('foo', 'bar')->shouldBeCalled()->willReturn($request);
34+
$request->hasHeader('baz')->shouldBeCalled()->willReturn(true);
35+
36+
$this->handleRequest($request, function () {}, function () {});
37+
}
38+
}

spec/HeaderRemovePluginSpec.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Plugin;
4+
5+
use PhpSpec\Exception\Example\SkippingException;
6+
use PhpSpec\ObjectBehavior;
7+
use Prophecy\Argument;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\StreamInterface;
10+
11+
class HeaderRemovePluginSpec extends ObjectBehavior
12+
{
13+
public function it_is_initializable()
14+
{
15+
$this->beConstructedWith([]);
16+
$this->shouldHaveType('Http\Client\Plugin\HeaderRemovePlugin');
17+
}
18+
19+
public function it_is_a_plugin()
20+
{
21+
$this->beConstructedWith([]);
22+
$this->shouldImplement('Http\Client\Plugin\Plugin');
23+
}
24+
25+
public function it_removes_the_header(RequestInterface $request)
26+
{
27+
$this->beConstructedWith([
28+
'foo',
29+
'baz'
30+
]);
31+
32+
$request->hasHeader('foo')->shouldBeCalled()->willReturn(false);
33+
34+
$request->hasHeader('baz')->shouldBeCalled()->willReturn(true);
35+
$request->withoutHeader('baz')->shouldBeCalled()->willReturn($request);
36+
37+
$this->handleRequest($request, function () {}, function () {});
38+
}
39+
}

spec/HeaderSetPluginSpec.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Plugin;
4+
5+
use PhpSpec\Exception\Example\SkippingException;
6+
use PhpSpec\ObjectBehavior;
7+
use Prophecy\Argument;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\StreamInterface;
10+
11+
class HeaderSetPluginSpec extends ObjectBehavior
12+
{
13+
public function it_is_initializable()
14+
{
15+
$this->beConstructedWith([]);
16+
$this->shouldHaveType('Http\Client\Plugin\HeaderSetPlugin');
17+
}
18+
19+
public function it_is_a_plugin()
20+
{
21+
$this->beConstructedWith([]);
22+
$this->shouldImplement('Http\Client\Plugin\Plugin');
23+
}
24+
25+
public function it_set_the_header(RequestInterface $request)
26+
{
27+
$this->beConstructedWith([
28+
'foo'=>'bar',
29+
'baz'=>'qux'
30+
]);
31+
32+
$request->withHeader('foo', 'bar')->shouldBeCalled()->willReturn($request);
33+
$request->withHeader('baz', 'qux')->shouldBeCalled()->willReturn($request);
34+
35+
$this->handleRequest($request, function () {}, function () {});
36+
}
37+
}

src/HeaderAppendPlugin.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
/**
8+
* Adds headers to the request.
9+
* If the header already exists the value will be appended to the current value.
10+
*
11+
* This only makes sense for headers that can have multiple values like 'Forwarded'
12+
*
13+
* @link https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
14+
*
15+
* @author Soufiane Ghzal <[email protected]>
16+
*/
17+
class HeaderAppendPlugin implements Plugin
18+
{
19+
private $headers = [];
20+
21+
/**
22+
* @param array $headers headers to add to the request
23+
*/
24+
public function __construct(array $headers)
25+
{
26+
$this->headers = $headers;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
33+
{
34+
foreach ($this->headers as $header => $headerValue) {
35+
$request = $request->withAddedHeader($header, $headerValue);
36+
}
37+
38+
return $next($request);
39+
}
40+
}

src/HeaderDefaultsPlugin.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
/**
8+
* Set default values for the request headers.
9+
* If a given header already exists the value wont be replaced and the request wont be changed.
10+
*
11+
* @author Soufiane Ghzal <[email protected]>
12+
*/
13+
class HeaderDefaultsPlugin implements Plugin
14+
{
15+
private $headers = [];
16+
17+
/**
18+
* @param array $headers headers to set to the request
19+
*/
20+
public function __construct(array $headers)
21+
{
22+
$this->headers = $headers;
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
29+
{
30+
foreach ($this->headers as $header => $headerValue) {
31+
if (!$request->hasHeader($header)) {
32+
$request = $request->withHeader($header, $headerValue);
33+
}
34+
}
35+
36+
return $next($request);
37+
}
38+
}

src/HeaderRemovePlugin.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
/**
8+
* Removes headers from the request.
9+
*
10+
* @author Soufiane Ghzal <[email protected]>
11+
*/
12+
class HeaderRemovePlugin implements Plugin
13+
{
14+
private $headers = [];
15+
16+
/**
17+
* @param array $headers headers to remove from the request
18+
*/
19+
public function __construct(array $headers)
20+
{
21+
$this->headers = $headers;
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
28+
{
29+
foreach ($this->headers as $header) {
30+
if ($request->hasHeader($header)) {
31+
$request = $request->withoutHeader($header);
32+
}
33+
}
34+
35+
return $next($request);
36+
}
37+
}

src/HeaderSetPlugin.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
/**
8+
* Set headers to the request.
9+
* If the header does not exist it wil be set, if the header already exists it will be replaced.
10+
*
11+
* @author Soufiane Ghzal <[email protected]>
12+
*/
13+
class HeaderSetPlugin implements Plugin
14+
{
15+
private $headers = [];
16+
17+
/**
18+
* @param array $headers headers to set to the request
19+
*/
20+
public function __construct(array $headers)
21+
{
22+
$this->headers = $headers;
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
29+
{
30+
foreach ($this->headers as $header => $headerValue) {
31+
$request = $request->withHeader($header, $headerValue);
32+
}
33+
34+
return $next($request);
35+
}
36+
}

0 commit comments

Comments
 (0)