HmacSha1SignatureTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace League\OAuth1\Client\Tests;
  3. use League\OAuth1\Client\Signature\HmacSha1Signature;
  4. use Mockery as m;
  5. use PHPUnit\Framework\TestCase;
  6. class HmacSha1SignatureTest extends TestCase
  7. {
  8. protected function tearDown(): void
  9. {
  10. m::close();
  11. parent::tearDown();
  12. }
  13. public function testSigningRequest()
  14. {
  15. $signature = new HmacSha1Signature($this->getMockClientCredentials());
  16. $uri = 'http://www.example.com/?qux=corge';
  17. $parameters = ['foo' => 'bar', 'baz' => null];
  18. $this->assertEquals('A3Y7C1SUHXR1EBYIUlT3d6QT1cQ=', $signature->sign($uri, $parameters));
  19. }
  20. public function testSigningRequestWhereThePortIsNotStandard()
  21. {
  22. $signature = new HmacSha1Signature($this->getMockClientCredentials());
  23. $uri = 'http://www.example.com:8080/?qux=corge';
  24. $parameters = ['foo' => 'bar', 'baz' => null];
  25. $this->assertEquals('ECcWxyi5UOC1G0MxH0ygm6Pd6JE=', $signature->sign($uri, $parameters));
  26. }
  27. public function testQueryStringFromArray()
  28. {
  29. $array = ['a' => 'b'];
  30. $res = $this->invokeQueryStringFromData($array);
  31. $this->assertSame(
  32. 'a%3Db',
  33. $res
  34. );
  35. }
  36. public function testQueryStringFromIndexedArray()
  37. {
  38. $array = ['a', 'b'];
  39. $res = $this->invokeQueryStringFromData($array);
  40. $this->assertSame(
  41. '0%3Da%261%3Db',
  42. $res
  43. );
  44. }
  45. public function testQueryStringFromMultiDimensionalArray()
  46. {
  47. $array = [
  48. 'a' => [
  49. 'b' => [
  50. 'c' => 'd',
  51. ],
  52. 'e' => [
  53. 'f' => 'g',
  54. ],
  55. ],
  56. 'h' => 'i',
  57. 'empty' => '',
  58. 'null' => null,
  59. 'false' => false,
  60. ];
  61. // Convert to query string.
  62. $res = $this->invokeQueryStringFromData($array);
  63. $this->assertSame(
  64. 'a%5Bb%5D%5Bc%5D%3Dd%26a%5Be%5D%5Bf%5D%3Dg%26h%3Di%26empty%3D%26null%3D%26false%3D',
  65. $res
  66. );
  67. // Reverse engineer the string.
  68. $res = urldecode($res);
  69. $this->assertSame(
  70. 'a[b][c]=d&a[e][f]=g&h=i&empty=&null=&false=',
  71. $res
  72. );
  73. // Finally, parse the string back to an array.
  74. parse_str($res, $original_array);
  75. // And ensure it matches the orignal array (approximately).
  76. $this->assertSame(
  77. [
  78. 'a' => [
  79. 'b' => [
  80. 'c' => 'd',
  81. ],
  82. 'e' => [
  83. 'f' => 'g',
  84. ],
  85. ],
  86. 'h' => 'i',
  87. 'empty' => '',
  88. 'null' => '', // null value gets lost in string translation
  89. 'false' => '', // false value gets lost in string translation
  90. ],
  91. $original_array
  92. );
  93. }
  94. public function testSigningRequestWithMultiDimensionalParams()
  95. {
  96. $signature = new HmacSha1Signature($this->getMockClientCredentials());
  97. $uri = 'http://www.example.com/';
  98. $parameters = [
  99. 'a' => [
  100. 'b' => [
  101. 'c' => 'd',
  102. ],
  103. 'e' => [
  104. 'f' => 'g',
  105. ],
  106. ],
  107. 'h' => 'i',
  108. 'empty' => '',
  109. 'null' => null,
  110. 'false' => false,
  111. ];
  112. $this->assertEquals('ZUxiJKugeEplaZm9e4hshN0I70U=', $signature->sign($uri, $parameters));
  113. }
  114. protected function invokeQueryStringFromData(array $args)
  115. {
  116. $signature = new HmacSha1Signature(m::mock('League\OAuth1\Client\Credentials\ClientCredentialsInterface'));
  117. $refl = new \ReflectionObject($signature);
  118. $method = $refl->getMethod('queryStringFromData');
  119. $method->setAccessible(true);
  120. return $method->invokeArgs($signature, [$args]);
  121. }
  122. protected function getMockClientCredentials()
  123. {
  124. $clientCredentials = m::mock('League\OAuth1\Client\Credentials\ClientCredentialsInterface');
  125. $clientCredentials->shouldReceive('getSecret')->andReturn('clientsecret');
  126. return $clientCredentials;
  127. }
  128. }