Skip to content

Commit 790a79c

Browse files
committed
Header Plugin
1 parent 789cc6d commit 790a79c

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

spec/HeaderPluginSpec.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 HeaderPluginSpec extends ObjectBehavior
12+
{
13+
public function it_is_initializable()
14+
{
15+
$this->shouldHaveType('Http\Client\Plugin\HeaderPlugin');
16+
}
17+
18+
public function it_is_a_plugin()
19+
{
20+
$this->shouldImplement('Http\Client\Plugin\Plugin');
21+
}
22+
23+
public function it_sets_the_value(RequestInterface $request)
24+
{
25+
$request->hasHeader('foo')->shouldBeCalled()->willReturn(false);
26+
$request->withHeader('foo', 'bar')->shouldBeCalled()->willReturn($request);
27+
28+
$this->withHeader('foo', 'bar');
29+
$this->handleRequest($request, function () {
30+
}, function () {
31+
});
32+
}
33+
34+
public function it_does_not_override_the_value(RequestInterface $request)
35+
{
36+
$request->hasHeader('foo')->shouldBeCalled()->willReturn(true);
37+
38+
$this->withHeader('foo', 'bar');
39+
$this->handleRequest($request, function () {
40+
}, function () {
41+
});
42+
}
43+
44+
public function it_overrides_the_value(RequestInterface $request)
45+
{
46+
$request->withHeader('foo', 'bar')->shouldBeCalled()->willReturn($request);
47+
48+
$this->withHeader('foo', 'bar', true);
49+
$this->handleRequest($request, function () {
50+
}, function () {
51+
});
52+
}
53+
54+
public function it_removes_the_value(RequestInterface $request)
55+
{
56+
$request->hasHeader('foo')->shouldBeCalled()->willReturn(true);
57+
$request->withoutHeader('foo')->shouldBeCalled()->willReturn($request);
58+
59+
$this->withoutHeader('foo');
60+
$this->handleRequest($request, function () {
61+
}, function () {
62+
});
63+
}
64+
65+
public function it_does_not_remove_the_value(RequestInterface $request)
66+
{
67+
$request->hasHeader('foo')->shouldBeCalled()->willReturn(false);
68+
69+
$this->withoutHeader('foo');
70+
$this->handleRequest($request, function () {
71+
}, function () {
72+
});
73+
}
74+
}

src/HeaderPlugin.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
/**
8+
* Manages http headers.
9+
*/
10+
class HeaderPlugin implements Plugin
11+
{
12+
/**
13+
* @var array headers that have fixed value
14+
*/
15+
private $headers = [];
16+
private $removedHeader = [];
17+
18+
/**
19+
* Sets a header with the given value to every requests.
20+
*
21+
* @param string $name name of the header
22+
* @param string $value value of the header
23+
* @param bool $override By default if the header is already set on the request the value wont be replaced
24+
* pass $override to true to make the header value the same for every request
25+
*/
26+
public function withHeader($name, $value, $override = false)
27+
{
28+
$this->headers[$name] = ['value' => $value, 'override' => $override];
29+
}
30+
31+
/**
32+
* Automatically removes the given header from the request.
33+
*
34+
* @param string $name name of the header to remove
35+
*/
36+
public function withoutHeader($name)
37+
{
38+
$this->removedHeader[] = $name;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
45+
{
46+
foreach ($this->headers as $header => $headerData) {
47+
if ($headerData['override'] || !$request->hasHeader($header)) {
48+
$request = $request->withHeader($header, $headerData['value']);
49+
}
50+
}
51+
foreach ($this->removedHeader as $header) {
52+
if ($request->hasHeader($header)) {
53+
$request = $request->withoutHeader($header);
54+
}
55+
}
56+
57+
return $next($request);
58+
}
59+
}

0 commit comments

Comments
 (0)