From 45d2c97fee1ddbbb5cc42e2e47f6a35d80b531d4 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 27 Mar 2017 19:34:30 +0200 Subject: [PATCH 1/8] Added QueryDefaultsPlugin This will fix #66 --- spec/Plugin/QueryDefaultsPluginSpec.php | 39 ++++++++++++++++++ src/Plugin/QueryDefaultsPlugin.php | 53 +++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 spec/Plugin/QueryDefaultsPluginSpec.php create mode 100644 src/Plugin/QueryDefaultsPlugin.php diff --git a/spec/Plugin/QueryDefaultsPluginSpec.php b/spec/Plugin/QueryDefaultsPluginSpec.php new file mode 100644 index 0000000..4093568 --- /dev/null +++ b/spec/Plugin/QueryDefaultsPluginSpec.php @@ -0,0 +1,39 @@ +beConstructedWith([]); + $this->shouldHaveType('Http\Client\Common\Plugin\QueryDefaultsPlugin'); + } + + public function it_is_a_plugin() + { + $this->beConstructedWith([]); + $this->shouldImplement('Http\Client\Common\Plugin'); + } + + public function it_sets_the_default_header(RequestInterface $request, UriInterface $uri) + { + $this->beConstructedWith([ + 'foo' => 'bar', + ]); + + $request->getUri()->shouldBeCalled()->willReturn($uri); + $uri->getQuery()->shouldBeCalled()->willReturn('test=true'); + $uri->withQuery('test=true&foo=bar')->shouldBeCalled()->willReturn($uri); + $request->withUri($uri)->shouldBeCalled()->willReturn($request); + + $this->handleRequest($request, function () {}, function () {}); + } +} diff --git a/src/Plugin/QueryDefaultsPlugin.php b/src/Plugin/QueryDefaultsPlugin.php new file mode 100644 index 0000000..2f44759 --- /dev/null +++ b/src/Plugin/QueryDefaultsPlugin.php @@ -0,0 +1,53 @@ + + */ +final class QueryDefaultsPlugin implements Plugin +{ + /** + * @var array + */ + private $queryParams = []; + + /** + * @param array $queryParams Hashmap of query name to query value + */ + public function __construct(array $queryParams) + { + $this->queryParams = $queryParams; + } + + /** + * {@inheritdoc} + */ + public function handleRequest(RequestInterface $request, callable $next, callable $first) + { + foreach ($this->queryParams as $name => $value) { + $uri = $request->getUri(); + $array = []; + parse_str($uri->getQuery(), $array); + + // If query value is not found + if (!isset($array[$name])) { + $array[$name] = $value; + + // Create a new request with the new URI with the added query param + $request = $request->withUri( + $uri->withQuery(http_build_query($array)) + ); + } + } + + return $next($request); + } +} From bb721945247652fbebddc7b5f66896b4b7566738 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 27 Mar 2017 19:36:20 +0200 Subject: [PATCH 2/8] Added change log --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6df8e3e..56cfbed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Change Log +## 1.5.0 - Unreleased + +### Added + +- `QueryDefaultsPlugin` to add default query parameters. ## 1.4.2 - 2017-03-18 From 0bc6c6b823292140f5d5d3457a98d73fb0c3a3b3 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Mon, 27 Mar 2017 19:36:35 +0200 Subject: [PATCH 3/8] Style CI fix --- src/Plugin/HeaderAppendPlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/HeaderAppendPlugin.php b/src/Plugin/HeaderAppendPlugin.php index 1e7b320..26fd813 100644 --- a/src/Plugin/HeaderAppendPlugin.php +++ b/src/Plugin/HeaderAppendPlugin.php @@ -12,7 +12,7 @@ * * This only makes sense for headers that can have multiple values like 'Forwarded' * - * @link https://en.wikipedia.org/wiki/List_of_HTTP_header_fields + * @see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields * * @author Soufiane Ghzal */ From 780d0c70ccd9dfd72c04593f78535cf744d9455b Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 28 Mar 2017 09:11:49 +0200 Subject: [PATCH 4/8] Added docs about params not beeing encoded --- src/Plugin/QueryDefaultsPlugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin/QueryDefaultsPlugin.php b/src/Plugin/QueryDefaultsPlugin.php index 2f44759..7a20963 100644 --- a/src/Plugin/QueryDefaultsPlugin.php +++ b/src/Plugin/QueryDefaultsPlugin.php @@ -20,7 +20,7 @@ final class QueryDefaultsPlugin implements Plugin private $queryParams = []; /** - * @param array $queryParams Hashmap of query name to query value + * @param array $queryParams Hashmap of query name to query value. Names and values should not be url encoded. */ public function __construct(array $queryParams) { From 23bbe55b865d00e9c060d9710458dff6769d3c0c Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 28 Mar 2017 09:12:23 +0200 Subject: [PATCH 5/8] Updated changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56cfbed..b0ca21a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Change Log -## 1.5.0 - Unreleased +## Unreleased ### Added From a45b9d17f22d5caf214b0d75f7e18841a921e502 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 28 Mar 2017 09:13:55 +0200 Subject: [PATCH 6/8] Updated Spec --- spec/Plugin/QueryDefaultsPluginSpec.php | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/spec/Plugin/QueryDefaultsPluginSpec.php b/spec/Plugin/QueryDefaultsPluginSpec.php index 4093568..6327944 100644 --- a/spec/Plugin/QueryDefaultsPluginSpec.php +++ b/spec/Plugin/QueryDefaultsPluginSpec.php @@ -2,25 +2,26 @@ namespace spec\Http\Client\Common\Plugin; -use PhpSpec\Exception\Example\SkippingException; +use Http\Client\Common\Plugin; use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\StreamInterface; use PhpSpec\ObjectBehavior; -use Prophecy\Argument; use Psr\Http\Message\UriInterface; class QueryDefaultsPluginSpec extends ObjectBehavior { - public function it_is_initializable() + public function let() { $this->beConstructedWith([]); + } + + public function it_is_initializable() + { $this->shouldHaveType('Http\Client\Common\Plugin\QueryDefaultsPlugin'); } public function it_is_a_plugin() { - $this->beConstructedWith([]); - $this->shouldImplement('Http\Client\Common\Plugin'); + $this->shouldImplement(Plugin::class); } public function it_sets_the_default_header(RequestInterface $request, UriInterface $uri) @@ -34,6 +35,8 @@ public function it_sets_the_default_header(RequestInterface $request, UriInterfa $uri->withQuery('test=true&foo=bar')->shouldBeCalled()->willReturn($uri); $request->withUri($uri)->shouldBeCalled()->willReturn($request); - $this->handleRequest($request, function () {}, function () {}); + $this->handleRequest($request, function () { + }, function () { + }); } } From e5fbe4a0ed96fca8a1c044060a3799760395d463 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 28 Mar 2017 09:24:41 +0200 Subject: [PATCH 7/8] Do not use ::class constant --- spec/Plugin/QueryDefaultsPluginSpec.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/Plugin/QueryDefaultsPluginSpec.php b/spec/Plugin/QueryDefaultsPluginSpec.php index 6327944..e4c88d0 100644 --- a/spec/Plugin/QueryDefaultsPluginSpec.php +++ b/spec/Plugin/QueryDefaultsPluginSpec.php @@ -21,7 +21,7 @@ public function it_is_initializable() public function it_is_a_plugin() { - $this->shouldImplement(Plugin::class); + $this->shouldImplement('Http\Client\Common\Plugin'); } public function it_sets_the_default_header(RequestInterface $request, UriInterface $uri) From 85e2938584d5527830db3722002bb9bc9a6d28d4 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Tue, 28 Mar 2017 09:39:03 +0200 Subject: [PATCH 8/8] Updated docs --- src/Plugin/QueryDefaultsPlugin.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Plugin/QueryDefaultsPlugin.php b/src/Plugin/QueryDefaultsPlugin.php index 7a20963..6c1e32c 100644 --- a/src/Plugin/QueryDefaultsPlugin.php +++ b/src/Plugin/QueryDefaultsPlugin.php @@ -20,7 +20,8 @@ final class QueryDefaultsPlugin implements Plugin private $queryParams = []; /** - * @param array $queryParams Hashmap of query name to query value. Names and values should not be url encoded. + * @param array $queryParams Hashmap of query name to query value. Names and values must not be url encoded as + * this plugin will encode them */ public function __construct(array $queryParams) {