Skip to content

Commit

Permalink
Improve tests for actually test subscriber is called by Guzzle
Browse files Browse the repository at this point in the history
  • Loading branch information
navitronic committed Oct 19, 2015
1 parent 7873dbf commit fcae83b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
5 changes: 0 additions & 5 deletions src/HttpSignatures/GuzzleHttp/RequestSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ public function getEvents()
public function onBefore(BeforeEvent $event)
{
$request = $event->getRequest();

if ($request->getConfig()['auth'] != 'http-signatures') {
return;
}

$this->context->signer()->sign(new Message($request));
}
}
35 changes: 22 additions & 13 deletions tests/GuzzleHttpSignerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

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\RequestSubscriber;
use HttpSignatures\Context;
Expand All @@ -14,7 +18,7 @@ class GuzzleHttpSignerTest extends \PHPUnit_Framework_TestCase
private $context;

/**
* @var \GuzzleHttp\Client
* @var Client
*/
private $client;

Expand All @@ -26,9 +30,17 @@ public function setUp()
'headers' => array('(request-target)', 'date'),
));

$this->client = new \GuzzleHttp\Client([
'auth' => 'http-signatures'
$this->client = new Client();

$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));
}

Expand All @@ -37,11 +49,10 @@ public function setUp()
*/
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')
));

$this->context->signer()->sign(new Message($message));
$message = $this->history->getLastRequest();

$expectedString = implode(
',',
Expand Down Expand Up @@ -69,11 +80,10 @@ public function testGuzzleRequestHasExpectedHeaders()
*/
public function testGuzzleRequestHasExpectedHeaders2()
{
$message = $this->client->createRequest('GET', '/path', array(
$this->client->get('/path', array(
'headers' => array('date' => 'today', 'accept' => 'llamas')
));

$this->context->signer()->sign(new Message($message));
$message = $this->history->getLastRequest();

$expectedString = implode(
',',
Expand All @@ -98,11 +108,10 @@ public function testGuzzleRequestHasExpectedHeaders2()

public function testVerifyGuzzleRequest()
{
$message = $this->client->createRequest('GET', '/path?query=123', array(
'headers' => array('date' => 'today', 'accept' => 'dogs')
$this->client->get('/path?query=123', array(
'headers' => array('date' => 'today', 'accept' => 'llamas')
));

$this->context->signer()->sign(new Message($message));
$message = $this->history->getLastRequest();

$this->assertTrue($this->context->verifier()->isValid(new Message($message)));
}
Expand Down

0 comments on commit fcae83b

Please sign in to comment.