[英]symfony2 test client request with post data
我想在symfony web應用上測試使用facebook的登錄,我創建了一個測試用戶並請求該用戶的訪問令牌,我想發送以下發布請求:
public function setup()
{
$this->access_token = 'CAAL0aG1XXXXXX...';
$this->email = 'test....@tfbnw.net';
$this->uid = '105.......';
$this->formData =json_encode(
array(
'authResponse' => array(
'access_token' => $this->access_token,
'expiresIn' => 5418,
'userID' => $this->uid
),
'status' => 'connected'));
}
public function testFacebookConnection()
{
$client = static::createClient();
// Submit a raw JSON string in the request body
$client->request(
'POST',
$client->getContainer()->get('router')->generate('oauth_connect'),
array(),
array(),
array('CONTENT_TYPE' => 'application/json'),
'{ "service":"facebook","authentication" : "'. $this->formData.'"}'
);
die(var_dump($client->getResponse()->getContent()));
}
並在調用URI方法簽名下方
/**
* Calls a URI.
*
* @param string $method The request method
* @param string $uri The URI to fetch
* @param array $parameters The Request parameters
* @param array $files The files
* @param array $server The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does)
* @param string $content The raw body data
* @param bool $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())
*
* @return Crawler
*/
public function request($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true)
{
您應該在方法簽名后的第三個參數中使用post params:
$this->postData =
array(
'service' => 'facebook',
'authentication'=> $this->formData,
);
然后調用方法如下:
$client->request(
'POST',
$client->getContainer()->get('router')->generate('oauth_connect'),
$this->postData,
array(),
array('CONTENT_TYPE' => 'application/json')
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.