Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony1 使用Symfony 1.4中的sfBrowser进行功能测试时未通过内容类型_Symfony1_Symfony 1.4_Functional Testing - Fatal编程技术网

Symfony1 使用Symfony 1.4中的sfBrowser进行功能测试时未通过内容类型

Symfony1 使用Symfony 1.4中的sfBrowser进行功能测试时未通过内容类型,symfony1,symfony-1.4,functional-testing,Symfony1,Symfony 1.4,Functional Testing,测试针对POST api端点,其中数据作为JSON包含在POST主体中。在进行post调用之前,我将内容类型设置为'application/json'。但是,当我测试格式isFormat('JSON')时,响应为空。如果我转储$request->contentType(),这也会生成null 在功能测试期间,setHttpHeader('Content-Type','application/json')没有正确设置标头的原因是什么?您的设置方法是正确的,但在sfBrowserBase内部存在以下

测试针对POST api端点,其中数据作为JSON包含在POST主体中。在进行post调用之前,我将
内容类型设置为
'application/json'
。但是,当我测试格式
isFormat('JSON')
时,响应为空。如果我转储
$request->contentType()
,这也会生成null


在功能测试期间,
setHttpHeader('Content-Type','application/json')
没有正确设置标头的原因是什么?

您的设置方法是正确的,但在sfBrowserBase内部存在以下错误:

foreach ($this->headers as $header => $value)
{
  $_SERVER['HTTP_'.strtoupper(str_replace('-', '_', $header))] = $value;
}
设置前缀为HTTP的内容类型。但是在$request->getContentType()方法中,假设您没有前缀

因此,如果你改变这一点:

foreach ($this->headers as $header => $value)
{
  $_SERVER[strtoupper(str_replace('-', '_', $header))] = $value;
}
您可以正确地执行
$request->getContentType()


您可以找到更新。

很好,感谢@nicolx,我可以解释更多正在发生的事情,并提供一些进一步的指导

正如@nicolx
$request->getContentType()
所指出的,正在查找不带前缀HTTP的HTTP头(请参见
sfWebRequest
中的第163至173行)。但是,sfBrowserBase总是向所有头添加HTTP_uu前缀。因此,添加此mod:

foreach($this->headers as $header => $value) 
{
  if(strotolower($header) == 'content-type' || strtolower($header) == 'content_type')
  {
    $_SERVER[strtoupper(str_replace('-','_',$header))] = $value;
  } else {
    $_SERVER['HTTP_'.strtoupper(str_replace('-','_',$header))] = $value;
  }
}
这将处理设置的
ContentType
标题,并可在您的操作中检测到。如果不包括
HTTP\uuu
前缀,则其他标题将不起作用(例如
$request->isXmlHtttpHeader()
将失败,即使您在测试文件中设置了此前缀)

测试方法
isFormat()
不是测试ContentType头,而是测试Symfony路由设置。如果我将路由设置为特别具有
sf\u格式:json

some_route:
  url: /something/to/do
  param: {module: top, action: index, sf_format: json}
然后是测试

with('request')->begin()->
   isFormat('json')->
end()->
返回true

由于我想测试标题设置,我向sfTesterRequest添加了一个名为
isContentType()
的新测试方法。此方法的代码为:

public function isContentType($type)
{
  $this->tester->is($this->request->getContentType(),$type, sprintf('request method is "%s"',strtoupper($type)));

  return $this->getObjectToReturn();
}
调用此测试只会变成:

with('request')->begin()->
  isContentType('Application/Json')->
end()->

这实际上打破了其他标题。您需要为ContentType设置一个异常,因为
$request->getContentType()
正在查找没有HTTP的头,但是其他诸如
$request->isXmlHttpRequest()
的头确实需要HTTP前缀。因此,foreach循环变为:
foreach($header=>value){if(strtolower($header)='content-type'| | strtolower($header)='content-type'){$\u服务器[strtouper(strtouper-replace('-',''''''''''u',$header))]=$value;}其他{$\HTTP..strtouper(strtouper-replace('-','-','''',$header))=$value;}