Skip to content

Added QueryDefaultsPlugin #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## 1.5.0 - Unreleased
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, 1.5 or Unreleased? Using a version there is confusing, that's why we usually avoid it.


### Added

- `QueryDefaultsPlugin` to add default query parameters.

## 1.4.2 - 2017-03-18

Expand Down
39 changes: 39 additions & 0 deletions spec/Plugin/QueryDefaultsPluginSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace spec\Http\Client\Common\Plugin;

use PhpSpec\Exception\Example\SkippingException;
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()
{
$this->beConstructedWith([]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can add this to a let method, it's like setUp in phpunit. If you use beConstructedWith again, it will override the on in the let method.

$this->shouldHaveType('Http\Client\Common\Plugin\QueryDefaultsPlugin');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we use ::class constants wherever possible. phpspec 3.0 generates specs like that by default.

}

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 () {});
}
}
2 changes: 1 addition & 1 deletion src/Plugin/HeaderAppendPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
Expand Down
53 changes: 53 additions & 0 deletions src/Plugin/QueryDefaultsPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Http\Client\Common\Plugin;

use Http\Client\Common\Plugin;
use Psr\Http\Message\RequestInterface;

/**
* Set query to default value if it does not exist.
*
* If a given query parameter already exists the value wont be replaced and the request wont be changed.
*
* @author Tobias Nyholm <[email protected]>
*/
final class QueryDefaultsPlugin implements Plugin
{
/**
* @var array
*/
private $queryParams = [];

/**
* @param array $queryParams Hashmap of query name to query value
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if i read the code correctly, keys and values should not be http encoded, right? i think we should mention that here to avoid confusion and double-encoded parameters

*/
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);
}
}