PHPUnit - getallheaders 不起作用
PHPUnit - getallheaders not work
我正在测试我的代码,header 有一些问题。在每个 api 中我使用
$headers = getallheaders();
得到它,当我使用应用程序或 crhome 邮递员扩展进行测试时,这工作正常。
当我开始测试时,像这样
$client = $this->createClient();
$client->request('GET', '/api/shotcard',
['qrcode'=>'D0m1c173'], [],
['HTTP_API_TOKEN' => 'abc123']
);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
我尝试用那个测试令牌(不是我将在应用程序中使用的令牌)的用户用那个二维码拍摄一张卡片,我在这里看到这样的调用:。
这样测试失败:
PHP Fatal error: Call to undefined function AppBackendBundle\Controller\getallheaders() in /var/www/pitstop/src/AppBackendBundle/Controller/ApiController.php on line 42
来自this篇文章:
If you use Nginx, PHP-FPM or any other FastCGI method of running PHP
you’ve probably noticed that the function getallheaders()
does not
exist. There are many creative workarounds in the wild, but PHP offers
two very nice features to ease your pain.
来自 getallheaders()
用户在 joyview at gmail dot com
的 PHP 手册中提供的评论
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;
}
}
我就是这样解决的(感谢https://whosebug.com/a/11681422/5475228)
private function request_headers($type, Request $request)
{
if(function_exists("getallheaders"))
{
if($header = getallheaders()[$type])
{
return $header;
}
}
return $request->headers->get($type);
}
所以来自应用程序的正常请求使用 getallheaders() 获取 header,来自 PHPUnit 的请求从 Request object 获取。我不知道为什么(如果有人能解释的话)但有效。
与@Matteos 代码略有不同,删除了 Mod-Rewrite 并添加了通常由 getallheaders()
返回的 Content-Length 和 Content-Type。有趣的是,getallheaders()
返回的数组键的大小写似乎到处都是,而且不一致,而显然这个版本确保了一致性。
$allHeaders = array();
foreach($_SERVER as $name => $value) {
if($name != 'HTTP_MOD_REWRITE' && (substr($name, 0, 5) == 'HTTP_' || $name == 'CONTENT_LENGTH' || $name == 'CONTENT_TYPE')) {
$name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', str_replace('HTTP_', '', $name)))));
$allHeaders[$name] = $value;
}
}
我正在测试我的代码,header 有一些问题。在每个 api 中我使用
$headers = getallheaders();
得到它,当我使用应用程序或 crhome 邮递员扩展进行测试时,这工作正常。 当我开始测试时,像这样
$client = $this->createClient();
$client->request('GET', '/api/shotcard',
['qrcode'=>'D0m1c173'], [],
['HTTP_API_TOKEN' => 'abc123']
);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
我尝试用那个测试令牌(不是我将在应用程序中使用的令牌)的用户用那个二维码拍摄一张卡片,我在这里看到这样的调用:。 这样测试失败:
PHP Fatal error: Call to undefined function AppBackendBundle\Controller\getallheaders() in /var/www/pitstop/src/AppBackendBundle/Controller/ApiController.php on line 42
来自this篇文章:
If you use Nginx, PHP-FPM or any other FastCGI method of running PHP you’ve probably noticed that the function
getallheaders()
does not exist. There are many creative workarounds in the wild, but PHP offers two very nice features to ease your pain.
来自 getallheaders()
用户在 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;
}
}
我就是这样解决的(感谢https://whosebug.com/a/11681422/5475228)
private function request_headers($type, Request $request)
{
if(function_exists("getallheaders"))
{
if($header = getallheaders()[$type])
{
return $header;
}
}
return $request->headers->get($type);
}
所以来自应用程序的正常请求使用 getallheaders() 获取 header,来自 PHPUnit 的请求从 Request object 获取。我不知道为什么(如果有人能解释的话)但有效。
与@Matteos 代码略有不同,删除了 Mod-Rewrite 并添加了通常由 getallheaders()
返回的 Content-Length 和 Content-Type。有趣的是,getallheaders()
返回的数组键的大小写似乎到处都是,而且不一致,而显然这个版本确保了一致性。
$allHeaders = array();
foreach($_SERVER as $name => $value) {
if($name != 'HTTP_MOD_REWRITE' && (substr($name, 0, 5) == 'HTTP_' || $name == 'CONTENT_LENGTH' || $name == 'CONTENT_TYPE')) {
$name = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', str_replace('HTTP_', '', $name)))));
$allHeaders[$name] = $value;
}
}