0
Good morning.
Tldr:我想,我需要的Symfony测试客户端类似的东西,以js的XHR设置:withcredentals:真。symfony的测试 - 在新的请求验证不工作
Symfony 3.1. I have action in rest api controller:
/**
* @Rest\GET("/get-stuff")
* @Rest\View
* @Security("is_granted('IS_AUTHENTICATED_FULLY')")
*/
public function GetStuffAction($userId)
{
// get userId from session
$userId = $this->getUser()->getId();
$em = $this->getDoctrine()->getManager();
$Stuff = $em->getRepository('MyApiBundle:Stuff')->findBy(['user' => $userId]);
return $this->viewGroup($Stuff, ['stuff']);
}
它确实工作正确。
现在我想测试,所以我有:
class TestCase extends WebTestCase
{
public function testIndex()
{
$this->client = self::createClient();
$this->storage = new MockFileSessionStorage(__dir__.'/../../../../app/cache/test/sessions');
$this->session = new Session($this->storage);
$this->logIn($this->getUser(), new Response());
$this->client->request('GET', '/get-stuff');
$content = $this->client->getResponse()->getContent();
$this->assertEquals({"message":"Full authentication is required to access this resource.","code":0},$content);
}
public function logIn(User $user, Response $response)
{
$this->session->start();
$this->cookie = new Cookie('MOCKSESSID', $this->storage->getId());
$this->cookieJar = new CookieJar();
$this->cookieJar->set($this->cookie);
$this->token = new UsernamePasswordToken($user, 'user', 'main', $user->getRoles());
$this->session->set('_security_main', serialize($this->token));
$this->getSecurityManager()->loginUser(
$this->container->getParameter('fos_user.firewall_name'),
$user,
$response
);
$this->session->save();
}
}
并测试它给我留言:“全认证才能访问该资源”。在方法logIn()之后,我可以读取与登录用户连接的会话数据。 我怎样才能让这部分中记录到没有收到消息有关身份验证?:
$this->client->request('GET', '/get-stuff');
$content = $this->client->getResponse()->getContent();
更多细节: 1.我的测试类扩展WebTestCase。 2.我的用户被FosUserBundle覆盖。
我想这就像在javascript中: var xhr = new XMLHttpRequest(); xhr.withCredentials = true; 但我不知道是什么。
预先感谢您帮助解决我的问题!