DigestTest.php 4.2 KB

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