diff --git a/spec/Authentication/QueryParamSpec.php b/spec/Authentication/QueryParamSpec.php new file mode 100644 index 0000000..9f4d4b2 --- /dev/null +++ b/spec/Authentication/QueryParamSpec.php @@ -0,0 +1,55 @@ +beConstructedWith([ + 'username' => 'username', + 'password' => 'password', + ]); + } + + function it_is_initializable() + { + $this->shouldHaveType('Http\Message\Authentication\QueryParam'); + } + + function it_authenticates_a_request( + RequestInterface $request, + UriInterface $uri, + RequestInterface $newRequest, + UriInterface $newUri + ) { + $request->getUri()->willReturn($uri); + $uri->getQuery()->willReturn('param1=value1¶m2[]=value2'); + $uri->withQuery('param1=value1¶m2%5B0%5D=value2&username=username&password=password')->will( + function ($args) use ($newUri) { + $newUri->getQuery()->willReturn($args[0]); + + return $newUri; + } + ); + + $request->withUri($newUri)->will(function ($args) use ($newRequest) { + $newRequest->getUri()->willReturn($args[0]); + + return $newRequest; + }); + + $authenticatedRequest = $this->authenticate($request); + $authenticatedRequest->shouldBe($newRequest); + + $authenticatedUri = $authenticatedRequest->getUri(); + $authenticatedUri->shouldBe($newUri); + $authenticatedUri->getQuery()->shouldReturn('param1=value1¶m2%5B0%5D=value2&username=username&password=password'); + } +} diff --git a/src/Authentication/QueryParam.php b/src/Authentication/QueryParam.php new file mode 100644 index 0000000..14b58ff --- /dev/null +++ b/src/Authentication/QueryParam.php @@ -0,0 +1,50 @@ + + */ +final class QueryParam implements Authentication +{ + /** + * @var array + */ + private $params = []; + + /** + * @param array $params + */ + public function __construct(array $params) + { + $this->params = $params; + } + + /** + * {@inheritdoc} + */ + public function authenticate(RequestInterface $request) + { + $uri = $request->getUri(); + $query = $uri->getQuery(); + $params = []; + + parse_str($query, $params); + + $params = array_merge($params, $this->params); + + $query = http_build_query($params); + + $uri = $uri->withQuery($query); + + return $request->withUri($uri); + } +}