ZFL
ZFL

Reputation: 53

Symfony 4 - Functional Test - Client not following redirects though followRedirects is true

I have been trying to start implementing some functionnal tests in the Symfony 4 application I am working on and I am failing to be redirected and eventually get a 200 response code from my index page even though I get a 200 response in the end when accessing it through the local symfony server.

Here is the test in question

class MainControllerTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();
        $client->request('GET', '/fr');
        $client->followRedirects(true);
        var_dump($client->isFollowingRedirects());
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
    }
}

and here is the phpunit output:

#!/usr/bin/env php
PHPUnit 6.5.14 by Sebastian Bergmann and contributors.

Testing Project Test Suite
Fbool(true)
...                                                                4 / 4 (100%)

Time: 888 ms, Memory: 22.00MB

There was 1 failure:

1) App\Tests\Controller\MainControllerTest::testIndex
Failed asserting that 301 matches expected 200.

FollowRedirects is indeed set to true, yet I still get a 301 response when I should get a 200.

Thanks in advance for any advice or answer on how to solve this issue :)

Upvotes: 5

Views: 2453

Answers (1)

Jakumi
Jakumi

Reputation: 8374

followRedirects is a setting. It is used for every request that is done afterwards.

so setting that after you've done your request is ... well ... useless.

so ... change

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

to

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

and it should work.

Upvotes: 5

Related Questions