http-signatures-guzzlehttp/tests/GuzzleHttpSignerTest.php

150 lines
3.9 KiB
PHP
Raw Normal View History

2014-10-06 08:21:26 +00:00
<?php
namespace HttpSignatures\Test;
use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
2014-10-06 08:21:26 +00:00
use HttpSignatures\Context;
use HttpSignatures\GuzzleHttpSignatures;
2014-10-06 08:21:26 +00:00
class GuzzleHttpSignerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Context
*/
private $context;
/**
* @var Client
2014-10-06 08:21:26 +00:00
*/
private $client;
/**
* @var
*/
private $history = [];
2014-10-06 08:21:26 +00:00
public function setUp()
{
$this->context = new Context([
'keys' => ['pda' => 'secret'],
2014-10-06 08:21:26 +00:00
'algorithm' => 'hmac-sha256',
'headers' => ['(request-target)', 'date'],
2014-10-06 08:21:26 +00:00
]);
$stack = new HandlerStack();
$stack->setHandler(new MockHandler([
new Response(200, ['Content-Length' => 0]),
]));
$stack->push(GuzzleHttpSignatures::middlewareFromContext($this->context));
$stack->push(Middleware::history($this->history));
$this->client = new Client(['handler' => $stack]);
2014-10-06 08:21:26 +00:00
}
/**
* test signing a message
*/
public function testGuzzleRequestHasExpectedHeaders()
{
$this->client->get('/path?query=123', [
'headers' => ['date' => 'today', 'accept' => 'llamas']
]);
// get last request
$message = end($this->history);
/** @var Request $request */
$request = $message['request'];
/** @var Response $response */
$response = $message['request'];
2014-10-06 08:21:26 +00:00
$expectedString = implode(
',',
[
2014-10-06 08:21:26 +00:00
'keyId="pda"',
'algorithm="hmac-sha256"',
'headers="(request-target) date"',
'signature="SFlytCGpsqb/9qYaKCQklGDvwgmrwfIERFnwt+yqPJw="',
]
2014-10-06 08:21:26 +00:00
);
$this->assertEquals(
[$expectedString],
$request->getHeader('Signature')
2014-10-06 08:21:26 +00:00
);
$this->assertEquals(
['Signature ' . $expectedString],
$request->getHeader('Authorization')
2014-10-06 08:21:26 +00:00
);
}
/**
* test signing a message with a URL that doesn't contain a ?query
*/
public function testGuzzleRequestHasExpectedHeaders2()
{
$this->client->get('/path', [
'headers' => ['date' => 'today', 'accept' => 'llamas']
]);
// get last request
$message = end($this->history);
/** @var Request $request */
$request = $message['request'];
/** @var Response $response */
$response = $message['request'];
2014-10-06 08:21:26 +00:00
$expectedString = implode(
',',
[
2014-10-06 08:21:26 +00:00
'keyId="pda"',
'algorithm="hmac-sha256"',
'headers="(request-target) date"',
'signature="DAtF133khP05pS5Gh8f+zF/UF7mVUojMj7iJZO3Xk4o="',
]
2014-10-06 08:21:26 +00:00
);
$this->assertEquals(
[$expectedString],
$request->getHeader('Signature')
2014-10-06 08:21:26 +00:00
);
$this->assertEquals(
['Signature ' . $expectedString],
$request->getHeader('Authorization')
2014-10-06 08:21:26 +00:00
);
}
public function getVerifyGuzzleRequestVectors() {
return [
/* path, headers */
['/path?query=123', ['date' => 'today', 'accept' => 'llamas']],
['/path?z=zebra&a=antelope', ['date' => 'today']],
];
}
/**
* @dataProvider getVerifyGuzzleRequestVectors
* @param string $path
* @param array $headers
*/
public function testVerifyGuzzleRequest($path, $headers)
2014-10-06 08:21:26 +00:00
{
$this->client->get($path, ['headers' => $headers]);
// get last request
$message = end($this->history);
/** @var Request $request */
$request = $message['request'];
/** @var Response $response */
$response = $message['request'];
2014-10-06 08:21:26 +00:00
$this->assertTrue($this->context->verifier()->isValid($request));
2014-10-06 08:21:26 +00:00
}
}