DigestTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Http\Client\Auth;
  16. use Cake\Http\Client;
  17. use Cake\Http\Client\Auth\Digest;
  18. use Cake\Http\Client\Request;
  19. use Cake\Http\Client\Response;
  20. use Cake\TestSuite\TestCase;
  21. /**
  22. * Digest authentication test
  23. */
  24. class DigestTest extends TestCase
  25. {
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject|\Cake\Http\Client
  28. */
  29. public $client;
  30. /**
  31. * @var \Cake\Http\Client\Auth\Digest
  32. */
  33. public $auth;
  34. /**
  35. * Setup
  36. *
  37. * @return void
  38. */
  39. public function setUp()
  40. {
  41. parent::setUp();
  42. $this->client = $this->getClientMock();
  43. $this->auth = new Digest($this->client);
  44. }
  45. /**
  46. * @return \PHPUnit_Framework_MockObject_MockObject|\Cake\Http\Client
  47. */
  48. protected function getClientMock()
  49. {
  50. return $this->getMockBuilder(Client::class)
  51. ->setMethods(['send'])
  52. ->getMock();
  53. }
  54. /**
  55. * test getting data from additional request method
  56. *
  57. * @return void
  58. */
  59. public function testRealmAndNonceFromExtraRequest()
  60. {
  61. $headers = [
  62. 'WWW-Authenticate: Digest realm="The batcave",nonce="4cded326c6c51"',
  63. ];
  64. $response = new Response($headers, '');
  65. $this->client->expects($this->once())
  66. ->method('send')
  67. ->will($this->returnValue($response));
  68. $auth = ['username' => 'admin', 'password' => '1234'];
  69. $request = new Request('http://example.com/some/path', Request::METHOD_GET);
  70. $request = $this->auth->authentication($request, $auth);
  71. $result = $request->getHeaderLine('Authorization');
  72. $this->assertContains('Digest', $result);
  73. $this->assertContains('realm="The batcave"', $result);
  74. $this->assertContains('nonce="4cded326c6c51"', $result);
  75. $this->assertContains('response="a21a874c0b29165929f5d24d1aad2c47"', $result);
  76. $this->assertContains('uri="/some/path"', $result);
  77. $this->assertNotContains('qop=', $result);
  78. $this->assertNotContains('nc=', $result);
  79. }
  80. /**
  81. * testQop method
  82. *
  83. * @return void
  84. */
  85. public function testQop()
  86. {
  87. $headers = [
  88. 'WWW-Authenticate: Digest realm="The batcave",nonce="4cded326c6c51",qop="auth"',
  89. ];
  90. $response = new Response($headers, '');
  91. $this->client->expects($this->once())
  92. ->method('send')
  93. ->will($this->returnValue($response));
  94. $auth = ['username' => 'admin', 'password' => '1234'];
  95. $request = new Request('http://example.com/some/path', Request::METHOD_GET);
  96. $request = $this->auth->authentication($request, $auth);
  97. $result = $request->getHeaderLine('Authorization');
  98. $this->assertContains('qop="auth"', $result);
  99. $this->assertContains('nc=00000001', $result);
  100. $this->assertRegexp('/cnonce="[a-z0-9]+"/', $result);
  101. }
  102. /**
  103. * testOpaque method
  104. *
  105. * @return void
  106. */
  107. public function testOpaque()
  108. {
  109. $headers = [
  110. 'WWW-Authenticate: Digest realm="The batcave",nonce="4cded326c6c51",opaque="d8ea7aa61a1693024c4cc3a516f49b3c"',
  111. ];
  112. $response = new Response($headers, '');
  113. $this->client->expects($this->once())
  114. ->method('send')
  115. ->will($this->returnValue($response));
  116. $auth = ['username' => 'admin', 'password' => '1234'];
  117. $request = new Request('http://example.com/some/path', Request::METHOD_GET);
  118. $request = $this->auth->authentication($request, $auth);
  119. $result = $request->getHeaderLine('Authorization');
  120. $this->assertContains('opaque="d8ea7aa61a1693024c4cc3a516f49b3c"', $result);
  121. }
  122. }