【发布时间】:2016-04-27 12:59:34
【问题描述】:
在 symfony 3 控制器文件中我有函数:
/**
* @Route("/user/registration", name="post_registration")
* @Method("POST")
* @return mixed
*/
public function postRegistration()
{
$post = $this->getAllPost();
$this->curl = $this->get('ApiClient');
$responseArray = $this->curl->post(
$this->container->getParameter('api_url') . '/users',
$post
);
if (isset($responseArray['api_key'])) {
return $this->redirectResponseWithCookie($responseArray['api_key']);
} else {
return $this->render(
static::REGISTRATION_TEMPLATE,
['errors' => $responseArray['errors']]
);
}
}
一方面调用函数
redirectResponseWithCookie()
应该重定向页面。
我想测试重定向标头 - 它是否具有正确的位置值。
我在 UserRegistrationContext 类中有函数
class UserRegistrationContext extends BaseContext implements Context, SnippetAcceptingContext
{
/**
* @When I register successfully into the system
* @return null
*/
public function iRegisterSuccessfullyIntoTheSystem()
{
$this->canIntercept();
$this->session->getDriver()->getClient()->followRedirects(false);
// Enters his data
// Posts to www.notification.guru .
$this->session->getDriver()->getClient()
->request('POST', $this->baseUrl . '/user/registration', $this->getFormArray());
echo 'test';
}
}
BaseContext 类只有一些辅助函数,它的构造器初始化会话:
$driver = new GoutteDriver();
$this->session = new Session($driver);
这部分可能有误:
$this->session->getDriver()->getClient()->followRedirects(false);
我刚刚从 behat 2.5 获取代码并尝试调整以使其与 behat 3 一起使用,但不确定它是否正确,但至少不会引发错误。
它应该停止重定向,所以我可以得到一个带有
的响应头getResponseHeaders()
但问题是它尝试重定向,并且代码失败,因为真正的站点还没有在它重定向的地方启动。而且我也无法测试我猜在真正重定向后的标题。
所以我猜重定向必须停止。
如何做到这一点?我找不到信息。
测试失败
$this->session->getDriver()->getClient()
->request('POST', $this->baseUrl . '/user/registration', $this->getFormArray());
【问题讨论】:
-
其实我看到那个代码没有错,运行测试后在控制台看不太清楚。