Skip to content

Commit

Permalink
added missing query string auth, add to account api (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
SecondeJK authored Feb 13, 2023
1 parent c4e6688 commit 5a2ac50
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Account/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\Container\ContainerInterface;
use Vonage\Client\APIResource;
use Vonage\Client\Credentials\Handler\BasicHandler;
use Vonage\Client\Credentials\Handler\BasicQueryHandler;

class ClientFactory
{
Expand All @@ -25,7 +26,7 @@ public function __invoke(ContainerInterface $container): Client
->setBaseUrl($accountApi->getClient()->getRestUrl())
->setIsHAL(false)
->setBaseUri('/account')
->setAuthHandler(new BasicHandler())
->setAuthHandler(new BasicQueryHandler())
;

return new Client($accountApi);
Expand Down
17 changes: 17 additions & 0 deletions src/Client/Credentials/Handler/BasicQueryHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Vonage\Client\Credentials\Handler;

use Psr\Http\Message\RequestInterface;
use Vonage\Client\Credentials\Basic;
use Vonage\Client\Credentials\CredentialsInterface;

class BasicQueryHandler extends AbstractHandler
{
public function __invoke(RequestInterface $request, CredentialsInterface $credentials): RequestInterface
{
$credentials = $this->extract(Basic::class, $credentials);

return $request->withUri($request->getUri()->withQuery(http_build_query($credentials->asArray())));
}
}
5 changes: 5 additions & 0 deletions test/Account/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function setUp(): void
$this->api = new APIResource();
$this->api->setBaseUrl('https://rest.nexmo.com')
->setIsHAL(false)
->setAuthHandler(new Client\Credentials\Handler\BasicQueryHandler())
->setBaseUri('/account');

$this->api->setClient($this->vonageClient->reveal());
Expand Down Expand Up @@ -163,6 +164,10 @@ public function testGetBalance(): void
$this->assertEquals('rest.nexmo.com', $request->getUri()->getHost());
$this->assertEquals('GET', $request->getMethod());

$uri = $request->getUri();
$uriString = $uri->__toString();
$this->assertEquals('https://rest.nexmo.com/account/get-balance?api_key=abc&api_secret=def', $uriString);

return true;
}))->shouldBeCalledTimes(1)->willReturn($this->getResponse('get-balance'));

Expand Down
23 changes: 23 additions & 0 deletions test/Client/Credentials/Handler/BasicQueryHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace VonageTest\Client\Credentials\Handler;

use Laminas\Diactoros\Request;
use Vonage\Client\Credentials\Basic;
use Vonage\Client\Credentials\Handler\BasicQueryHandler;
use PHPUnit\Framework\TestCase;

class BasicQueryHandlerTest extends TestCase
{
public function testWillAddCredentialsToRequest(): void
{
$request = new Request('https://example.com');
$credentials = new Basic('abc', 'xyz');
$handler = new BasicQueryHandler();
$request = $handler($request, $credentials);

$uri = $request->getUri();
$uriString = $uri->__toString();
$this->assertEquals('https://example.com?api_key=abc&api_secret=xyz', $uriString);
}
}

0 comments on commit 5a2ac50

Please sign in to comment.