如何使用 DunglasAngularCsrfBundle 测试 Symfony2 api

How to test Symfony2 api with DunglasAngularCsrfBundle

我将 Symfony2 与 DunglasAngularCsrfBundle 一起使用,当我 运行 我的 PHpunit 测试时出现错误 {"code":403,"message":"Bad CSRF token."}

如果用户使用 http basic

self::$user_client = static::createClient(array(), array(
    'PHP_AUTH_USER' => 'user1@mail.com',
    'PHP_AUTH_PW' => 'user1',
    'HTTP_HOST' => static::getHost()
));

此条件 return 在 EventListener\AngularCsrfValidationListener

中为假
if (!$value || !$this->angularCsrfTokenManager->isTokenValid($value)) {
        throw new AccessDeniedHttpException('Bad CSRF token.');
    }

测试中生成的令牌不相等,我尝试这样的另一个身份验证:

protected function logIn($email)
{
$this->client = static::createClient();

$user = $this->client->getContainer()
    ->get('doctrine')
    ->getManager()
    ->getRepository('UserBundle:User')
    ->findOneByEmail($email);

$providerKey = static::$kernel->getContainer()->getParameter('fos_user.firewall_name');
$token = new UsernamePasswordToken($user, null, $providerKey, $user->getRoles());


$session = $this->client->getContainer()->get('session');
$session->set('_security_'.$providerKey, serialize($token));
$session->save();
$cookie = new Cookie($session->getName(), $session->getId());

$this->client->getCookieJar()->set($cookie);

}

未创建令牌。 Return 在 Symfony\Component\Security\Csrf\CsrfTokenManager::isTokenValid:

中为假
 $this->storage->hasToken($token->getId())

请帮忙解决这个问题,谢谢

已解决

self::$client->request('GET', '/') 

在 cookie 中设置令牌