Symfony2功能测试重定向


Symfony2 functional test redirect

我正在使用Symfony2和FOSRestBundle,现在我试图为我的rest api编写一个功能测试。我想POST用户名并显示它,类似于http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html.也就是说,我的控制器返回一个View::createRedirect([...],Codes::HTTP_CREATED)实例。

现在,我的测试看起来像:

[...]
    $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);

当我运行它时,我得到的输出是:

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.

为什么没有重定向?我的错误在哪里?

谢谢你的帮助!

如果要生成重定向,后端应生成301("永久")或302("临时")HTTP状态。即使设置了Location标头,201的状态代码也不是真正的重定向。我认为这取决于客户端实现201+Location上发生的情况。