diff --git a/spec/HeaderAppendPluginSpec.php b/spec/HeaderAppendPluginSpec.php new file mode 100644 index 0000000..072b350 --- /dev/null +++ b/spec/HeaderAppendPluginSpec.php @@ -0,0 +1,37 @@ +beConstructedWith([]); + $this->shouldHaveType('Http\Client\Plugin\HeaderAppendPlugin'); + } + + public function it_is_a_plugin() + { + $this->beConstructedWith([]); + $this->shouldImplement('Http\Client\Plugin\Plugin'); + } + + public function it_appends_the_header(RequestInterface $request) + { + $this->beConstructedWith([ + 'foo'=>'bar', + 'baz'=>'qux' + ]); + + $request->withAddedHeader('foo', 'bar')->shouldBeCalled()->willReturn($request); + $request->withAddedHeader('baz', 'qux')->shouldBeCalled()->willReturn($request); + + $this->handleRequest($request, function () {}, function () {}); + } +} diff --git a/spec/HeaderDefaultsPluginSpec.php b/spec/HeaderDefaultsPluginSpec.php new file mode 100644 index 0000000..4fceba4 --- /dev/null +++ b/spec/HeaderDefaultsPluginSpec.php @@ -0,0 +1,38 @@ +beConstructedWith([]); + $this->shouldHaveType('Http\Client\Plugin\HeaderDefaultsPlugin'); + } + + public function it_is_a_plugin() + { + $this->beConstructedWith([]); + $this->shouldImplement('Http\Client\Plugin\Plugin'); + } + + public function it_sets_the_default_header(RequestInterface $request) + { + $this->beConstructedWith([ + 'foo' => 'bar', + 'baz' => 'qux' + ]); + + $request->hasHeader('foo')->shouldBeCalled()->willReturn(false); + $request->withHeader('foo', 'bar')->shouldBeCalled()->willReturn($request); + $request->hasHeader('baz')->shouldBeCalled()->willReturn(true); + + $this->handleRequest($request, function () {}, function () {}); + } +} diff --git a/spec/HeaderRemovePluginSpec.php b/spec/HeaderRemovePluginSpec.php new file mode 100644 index 0000000..b2edebd --- /dev/null +++ b/spec/HeaderRemovePluginSpec.php @@ -0,0 +1,39 @@ +beConstructedWith([]); + $this->shouldHaveType('Http\Client\Plugin\HeaderRemovePlugin'); + } + + public function it_is_a_plugin() + { + $this->beConstructedWith([]); + $this->shouldImplement('Http\Client\Plugin\Plugin'); + } + + public function it_removes_the_header(RequestInterface $request) + { + $this->beConstructedWith([ + 'foo', + 'baz' + ]); + + $request->hasHeader('foo')->shouldBeCalled()->willReturn(false); + + $request->hasHeader('baz')->shouldBeCalled()->willReturn(true); + $request->withoutHeader('baz')->shouldBeCalled()->willReturn($request); + + $this->handleRequest($request, function () {}, function () {}); + } +} diff --git a/spec/HeaderSetPluginSpec.php b/spec/HeaderSetPluginSpec.php new file mode 100644 index 0000000..53c1b75 --- /dev/null +++ b/spec/HeaderSetPluginSpec.php @@ -0,0 +1,37 @@ +beConstructedWith([]); + $this->shouldHaveType('Http\Client\Plugin\HeaderSetPlugin'); + } + + public function it_is_a_plugin() + { + $this->beConstructedWith([]); + $this->shouldImplement('Http\Client\Plugin\Plugin'); + } + + public function it_set_the_header(RequestInterface $request) + { + $this->beConstructedWith([ + 'foo'=>'bar', + 'baz'=>'qux' + ]); + + $request->withHeader('foo', 'bar')->shouldBeCalled()->willReturn($request); + $request->withHeader('baz', 'qux')->shouldBeCalled()->willReturn($request); + + $this->handleRequest($request, function () {}, function () {}); + } +} diff --git a/src/HeaderAppendPlugin.php b/src/HeaderAppendPlugin.php new file mode 100644 index 0000000..b3fcac2 --- /dev/null +++ b/src/HeaderAppendPlugin.php @@ -0,0 +1,40 @@ + + */ +class HeaderAppendPlugin implements Plugin +{ + private $headers = []; + + /** + * @param array $headers headers to add to the request + */ + public function __construct(array $headers) + { + $this->headers = $headers; + } + + /** + * {@inheritdoc} + */ + public function handleRequest(RequestInterface $request, callable $next, callable $first) + { + foreach ($this->headers as $header => $headerValue) { + $request = $request->withAddedHeader($header, $headerValue); + } + + return $next($request); + } +} diff --git a/src/HeaderDefaultsPlugin.php b/src/HeaderDefaultsPlugin.php new file mode 100644 index 0000000..fbee194 --- /dev/null +++ b/src/HeaderDefaultsPlugin.php @@ -0,0 +1,38 @@ + + */ +class HeaderDefaultsPlugin implements Plugin +{ + private $headers = []; + + /** + * @param array $headers headers to set to the request + */ + public function __construct(array $headers) + { + $this->headers = $headers; + } + + /** + * {@inheritdoc} + */ + public function handleRequest(RequestInterface $request, callable $next, callable $first) + { + foreach ($this->headers as $header => $headerValue) { + if (!$request->hasHeader($header)) { + $request = $request->withHeader($header, $headerValue); + } + } + + return $next($request); + } +} diff --git a/src/HeaderRemovePlugin.php b/src/HeaderRemovePlugin.php new file mode 100644 index 0000000..b49ac26 --- /dev/null +++ b/src/HeaderRemovePlugin.php @@ -0,0 +1,37 @@ + + */ +class HeaderRemovePlugin implements Plugin +{ + private $headers = []; + + /** + * @param array $headers headers to remove from the request + */ + public function __construct(array $headers) + { + $this->headers = $headers; + } + + /** + * {@inheritdoc} + */ + public function handleRequest(RequestInterface $request, callable $next, callable $first) + { + foreach ($this->headers as $header) { + if ($request->hasHeader($header)) { + $request = $request->withoutHeader($header); + } + } + + return $next($request); + } +} diff --git a/src/HeaderSetPlugin.php b/src/HeaderSetPlugin.php new file mode 100644 index 0000000..0e855f9 --- /dev/null +++ b/src/HeaderSetPlugin.php @@ -0,0 +1,36 @@ + + */ +class HeaderSetPlugin implements Plugin +{ + private $headers = []; + + /** + * @param array $headers headers to set to the request + */ + public function __construct(array $headers) + { + $this->headers = $headers; + } + + /** + * {@inheritdoc} + */ + public function handleRequest(RequestInterface $request, callable $next, callable $first) + { + foreach ($this->headers as $header => $headerValue) { + $request = $request->withHeader($header, $headerValue); + } + + return $next($request); + } +}