-
Notifications
You must be signed in to change notification settings - Fork 53
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
Changes from 3 commits
45d2c97
bb72194
0bc6c6b
780d0c7
23bbe55
a45b9d1
e5fbe4a
85e2938
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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([]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can add this to a |
||
$this->shouldHaveType('Http\Client\Common\Plugin\QueryDefaultsPlugin'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we use |
||
} | ||
|
||
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 () {}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
*/ | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
There was a problem hiding this comment.
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.