cakephpcakephp-2.0cakephp-2.6

How to set any header in cakePHP 2.6 Controller Test


I have a Controller function, that expects the header 'X-Bla-Bla' from my JSON call. I catch the header with this:

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

Now I want to write a test for this but I can't send headers.

My test looks like this:

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

How can I send the header?

If not, how can I still test my function?


Solution

  • If you set header this way in the test:

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

    before calling testAction(), then your controller's action will be able to read 'abc' with expression:

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