本文介绍了PHPUnit-getallheaders不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试我的代码,并且标题有一些问题.在每个api中,我都使用

I'm testing my code, and i have some problem with header. In each api i use

$headers = getallheaders();

得到它,当我使用应用程序或crhome邮递员扩展程序进行测试时,此方法工作正常.当我对测试进行测试时,就这样

to get that, and this works fine when i test with the app or crhome postman extension.When i lauch my test, like this

 $client = $this->createClient();
    $client->request('GET', '/api/shotcard',
        ['qrcode'=>'D0m1c173'], [],
        ['HTTP_API_TOKEN' => 'abc123']
    );

    $this->assertEquals(200, $client->getResponse()->getStatusCode());

我尝试用带有该测试令牌(而不是我将在应用程序中使用的令牌)的用户向该卡拍摄带有该qrcode的卡片,在这里我看到这样的呼叫:https://stackoverflow.com/a/11681422/5475228 .测试以这种方式失败:

where i try to shot a card with that qrcode with a user with that test token (not the token i'll use in the application), i see a call like this here: https://stackoverflow.com/a/11681422/5475228 .The test fails in this way:

推荐答案

来自这篇文章:

joyview中,用户在PHP手册上的getallheaders()功能上发表的评论在gmail点com上

From user contributed comments at getallheaders() function on PHP manual by joyview at gmail dot com

if (!function_exists('getallheaders')) {
    function getallheaders() {
    $headers = [];
    foreach ($_SERVER as $name => $value) {
        if (substr($name, 0, 5) == 'HTTP_') {
            $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
        }
    }
    return $headers;
    }
}

这篇关于PHPUnit-getallheaders不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 15:58