Improve tests for actually test subscriber is called by Guzzle

This commit is contained in:
Adrian Palmer 2015-10-19 15:02:53 +11:00
parent 7873dbfa27
commit fcae83b0c0
2 changed files with 22 additions and 18 deletions

View File

@ -27,11 +27,6 @@ class RequestSubscriber implements SubscriberInterface
public function onBefore(BeforeEvent $event) public function onBefore(BeforeEvent $event)
{ {
$request = $event->getRequest(); $request = $event->getRequest();
if ($request->getConfig()['auth'] != 'http-signatures') {
return;
}
$this->context->signer()->sign(new Message($request)); $this->context->signer()->sign(new Message($request));
} }
} }

View File

@ -2,6 +2,10 @@
namespace HttpSignatures\Test; namespace HttpSignatures\Test;
use GuzzleHttp\Client;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Subscriber\History;
use GuzzleHttp\Subscriber\Mock;
use HttpSignatures\GuzzleHttp\Message; use HttpSignatures\GuzzleHttp\Message;
use HttpSignatures\GuzzleHttp\RequestSubscriber; use HttpSignatures\GuzzleHttp\RequestSubscriber;
use HttpSignatures\Context; use HttpSignatures\Context;
@ -14,7 +18,7 @@ class GuzzleHttpSignerTest extends \PHPUnit_Framework_TestCase
private $context; private $context;
/** /**
* @var \GuzzleHttp\Client * @var Client
*/ */
private $client; private $client;
@ -26,9 +30,17 @@ class GuzzleHttpSignerTest extends \PHPUnit_Framework_TestCase
'headers' => array('(request-target)', 'date'), 'headers' => array('(request-target)', 'date'),
)); ));
$this->client = new \GuzzleHttp\Client([ $this->client = new Client();
'auth' => 'http-signatures'
$mock = new Mock([
new Response(200)
]); ]);
$this->client->getEmitter()->attach($mock);
$this->history = new History();
$this->client->getEmitter()->attach($this->history);
$this->client->getEmitter()->attach(new RequestSubscriber($this->context)); $this->client->getEmitter()->attach(new RequestSubscriber($this->context));
} }
@ -37,11 +49,10 @@ class GuzzleHttpSignerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGuzzleRequestHasExpectedHeaders() public function testGuzzleRequestHasExpectedHeaders()
{ {
$message = $this->client->createRequest('GET', '/path?query=123', array( $this->client->get('/path?query=123', array(
'headers' => array('date' => 'today', 'accept' => 'llamas') 'headers' => array('date' => 'today', 'accept' => 'llamas')
)); ));
$message = $this->history->getLastRequest();
$this->context->signer()->sign(new Message($message));
$expectedString = implode( $expectedString = implode(
',', ',',
@ -69,11 +80,10 @@ class GuzzleHttpSignerTest extends \PHPUnit_Framework_TestCase
*/ */
public function testGuzzleRequestHasExpectedHeaders2() public function testGuzzleRequestHasExpectedHeaders2()
{ {
$message = $this->client->createRequest('GET', '/path', array( $this->client->get('/path', array(
'headers' => array('date' => 'today', 'accept' => 'llamas') 'headers' => array('date' => 'today', 'accept' => 'llamas')
)); ));
$message = $this->history->getLastRequest();
$this->context->signer()->sign(new Message($message));
$expectedString = implode( $expectedString = implode(
',', ',',
@ -98,11 +108,10 @@ class GuzzleHttpSignerTest extends \PHPUnit_Framework_TestCase
public function testVerifyGuzzleRequest() public function testVerifyGuzzleRequest()
{ {
$message = $this->client->createRequest('GET', '/path?query=123', array( $this->client->get('/path?query=123', array(
'headers' => array('date' => 'today', 'accept' => 'dogs') 'headers' => array('date' => 'today', 'accept' => 'llamas')
)); ));
$message = $this->history->getLastRequest();
$this->context->signer()->sign(new Message($message));
$this->assertTrue($this->context->verifier()->isValid(new Message($message))); $this->assertTrue($this->context->verifier()->isValid(new Message($message)));
} }