DigestTest.php 3.8 KB

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