|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ApiClients\Tests\Tools\Psr7\Oauth1\Signature; |
| 4 | + |
| 5 | +use ApiClients\Tools\Psr7\Oauth1\Definition\ConsumerSecret; |
| 6 | +use ApiClients\Tools\Psr7\Oauth1\Definition\TokenSecret; |
| 7 | +use ApiClients\Tools\Psr7\Oauth1\Signature\HmacSha256Signature; |
| 8 | +use GuzzleHttp\Psr7\Uri; |
| 9 | +use Psr\Http\Message\UriInterface; |
| 10 | + |
| 11 | +class HmacSha256SignatureTest extends \PHPUnit_Framework_TestCase |
| 12 | +{ |
| 13 | + public function testGetMethod() |
| 14 | + { |
| 15 | + $this->assertSame('HMAC-SHA256', (new HmacSha256Signature(new ConsumerSecret('secret')))->getMethod()); |
| 16 | + } |
| 17 | + |
| 18 | + public function provideSign() |
| 19 | + { |
| 20 | + return [ |
| 21 | + [ |
| 22 | + new Uri('https://example.com/'), |
| 23 | + 'x0e2N4wRpr2UB+rX8mwnY01tp6fnXT5SE36kAvbdSYo=', |
| 24 | + 'ovXZ33UaQylAO8L8Ad1f/TwLU1YBXEygau34O7sDSw0=', |
| 25 | + ], |
| 26 | + [ |
| 27 | + new Uri('https://example.com/thea.pot',[], 'THEAPOT'), |
| 28 | + 'u5nNam6Cn+fCko38qAgxWUYmRrPPTF5OFOC9wZ4LHRg=', |
| 29 | + '8IQ1uD3ZR+b8U0Fd++pGGzqNmKiF94z3psbsa+M0SxQ=', |
| 30 | + ], |
| 31 | + [ |
| 32 | + new Uri('https://example.com/?foo=bar', ['foo' => 'bar',]), |
| 33 | + 'SlDm7qfJO/mxl1Ewnewj46jjbuCEKYGNTeew2uMDwPM=', |
| 34 | + '5vVeCSAcL52LVGWWQ1pUFUe7cpVwkhreMQMck3PEw+E=', |
| 35 | + ], |
| 36 | + [ |
| 37 | + new Uri('https://example.com/',['foo' => 'bar',], 'HEAD'), |
| 38 | + 'x0e2N4wRpr2UB+rX8mwnY01tp6fnXT5SE36kAvbdSYo=', |
| 39 | + 'ovXZ33UaQylAO8L8Ad1f/TwLU1YBXEygau34O7sDSw0=', |
| 40 | + ], |
| 41 | + ]; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param UriInterface $uri |
| 46 | + * @param $signedSignature |
| 47 | + * @dataProvider provideSign |
| 48 | + */ |
| 49 | + public function testSign(UriInterface $uri, $signedSignature, $signedTokenSecretSignature) |
| 50 | + { |
| 51 | + $secret = new ConsumerSecret('consumerSecret'); |
| 52 | + $signature = new HmacSha256Signature($secret); |
| 53 | + $tokenSecret = new TokenSecret('tokenSecret'); |
| 54 | + $this->assertSame($signedSignature, $signature->sign($uri)); |
| 55 | + $signature = $signature->withTokenSecret($tokenSecret); |
| 56 | + $this->assertSame($signedTokenSecretSignature, $signature->sign($uri)); |
| 57 | + $signature = $signature->withoutTokenSecret(); |
| 58 | + $this->assertSame($signedSignature, $signature->sign($uri)); |
| 59 | + } |
| 60 | +} |
0 commit comments