如何在 cakePHP 2.6 控制器测试中设置任何 header

How to set any header in cakePHP 2.6 Controller Test

我有一个 Controller 函数,它需要来自我的 JSON 调用的 header 'X-Bla-Bla'。我用这个抓住了 header:

$this->request->header('X-Bla-Bla')

现在我想为此编写一个测试,但我无法发送 headers。

我的测试是这样的:

    $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';

    $url = Router::url(array('api' => true, 'controller' => 'test', 'action' => 'index'));
    $options = array(
        'return' => 'contents',
    );
    $result = $this->testAction($url, $options);
    $this->assertNotEmpty($result);

如何发送header?

如果没有,我该如何测试我的功能?

如果你在测试中这样设置header:

$_SERVER['HTTP_X_BLA_BLA'] = 'abc';

在调用 testAction() 之前,您的控制器的操作将能够读取 'abc' 表达式:

$this->request->header('X-Bla-Bla')