我正在用Symfony2编写功能测试.
我有一个控制器调用一个getImage()流式传输图像文件的函数,如下所示:
public function getImage($filePath)
$response = new StreamedResponse();
$response->headers->set('Content-Type', 'image/png');
$response->setCallback(function () use ($filePath) {
$bytes = @readfile(filePath);
if ($bytes === false || $bytes <= 0)
throw new NotFoundHttpException();
});
return $response;
}
Run Code Online (Sandbox Code Playgroud)
在功能测试中,我尝试使用Symfony测试客户端请求内容,如下所示:
$client = static::createClient();
$client->request('GET', $url);
$content = $client->getResponse()->getContent();
Run Code Online (Sandbox Code Playgroud)
问题是它$content是空的,我猜是因为客户端收到HTTP头后立即生成响应,而不等待传递数据流.
有没有办法捕获流式响应的内容,同时仍然使用$client->request()(甚至一些其他功能)将请求发送到服务器?