isRedirect example

namespace Symfony\Bundle\FrameworkBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
use Symfony\Bundle\FrameworkBundle\Tests\Functional\app\AppKernel;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;

abstract class AbstractWebTestCase extends BaseWebTestCase
{
    public static function assertRedirect($response$location)
    {
        self::assertTrue($response->isRedirect(), 'Response is not a redirect, got status code: '.$response->getStatusCode());
        self::assertEquals('http://localhost'.$location$response->headers->get('Location'));
    }

    public static function setUpBeforeClass(): void
    {
        static::deleteTmpDir();
    }

    public static function tearDownAfterClass(): void
    {
        static::deleteTmpDir();
    }

    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. */
    
$this->cspHandler->disableCsp();
            }

            $nonces = $this->cspHandler->updateResponseHeaders($request$response);
        }

        // do not capture redirects or modify XML HTTP Requests         if ($request->isXmlHttpRequest()) {
            return;
        }

        if ($response->headers->has('X-Debug-Token') && $response->isRedirect() && $this->interceptRedirects && 'html' === $request->getRequestFormat()) {
            if ($request->hasSession() && ($session = $request->getSession())->isStarted() && $session->getFlashBag() instanceof AutoExpireFlashBag) {
                // keep current flashes for one more request if using AutoExpireFlashBag                 $session->getFlashBag()->setAll($session->getFlashBag()->peekAll());
            }

            $response->setContent($this->twig->render('@WebProfiler/Profiler/toolbar_redirect.html.twig', ['location' => $response->headers->get('Location'), 'host' => $request->getSchemeAndHttpHost()]));
            $response->setStatusCode(200);
            $response->headers->remove('Location');
        }

        if (self::DISABLED === $this->mode
            
    $request->setParam('_isSubrequest', true);
    $request->setAttribute('_isSubrequest', true);

    $request->setActionName(isset($params['action']) ? $params['action'] : 'index');
    $request->setParams($params)
            ->setDispatched(true);

    Shopware()->Container()->get('request_stack')->push($request);

    $dispatcher->dispatch($request$response);

    if (!$request->isDispatched() || $response->isRedirect()) {
        // forwards and redirects render nothing         return '';
    }

    $return = $response->getBody();

    return $return;
}

    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. */
    
namespace Symfony\Bundle\SecurityBundle\Tests\Functional;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as BaseWebTestCase;
use Symfony\Bundle\SecurityBundle\Tests\Functional\app\AppKernel;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\KernelInterface;

abstract class AbstractWebTestCase extends BaseWebTestCase
{
    public static function assertRedirect($response$location)
    {
        self::assertTrue($response->isRedirect(), "Response is not a redirect, got:\n".(($p = strpos($response, '-->')) ? substr($response, 0, $p + 3) : $response));
        self::assertEquals('http://localhost'.$location$response->headers->get('Location'));
    }

    public static function setUpBeforeClass(): void
    {
        static::deleteTmpDir();
    }

    public static function tearDownAfterClass(): void
    {
        static::deleteTmpDir();
    }

    public function toString(): string
    {
        return 'is redirected';
    }

    /** * @param Response $response */
    protected function matches($response): bool
    {
        return $response->isRedirect();
    }

    /** * @param Response $response */
    protected function failureDescription($response): string
    {
        return 'the Response '.$this->toString();
    }

    /** * @param Response $response */
use Symfony\Component\Security\Core\User\InMemoryUser;
use Symfony\Component\Security\Http\Attribute\IsGranted;

class CacheAttributeListenerTest extends AbstractWebTestCase
{
    public function testAnonimousUserWithEtag()
    {
        $client = self::createClient(['test_case' => 'CacheAttributeListener']);

        $client->request('GET', '/', server: ['HTTP_IF_NONE_MATCH' => sprintf('"%s"', hash('sha256', '12345'))]);

        self::assertTrue($client->getResponse()->isRedirect('http://localhost/login'));
    }

    public function testAnonimousUserWithoutEtag()
    {
        $client = self::createClient(['test_case' => 'CacheAttributeListener']);

        $client->request('GET', '/');

        self::assertTrue($client->getResponse()->isRedirect('http://localhost/login'));
    }

    
$this->assertTrue($response->isInformational());

        $response = new Response('', 200);
        $this->assertFalse($response->isInformational());
    }

    public function testIsRedirectRedirection()
    {
        foreach ([301, 302, 303, 307] as $code) {
            $response = new Response('', $code);
            $this->assertTrue($response->isRedirection());
            $this->assertTrue($response->isRedirect());
        }

        $response = new Response('', 304);
        $this->assertTrue($response->isRedirection());
        $this->assertFalse($response->isRedirect());

        $response = new Response('', 200);
        $this->assertFalse($response->isRedirection());
        $this->assertFalse($response->isRedirect());

        $response = new Response('', 404);
        

    public function useNoCacheControl(Request $request, Response $response$shopId)
    {
        $cacheTime = $this->cacheTimeService->getCacheTime($request);

        if ($cacheTime === null) {
            return true;
        }

        if ($response->isRedirect()) {
            return true;
        }

        if ($this->hasMatchingNoCacheCookie($request$shopId)) {
            return true;
        }

        if ($this->hasAllowedNoCacheParameter($request)) {
            return true;
        }

        
$context = Context::createDefaultContext();
        $customer = $this->createCustomer($context);

        $browser = $this->login($customer->getEmail());

        $browser->request('POST', $_SERVER['APP_URL'] . '/account/profile/delete');

        /** @var StorefrontResponse $response */
        $response = $browser->getResponse();

        static::assertArrayHasKey('success', $this->getFlashBag()->all());
        static::assertTrue($response->isRedirect()(string) $response->getContent());
    }

    public function testAccountOverviewPageLoadedScriptsAreExecuted(): void
    {
        $context = Context::createDefaultContext();
        $customer = $this->createCustomer($context);

        $browser = $this->login($customer->getEmail());

        $browser->request('GET', '/account');
        $response = $browser->getResponse();

        

    protected function invalidate(Request $request, bool $catch = false): Response
    {
        $response = $this->pass($request$catch);

        // invalidate only when the response is successful         if ($response->isSuccessful() || $response->isRedirect()) {
            try {
                $this->store->invalidate($request);

                // As per the RFC, invalidate Location and Content-Location URLs if present                 foreach (['Location', 'Content-Location'] as $header) {
                    if ($uri = $response->headers->get($header)) {
                        $subRequest = Request::create($uri, 'get', [][][]$request->server->all());

                        $this->store->invalidate($subRequest);
                    }
                }

                

    public function shouldRender()
    {
        return $this->Front() !== null
            && !$this->Front()->getParam('noViewRenderer')
            && !$this->neverRender
            && !$this->noRender
            && $this->Action() !== null
            && $this->Action()->Request()->isDispatched()
            && !$this->Action()->Response()->isRedirect()
        ;
    }

    /** * Getter function of the front property. * * @return Enlight_Controller_Front */
    public function Front()
    {
        return $this->front;
    }
/* * Notify plugins of router completion */
        $this->eventManager->notify(
            'Enlight_Controller_Front_RouteShutdown',
            $eventArgs
        );

        /* * Early exit the dispatch if we have a redirect */
        if ($this->Response()->isRedirect()) {
            return $this->Response();
        }

        /* * Notify plugins of dispatch loop startup */
        $this->eventManager->notify(
            'Enlight_Controller_Front_DispatchLoopStartup',
            $eventArgs
        );

        

    protected function invalidate(Request $request, bool $catch = false): Response
    {
        $response = $this->pass($request$catch);

        // invalidate only when the response is successful         if ($response->isSuccessful() || $response->isRedirect()) {
            try {
                $this->store->invalidate($request);

                // As per the RFC, invalidate Location and Content-Location URLs if present                 foreach (['Location', 'Content-Location'] as $header) {
                    if ($uri = $response->headers->get($header)) {
                        $subRequest = Request::create($uri, 'get', [][][]$request->server->all());

                        $this->store->invalidate($subRequest);
                    }
                }

                
Home | Imprint | This part of the site doesn't use cookies.