如何测试预期的标题? [英] How to test for expected headers?

查看:65
本文介绍了如何测试预期的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单元测试失败,因为标头已经发送.但是,在这种情况下标头是预期的.

I have a unit test that fails because headers are already sent. However, the header in this scenario is expected.

我如何告诉PHPUnit期望500标头?

How do I tell PHPUnit to expect a 500 header?

我已阅读此问题但没有帮助.

该方法包装在输出缓冲区中.

The method is wrapped inside an output buffer.

ob_start();
$foo->methodWhichSendsHeader();
ob_clean();

推荐答案

如果已安装xdebug,则可以使用xdebug_get_headers()获取标头.然后,您可以根据需要对其进行测试.

If you have xdebug installed you can use xdebug_get_headers() to get the headers. Then you can test them as needed.

$headers=xdebug_get_headers();

为您提供一个看起来像...的数组

gets you an array which looks like...

array(
    0 => "Content-type: text/html",
    1 => ...
)

因此,您需要解析每个标题行以将标题名称与值分开

So you'll need to parse each header line to separate the header name from the value

这篇关于如何测试预期的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆