UnauthorizedHttpExceptionTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\HttpKernel\Tests\Exception;
  11. use Symfony\Component\HttpKernel\Exception\HttpException;
  12. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  13. class UnauthorizedHttpExceptionTest extends HttpExceptionTest
  14. {
  15. public function testHeadersDefault()
  16. {
  17. $exception = new UnauthorizedHttpException('Challenge');
  18. $this->assertSame(['WWW-Authenticate' => 'Challenge'], $exception->getHeaders());
  19. }
  20. public function testWithHeaderConstruct()
  21. {
  22. $headers = [
  23. 'Cache-Control' => 'public, s-maxage=1200',
  24. ];
  25. $exception = new UnauthorizedHttpException('Challenge', '', null, 0, $headers);
  26. $headers['WWW-Authenticate'] = 'Challenge';
  27. $this->assertSame($headers, $exception->getHeaders());
  28. }
  29. /**
  30. * @dataProvider headerDataProvider
  31. */
  32. public function testHeadersSetter($headers)
  33. {
  34. $exception = new UnauthorizedHttpException('Challenge');
  35. $exception->setHeaders($headers);
  36. $this->assertSame($headers, $exception->getHeaders());
  37. }
  38. protected function createException(string $message = '', \Throwable $previous = null, int $code = 0, array $headers = []): HttpException
  39. {
  40. return new UnauthorizedHttpException('Challenge', $message, $previous, $code, $headers);
  41. }
  42. }