Skip to content

Commit

Permalink
unit tests for signature generation
Browse files Browse the repository at this point in the history
  • Loading branch information
ekhaled committed Apr 2, 2014
1 parent 1e81eb8 commit 62be39f
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/unit/methodTest.php
@@ -0,0 +1,70 @@
<?php

class methodTest extends baseTest
{

private $reflected;

public function _before()
{
parent::_before();
$this->reflected = new \ReflectionClass($this->V);
}

public function _after()
{
parent::_after();
}

public function testSignature()
{
$request_time = date('c');
$resource = array(
'method' => 'GET',
'endpoint' => '/v1/resourceName',
'data' => ''
);
$secret = '111111';
$this->V->setSecret($secret);

$this->assertSame(
$this->getSignature($request_time, $resource['method'], $resource['endpoint'], $secret),
$this->call('getSignature', $resource, $request_time)
);

//not a GET request, so signature should be the same
$resource['data'] = array('foo' => 'bar', 'bar' => 'foo');
$nonGet = array('POST', 'DELETE', 'PUT');
foreach ($nonGet as $meth) {
$resource['method'] = $meth;
$this->assertSame(
$this->getSignature($request_time, $resource['method'], $resource['endpoint'], $secret),
$this->call('getSignature', $resource, $request_time)
);
}

//for a GET request signature includes the query params
$resource['method'] = 'GET';
$this->assertSame(
$this->getSignature($request_time, $resource['method'], $resource['endpoint'] . "?foo=bar&bar=foo", $secret),
$this->call('getSignature', $resource, $request_time)
);

}


private function getSignature($request_time, $method, $endpoint, $secret)
{
$token = preg_replace("/\s+/", "", $request_time) . $method . $endpoint;

return hash_hmac("sha256", $token, $secret);
}

private function call($method, $arg1 = '', $arg2 = '', $arg3 = '')
{
$method = $this->reflected->getMethod($method);
$method->setAccessible(true);
return $method->invoke($this->V, $arg1, $arg2, $arg3);
}

}

0 comments on commit 62be39f

Please sign in to comment.