[英]Symfony2 functional test prints out redirect html and stops test execution
[英]Symfony2 functional test redirect
我正在使用Symfony2和FOSRestBundle,現在嘗試為我的其余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.
為什么不重定向? 我的錯誤在哪里?
感謝您的任何幫助!
如果要生成重定向,則后端應生成301
(“永久”)或302
(“臨時”)HTTP狀態。 即使您設置了Location
標頭,狀態碼201
也不是真正的重定向 。 我認為這取決於客戶端的實現,在201
+ Location
上會發生什么。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.