2014-08-29 62 views
0

我正在使用Symfony2和FOSRestBundle,现在我尝试为我的其余api编写功能测试。我想发布一个用户名并显示它,类似于http://npmasters.com/2012/11/25/Symfony2-Rest-FOSRestBundle.html。即,我的控制器返回一个View::createRedirect([...],Codes::HTTP_CREATED)实例。Symfony2功能测试重定向

现在,我的测试是这样的:

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

为什么不是重定向? 我的错误在哪里?

谢谢你的帮助!

回答

0

如果要生成重定向,则后端应生成301(“永久”)或302(“临时”)HTTP状态。状态代码201is not really a redirect,即使您设置了Location标题。我认为这取决于客户端实施201 + Location上发生了什么。