DigestAuthenticationEntryPointTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Security\Http\Tests\EntryPoint;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  13. use Symfony\Component\Security\Core\Exception\NonceExpiredException;
  14. use Symfony\Component\Security\Http\EntryPoint\DigestAuthenticationEntryPoint;
  15. class DigestAuthenticationEntryPointTest extends TestCase
  16. {
  17. public function testStart()
  18. {
  19. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
  20. $authenticationException = new AuthenticationException('TheAuthenticationExceptionMessage');
  21. $entryPoint = new DigestAuthenticationEntryPoint('TheRealmName', 'TheSecret');
  22. $response = $entryPoint->start($request, $authenticationException);
  23. $this->assertEquals(401, $response->getStatusCode());
  24. $this->assertRegExp('/^Digest realm="TheRealmName", qop="auth", nonce="[a-zA-Z0-9\/+]+={0,2}"$/', $response->headers->get('WWW-Authenticate'));
  25. }
  26. public function testStartWithNoException()
  27. {
  28. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
  29. $entryPoint = new DigestAuthenticationEntryPoint('TheRealmName', 'TheSecret');
  30. $response = $entryPoint->start($request);
  31. $this->assertEquals(401, $response->getStatusCode());
  32. $this->assertRegExp('/^Digest realm="TheRealmName", qop="auth", nonce="[a-zA-Z0-9\/+]+={0,2}"$/', $response->headers->get('WWW-Authenticate'));
  33. }
  34. public function testStartWithNonceExpiredException()
  35. {
  36. $request = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')->getMock();
  37. $nonceExpiredException = new NonceExpiredException('TheNonceExpiredExceptionMessage');
  38. $entryPoint = new DigestAuthenticationEntryPoint('TheRealmName', 'TheSecret');
  39. $response = $entryPoint->start($request, $nonceExpiredException);
  40. $this->assertEquals(401, $response->getStatusCode());
  41. $this->assertRegExp('/^Digest realm="TheRealmName", qop="auth", nonce="[a-zA-Z0-9\/+]+={0,2}", stale="true"$/', $response->headers->get('WWW-Authenticate'));
  42. }
  43. }