Symfony功能测试-自定义头不通过

2lpgd968  于 12个月前  发布在  其他
关注(0)|答案(3)|浏览(107)

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

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();

}
在正在测试的方法中,我在第一行有这样的内容:

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
        )

)
6za6bjd0

6za6bjd01#

我有同样的问题,经过一点挖掘,我认为这是一个功能,BrowserKit目前不支持。
我已经记录了一个问题:https://github.com/symfony/symfony/issues/5074
更新:这不是一个问题-请参阅下面的评论

示例代码

样品申请:

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

获取数据:

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

y0u0uwnf2#

如果你从Client.php中检查定义,在方法请求中,你会在docblock中看到一个非常有用的信息:

  • @param array $server服务器参数(HTTP头文件和PHP一样以HTTP_前缀引用)

这意味着你应该在你想要发送的头中添加一个**HTTP_**前缀。例如,如果你想传递头X-HTTP-Method-HTTP,你可以这样指定它:

$client->request(
        Request::METHOD_POST,
        '/api/v1/something',
        $body,
        [],
        ['HTTP_X-HTTP-Method-Override' => Request::METHOD_GET]
    );
qq24tv8q

qq24tv8q3#

除了HTTP_ suffix之外,您还必须小心如何尝试访问它们,下面有更多信息。

For that request :
     $this->client->request(
            Request::METHOD_POST,
            '/api/auth/login',
            [],
            [],
            [
                'CONTENT_TYPE' => 'application/json',
                'HTTP_X-interface' => "string",
            ],
            json_encode([
                'username' => "[email protected]",
                'password' => "exemplePassword"
            ]));
I want get header with : $request->headers->get('X-interface');

我有一个事件侦听器,用于侦听Lexik JWT包的“authentication_success”事件。

public function onAuthenticationSuccess(AuthenticationSuccessEvent $event)
{
    $request    = Request::createFromGlobals();
    $interface  = $request->headers->get('X-interface'); // null
}

public function onAuthenticationSuccess(AuthenticationSuccessEvent $event)
{
    $requestStack = $this->container->get('request_stack');
    $request = $requestStack->getMainRequest();
  $interface = $request->headers->get('X-interface'); // i have header
}

Request::Exclusive FromGlobals()方法在生产环境中有效,因为当前的请求头在全局变量中可用。这些全局变量在PHP接收请求之前由Web服务器定义。
功能测试中,当前请求头在全局变量中不可用。因此,Request::Exclusive FromGlobals()方法返回一个不包含当前请求头的新请求。
最后一个问题:

#[Route('/postTest', name: 'postTest', methods: ['post'])]

    public function postTest(Request $request)
    {
        dd($_SERVER);
    }

使用postMan:我在超级全局$_SERVER中有头
在测试中使用**$client->request(KernelBrowser)**:我在超级全局$_SERVER中没有header

相关问题