AuthenticationTest.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\HTTP\Test\Unit;
  7. class AuthenticationTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @param array $server
  11. * @param string $expectedLogin
  12. * @param string $expectedPass
  13. * @dataProvider getCredentialsDataProvider
  14. */
  15. public function testGetCredentials($server, $expectedLogin, $expectedPass)
  16. {
  17. $request = $this->createMock(\Magento\Framework\App\Request\Http::class);
  18. $request->expects($this->once())->method('getServerValue')->will($this->returnValue($server));
  19. $response = $this->createMock(\Magento\Framework\App\Response\Http::class);
  20. $authentication = new \Magento\Framework\HTTP\Authentication($request, $response);
  21. $this->assertSame([$expectedLogin, $expectedPass], $authentication->getCredentials());
  22. }
  23. /**
  24. * @return array
  25. */
  26. public function getCredentialsDataProvider()
  27. {
  28. $login = 'login';
  29. $password = 'password';
  30. $header = 'Basic bG9naW46cGFzc3dvcmQ=';
  31. $anotherLogin = 'another_login';
  32. $anotherPassword = 'another_password';
  33. $anotherHeader = 'Basic YW5vdGhlcl9sb2dpbjphbm90aGVyX3Bhc3N3b3Jk';
  34. return [
  35. [[], '', ''],
  36. [['REDIRECT_HTTP_AUTHORIZATION' => $header], $login, $password],
  37. [['HTTP_AUTHORIZATION' => $header], $login, $password],
  38. [['Authorization' => $header], $login, $password],
  39. [
  40. [
  41. 'REDIRECT_HTTP_AUTHORIZATION' => $header,
  42. 'PHP_AUTH_USER' => $anotherLogin,
  43. 'PHP_AUTH_PW' => $anotherPassword,
  44. ],
  45. $anotherLogin,
  46. $anotherPassword
  47. ],
  48. [
  49. [
  50. 'REDIRECT_HTTP_AUTHORIZATION' => $header,
  51. 'PHP_AUTH_USER' => $anotherLogin,
  52. 'PHP_AUTH_PW' => $anotherPassword,
  53. ],
  54. $anotherLogin,
  55. $anotherPassword
  56. ],
  57. [
  58. ['REDIRECT_HTTP_AUTHORIZATION' => $header, 'HTTP_AUTHORIZATION' => $anotherHeader],
  59. $anotherLogin,
  60. $anotherPassword
  61. ]
  62. ];
  63. }
  64. public function testSetAuthenticationFailed()
  65. {
  66. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  67. $request = $objectManager->getObject(\Magento\Framework\App\Request\Http::class);
  68. $response = $objectManager->getObject(\Magento\Framework\App\Response\Http::class);
  69. $authentication = $objectManager->getObject(
  70. \Magento\Framework\HTTP\Authentication::class,
  71. [
  72. 'httpRequest' => $request,
  73. 'httpResponse' => $response
  74. ]
  75. );
  76. $realm = uniqid();
  77. $authentication->setAuthenticationFailed($realm);
  78. $headers = $response->getHeaders();
  79. $this->assertTrue($headers->has('WWW-Authenticate'));
  80. $header = $headers->get('WWW-Authenticate');
  81. $this->assertEquals('Basic realm="' . $realm . '"', $header->current()->getFieldValue());
  82. $this->assertContains('401', $response->getBody());
  83. }
  84. }