asPlankBridge
asPlankBridge

Reputation: 1082

Symfony2 functional test redirect

I am working with Symfony2 and the FOSRestBundle and I now try to write a functional test for my rest api. I want to POST a user name and display it, similar to http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html. I.e., my controller returns a View::createRedirect([...],Codes::HTTP_CREATED) instance.

Now, my test looks like:

[...]
    $client = static::createClient(array('debug'=>true));
    $request = $client->request('POST', '/names', 
                      array(),
                      array(),
                      array('CONTENT_TYPE' => 'application/json'),
                      $expression);

    $response = $client->getResponse();
    \print_r($response);
    $this->assertEquals($response->getStatusCode(), 201);
    $response = $client->followRedirect();
    \print_r($response);

When I run it, I get this output:

Symfony\Component\HttpFoundation\Response Object
(
[...]
                [location] => Array
                    (
                        [0] => /names/90
                    )
[...]

There was 1 error:

1) MYNAME\MyBundle\Tests\Controller\RestControllerTest::testAddUser
LogicException: The request was not redirected.

Why wasn't it redirected? Where is my mistake?

Thank you for any help!

Upvotes: 0

Views: 1637

Answers (1)

lxg
lxg

Reputation: 13127

If you want to generate a redirect, your backend should generate a 301 (“permanent”) or 302 (“temporary”) HTTP status. A status code of 201 is not really a redirect, even if you set a Location header. I think it depends on the client implementation what happens on a 201 + Location.

Upvotes: 0

Related Questions