0

我有一个控制器函数,它需要来自我的 JSON 调用的标头“X-Bla-Bla”。我用这个抓住了标题:

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

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

我的测试如下所示:

    $_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);

如何发送标头?

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

4

1 回答 1

2

如果您在测试中以这种方式设置标题:

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

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

$this->request->header('X-Bla-Bla')
于 2016-02-01T19:44:53.817 回答