获取单元测试的状态码响应标头

时间:2014-10-12 16:35:53

标签: php phpunit

我正在阅读this question,因此我可以成功测试表格的标题:

header('X-Something: value');
header('Content-Type: text/html');

但是,我有设置响应代码的代码,我想在单元测试中验证它是否正常工作,但它们不会返回xdebug_get_headers返回值。这些电话看起来像是:

header('HTTP/1.1 201 Created');
header('HTTP/1.1 422 Unprocessable Entity');

还有检查这个标题吗?或者我只需要依赖其余的返回值来证明我的控制器正在工作

2 个答案:

答案 0 :(得分:2)

您可以使用http_response_code()函数返回您设置的值:

print_r("Response Code: " . http_response_code());

<强>结果:

Response Code: 422

文档:http_response_code() - PHP 5.4.0 +

答案 1 :(得分:0)

如果我理解了所有内容,那么您正在寻找headers_list(),它会返回已发送(或准备发送)的响应标头列表,请参阅docs。执行以下操作:

header("X-Sample-Test: foo");
header('Content-type: text/plain');

var_dump(headers_list());

将输出:

array(3) {
    [0]=>
    string(23) "X-Powered-By: PHP/5.1.3"
    [1]=>
    string(18) "X-Sample-Test: foo"
    [2]=>
    string(24) "Content-type: text/plain"
}
相关问题