DigestAuthenticationEntryPointTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace Symfony\Tests\Component\Security\Http\EntryPoint;
  3. use Symfony\Component\Security\Http\EntryPoint\DigestAuthenticationEntryPoint;
  4. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  5. use Symfony\Component\Security\Core\Exception\NonceExpiredException;
  6. class DigestAuthenticationEntryPointTest extends \PHPUnit_Framework_TestCase
  7. {
  8. public function testStart()
  9. {
  10. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  11. $authenticationException = new AuthenticationException('TheAuthenticationExceptionMessage');
  12. $entryPoint = new DigestAuthenticationEntryPoint('TheRealmName', 'TheKey');
  13. $response = $entryPoint->start($request, $authenticationException);
  14. $this->assertEquals(401, $response->getStatusCode());
  15. $this->assertAttributeEquals('TheAuthenticationExceptionMessage', 'statusText', $response);
  16. $this->assertRegExp('/^Digest realm="TheRealmName", qop="auth", nonce="[a-zA-Z0-9\/+]+={0,2}"$/', $response->headers->get('WWW-Authenticate'));
  17. }
  18. public function testStartWithNoException()
  19. {
  20. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  21. $entryPoint = new DigestAuthenticationEntryPoint('TheRealmName', 'TheKey');
  22. $response = $entryPoint->start($request);
  23. $this->assertEquals(401, $response->getStatusCode());
  24. $this->assertAttributeEquals('Unauthorized', 'statusText', $response);
  25. $this->assertRegExp('/^Digest realm="TheRealmName", qop="auth", nonce="[a-zA-Z0-9\/+]+={0,2}"$/', $response->headers->get('WWW-Authenticate'));
  26. }
  27. public function testStartWithNonceExpiredException()
  28. {
  29. $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
  30. $nonceExpiredException = new NonceExpiredException('TheNonceExpiredExceptionMessage');
  31. $entryPoint = new DigestAuthenticationEntryPoint('TheRealmName', 'TheKey');
  32. $response = $entryPoint->start($request, $nonceExpiredException);
  33. $this->assertEquals(401, $response->getStatusCode());
  34. $this->assertAttributeEquals('TheNonceExpiredExceptionMessage', 'statusText', $response);
  35. $this->assertRegExp('/^Digest realm="TheRealmName", qop="auth", nonce="[a-zA-Z0-9\/+]+={0,2}", stale="true"$/', $response->headers->get('WWW-Authenticate'));
  36. }
  37. }