zozo
zozo

Reputation: 8582

symfony 2 - request follow redirect

I'm writing some tests to check if all pages from a site are ok.

The relevant base code is this:

function pagesUpTest() {
   static::bootKernel($options);

   $client = static::$kernel->getContainer()->get('test.client');
   $client->setServerParameters($server);

   $crawler = $client->request('GET', $url);

   $this->assertTrue($client->getResponse()->isSuccessful());
}

Everything works ok for normal pages.

Still, there are a few pages that make redirect (some old articles that make redirects toward their newer and better versions - are dynamic and more can appear later so they cannot be treated separately).

Right now I just added a condition to skip asserting if is successful on redirects like this:

function pagesUpTest() {
   static::bootKernel($options);

   $client = static::$kernel->getContainer()->get('test.client');
   $client->setServerParameters($server);

   $crawler = $client->request('GET', $url);

   if (!$client->getResponse()->isRedirection()) {
      $this->assertTrue($client->getResponse()->isSuccessful());
   }
}

Still... this is assuming that after redirect everything is ok, so the test becomes less relevant for those cases. I want to follow the redirect, and check if THAT page is successful.

Any way I can do this?

Upvotes: 4

Views: 3072

Answers (1)

Matteo
Matteo

Reputation: 39380

You can simply follow the redirect in case of redirection as follow:

if ($client->getResponse()->isRedirection()) {
    $client->followRedirect();
}
$this->assertTrue($client->getResponse()->isSuccessful());

Seems possibile to tell to the client to follow the redirection by itself with the method:

$client->followRedirects(true);
$client->setMaxRedirects(10);

So the client follow the redirection by itself (But i never try it :))

Hope this help

Upvotes: 6

Related Questions