SecuredRedirectResponseTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\Tests\Component\HttpFoundation\SecuredRedirectResponseTest.
  5. */
  6. namespace Drupal\Tests\Component\HttpFoundation;
  7. use Drupal\Component\HttpFoundation\SecuredRedirectResponse;
  8. use PHPUnit\Framework\TestCase;
  9. use Symfony\Component\HttpFoundation\Cookie;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. /**
  12. * Test secure redirect base class.
  13. *
  14. * @group Routing
  15. * @coversDefaultClass \Drupal\Component\HttpFoundation\SecuredRedirectResponse
  16. */
  17. class SecuredRedirectResponseTest extends TestCase {
  18. /**
  19. * Test copying of redirect response.
  20. *
  21. * @covers ::createFromRedirectResponse
  22. * @covers ::fromResponse
  23. */
  24. public function testRedirectCopy() {
  25. $redirect = new RedirectResponse('/magic_redirect_url', 301, ['x-cache-foobar' => 123]);
  26. $redirect->setProtocolVersion('2.0');
  27. $redirect->setCharset('ibm-943_P14A-2000');
  28. $redirect->headers->setCookie(new Cookie('name', 'value'));
  29. // Make a cloned redirect.
  30. $secureRedirect = SecuredRedirectStub::createFromRedirectResponse($redirect);
  31. $this->assertEquals('/magic_redirect_url', $secureRedirect->getTargetUrl());
  32. $this->assertEquals(301, $secureRedirect->getStatusCode());
  33. // We pull the headers from the original redirect because there are default headers applied.
  34. $headers1 = $redirect->headers->allPreserveCase();
  35. $headers2 = $secureRedirect->headers->allPreserveCase();
  36. // We unset cache headers so we don't test arcane Symfony weirdness.
  37. // https://github.com/symfony/symfony/issues/16171
  38. unset($headers1['Cache-Control'], $headers2['Cache-Control']);
  39. $this->assertEquals($headers1, $headers2);
  40. $this->assertEquals('2.0', $secureRedirect->getProtocolVersion());
  41. $this->assertEquals('ibm-943_P14A-2000', $secureRedirect->getCharset());
  42. $this->assertEquals($redirect->headers->getCookies(), $secureRedirect->headers->getCookies());
  43. }
  44. }
  45. class SecuredRedirectStub extends SecuredRedirectResponse {
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function isSafe($url) {
  50. // Empty implementation for testing.
  51. return TRUE;
  52. }
  53. }