【问题标题】:Symfony2 functional test redirectSymfony2 功能测试重定向
【发布时间】:2014-08-29 16:27:59
【问题描述】:

我正在使用 Symfony2 和 FOSRestBundle,现在我尝试为我的 rest api 编写功能测试。我想发布一个用户名并显示它,类似于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.

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

感谢您的帮助!

【问题讨论】:

    标签: php rest symfony phpunit


    【解决方案1】:

    如果您想生成重定向,您的后端应生成301(“永久”)或302(“临时”)HTTP 状态。 201 is not really a redirect 的状态码,即使您设置了 Location 标头。我认为这取决于客户端实现在 201 + Location 上发生的事情。

    【讨论】:

      猜你喜欢
      • 2016-08-20
      • 1970-01-01
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      相关资源
      最近更新 更多