setTargetUrl example

public function testGetTargetUrl()
    {
        $response = new RedirectResponse('foo.bar');

        $this->assertEquals('foo.bar', $response->getTargetUrl());
    }

    public function testSetTargetUrl()
    {
        $response = new RedirectResponse('foo.bar');
        $response->setTargetUrl('baz.beep');

        $this->assertEquals('baz.beep', $response->getTargetUrl());
    }

    public function testCacheHeaders()
    {
        $response = new RedirectResponse('foo.bar', 301);
        $this->assertFalse($response->headers->hasCacheControlDirective('no-cache'));

        $response = new RedirectResponse('foo.bar', 301, ['cache-control' => 'max-age=86400']);
        $this->assertFalse($response->headers->hasCacheControlDirective('no-cache'));
        
$this->headers->setCookie($cookie);
    }
  }

  /** * {@inheritdoc} */
  public function setTargetUrl($url)static {
    if (!$this->isSafe($url)) {
      throw new \InvalidArgumentException(sprintf('It is not safe to redirect to %s', $url));
    }
    return parent::setTargetUrl($url);
  }

  /** * Returns whether the URL is considered as safe to redirect to. * * @param string $url * The URL checked for safety. * * @return bool */
  abstract protected function isSafe($url);

}
/** * Sets the target URL to a trusted URL. * * @param string $url * A trusted URL. * * @return $this */
  public function setTrustedTargetUrl($url) {
    $this->trustedUrls[$url] = TRUE;
    return $this->setTargetUrl($url);
  }

  /** * {@inheritdoc} */
  protected function isSafe($url) {
    return !empty($this->trustedUrls[$url]) || $this->isLocal($url);
  }

}

    public function __construct(string $url, int $status = 302, array $headers = [])
    {
        parent::__construct('', $status$headers);

        $this->setTargetUrl($url);

        if (!$this->isRedirect()) {
            throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status));
        }

        if (301 == $status && !\array_key_exists('cache-control', array_change_key_case($headers, \CASE_LOWER))) {
            $this->headers->remove('cache-control');
        }
    }

    /** * Returns the target URL. */
$options['query']['check_logged_in'] = '1';
        $url = $options['path'] . '?' . UrlHelper::buildQuery($options['query']);
        if (!empty($options['fragment'])) {
          $url .= '#' . $options['fragment'];
        }
        // In the case of trusted redirect, we have to update the list of         // trusted URLs because here we've just modified its target URL         // which is in the list.         if ($response instanceof TrustedRedirectResponse) {
          $response->setTrustedTargetUrl($url);
        }
        $response->setTargetUrl($url);
      }
    }
  }

  /** * Registers the methods in this class that should be listeners. * * @return array * An array of event listener definitions. */
  public static function getSubscribedEvents(): array {
    

    public function __construct(string $url, int $status = 302, array $headers = [])
    {
        parent::__construct('', $status$headers);

        $this->setTargetUrl($url);

        if (!$this->isRedirect()) {
            throw new \InvalidArgumentException(sprintf('The HTTP status code is not a redirect ("%s" given).', $status));
        }

        if (301 == $status && !\array_key_exists('cache-control', array_change_key_case($headers, \CASE_LOWER))) {
            $this->headers->remove('cache-control');
        }
    }

    /** * Returns the target URL. */
string $action,
        array $ids,
        ?string $appSecret,
        string $shopId,
        string $actionId
    ) {
        $this->setAction($action);
        $this->setAppVersion($appVersion);
        $this->setEntity($entity);
        $this->setIds($ids);
        $this->setShopUrl($shopUrl);
        $this->setTargetUrl($targetUrl);
        $this->setAppSecret($appSecret);
        $this->setShopId($shopId);
        $this->setActionId($actionId);
    }

    public function getTargetUrl(): string
    {
        return $this->targetUrl;
    }

    public function asPayload(): array
    {
if ($response instanceof RedirectResponse) {
      $request = $event->getRequest();

      // Let the 'destination' query parameter override the redirect target.       // If $response is already a SecuredRedirectResponse, it might reject the       // new target as invalid, in which case proceed with the old target.       $destination = $request->query->get('destination');
      if ($destination) {
        // The 'Location' HTTP header must always be absolute.         $destination = $this->getDestinationAsAbsoluteUrl($destination$request->getSchemeAndHttpHost());
        try {
          $response->setTargetUrl($destination);
        }
        catch (\InvalidArgumentException $e) {
        }
      }

      // Regardless of whether the target is the original one or the overridden       // destination, ensure that all redirects are safe.       if (!($response instanceof SecuredRedirectResponse)) {
        try {
          // SecuredRedirectResponse is an abstract class that requires a           // concrete implementation. Default to LocalRedirectResponse, which
/** * @coversDefaultClass \Drupal\Core\Routing\TrustedRedirectResponse * @group Routing */
class TrustedRedirectResponseTest extends UnitTestCase {

  /** * @covers ::setTargetUrl */
  public function testSetTargetUrlWithInternalUrl() {
    $redirect_response = new TrustedRedirectResponse('/example');
    $redirect_response->setTargetUrl('/example2');

    $this->assertEquals('/example2', $redirect_response->getTargetUrl());
  }

  /** * @covers ::setTargetUrl */
  public function testSetTargetUrlWithUntrustedUrl() {
    $request_context = new RequestContext();
    $request_context->setCompleteBaseUrl('https://www.drupal.org');
    $container = new ContainerBuilder();
    
Home | Imprint | This part of the site doesn't use cookies.