-
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 6 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,42 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client\Common\Plugin; | ||
|
||
use Http\Client\Common\Plugin; | ||
use Psr\Http\Message\RequestInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Psr\Http\Message\UriInterface; | ||
|
||
class QueryDefaultsPluginSpec extends ObjectBehavior | ||
{ | ||
public function let() | ||
{ | ||
$this->beConstructedWith([]); | ||
} | ||
|
||
public function it_is_initializable() | ||
{ | ||
$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->shouldImplement(Plugin::class); | ||
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. This will break php 5.4 compatibility. Even though I think we should drop 5.4 support, this is not a good enough reason. FYI @sagikazarmark |
||
} | ||
|
||
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. Names and values should not be url encoded. | ||
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. "should" is too weak. i would say "Names and values must not be url encoded as this plugin will encode them" |
||
*/ | ||
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.
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.