自定义标头未通过

自定义标头未通过

本文介绍了Symfony 功能测试 - 自定义标头未通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,当我发送一个新的 $client->请求我指定的标头时丢失了:

For some reason when i sent a new $client->request the headers I specify get lost:

public function testGetClientsAction()
{
    $client = static::createClient();

    $cookie = new Cookie('locale2', 'en', time() + 3600 * 24 * 7, '/', null, false, false);
    $client->getCookieJar()->set($cookie);

    // Visit user login page and login
    $crawler = $client->request('GET', '/login');
    $form = $crawler->selectButton('login')->form();
    $crawler = $client->submit($form, array('_username' => 'greg', '_password' => 'greg'));

    $client->request(
       'GET',
       '/clients',
        array(),
        array(),
        array('X-Requested-With' => 'XMLHttpRequest', 'accept' => 'application/json')
    );

    print_r($client->getResponse());
    die();

}

在正在测试的方法中,我在第一行有这个:

In the method that is being tested I have this on the first line:

print_r($request->headers->all());

回复如下:

Array
(
    [host] => Array
        (
            [0] => localhost
        )

    [user-agent] => Array
        (
            [0] => Symfony2 BrowserKit
        )

    [accept] => Array
        (
            [0] => text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
        )

    [accept-language] => Array
        (
            [0] => en-us,en;q=0.5
        )

    [accept-charset] => Array
        (
            [0] => ISO-8859-1,utf-8;q=0.7,*;q=0.7
        )

    [referer] => Array
        (
            [0] => http://localhost/login_check
        )

    [x-php-ob-level] => Array
        (
            [0] => 1
        )

)

推荐答案

我也有同样的问题,经过一番挖掘后,我认为这是 BrowserKit 目前不支持的功能.

I have the same issue and after a little dig through I think it is a feature that BrowserKit currently doesn't support.

我已经为它记录了一个问题:https://github.com/symfony/symfony/issues/5074

I have logged an issue for it:https://github.com/symfony/symfony/issues/5074

更新:这不是问题 - 请参阅下面的评论

Update: this is not an issue -- see the comments below

示例请求:

$client->request(
    'GET',
    $url,
    array(),
    array(),
    array(
        'HTTP_X_CUSTOM_VAR' => $var
    )
);

获取数据:

$request->headers->get('x-custom-var');

这篇关于Symfony 功能测试 - 自定义标头未通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:18