繁体   English   中英

symfony2测试带有发布数据的客户端请求

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM