title: CakePHP 2.1: $this->headers not getting set when testing controller with $this->testAction() when using a Component to redirect
body: <p>I am beginning to write tests for an application and I have made a wrapper RedirectComponent to handle all redirects.  Now that I am starting to test my app (which I am fairly new to testing) I am following this example here... <a href="http://book.cakephp.org/2.0/en/development/testing.html#a-more-complex-example" rel="nofollow">http://book.cakephp.org/2.0/en/development/testing.html#a-more-complex-example</a></p>
  
  <p>Everything is working good now, except that I cannot run asserts on the <code>$this-&gt;headers</code> parameter, because it is always blank. I discovered that if I redirect directly in the controller using the controllers redirect method everything works fine and <code>$this-&gt;headers</code> is set to:</p>
  
  <pre><code>array(
      'Location' =&gt; 'http://localhost/inquiries/view/4'
  )
  </code></pre>
  
  <p>However the whole app is using this RedirectComponent and I would like to get the <code>$this-&gt;headers</code> to show up when writing controller tests that redirect using this component.</p>
  
  <p>I am lost as to how to solve this problem though, as it seems I am doing everything right and it should be working.</p>
  
  <p>Below is my code.</p>
  
  <pre><code>class InquiriesControllerTestCase extends ControllerTestCase {
  
  
      /**
       * Test Add Pass
       *
       * @return void
       */
      public function testAddPass() { 
          $minimumInsertData = array(
                  'Inquiry'=&gt;array(
                      'fname'=&gt;'---Minimum Insert---',
                      'lname'=&gt;'---Minimum Insert---',
                      'phone'=&gt;'---Minimum Insert---',
                      'email'=&gt;'email@email.com',
                      'subject'=&gt;'---Minimum Insert---',
                      'inquiry'=&gt;'---Minimum Insert---',
                  )
              );
  
          #create mock controller
          $Inquiries = $this-&gt;generate('Inquiries', array(
              'components'=&gt;array(
                  'Email',    
                  'Redirect',
                  'Session',
                  'Sms',
              )
          ));
  
  
          #expects one sms message
          $Inquiries-&gt;Sms
              -&gt;expects($this-&gt;once())
              -&gt;method('send')
              -&gt;will($this-&gt;returnValue(true));
  
          #expects two emails
          $Inquiries-&gt;Email
              -&gt;expects($this-&gt;exactly(2))
              -&gt;method('send')
              -&gt;will($this-&gt;returnValue(true));
  
          #expects flash success
  
          $Inquiries-&gt;Redirect
              -&gt;expects($this-&gt;once())
              -&gt;method('flashSuccess');
  
          #call add
          $test = $this-&gt;testAction('/inquiries/add', array('data'=&gt;$minimumInsertData, 'method'=&gt;'post', 'return'=&gt;'contents'));
          debug($this-&gt;headers);
          die;
      }
  }
  
  
  
  class InquiriesController extends MastersController {
  
      /*
       * Name
       */
      public $name = 'Inquiries';
  
      /*
       * Attached Components
       */
      public $components = array(     
          'Captcha',
          'Email',
          'PersistentValidation',
          'Sms',
      );
  
      /*
       * Helpers
       */
      public $helpers = array(
          'AjaxChat',
      );
  
      /**
       * Add
       * @return void
       */
      public function add(){
          $this-&gt;viewPath = 'Inquiries';
          $this-&gt;set("title_for_layout", 'Contact Us');
          if(!empty($this-&gt;request-&gt;data)) {
              $this-&gt;request-&gt;data['Inquiry']['status_id'] = $this-&gt;Inquiry-&gt;Status-&gt;id('unresolved', 'inquiry');
              //$this-&gt;request-&gt;data['Inquiry']['user_id'] = $this-&gt;Auth-&gt;user('id');
              //$this-&gt;request-&gt;data['Inquiry']['account_id'] = $this-&gt;Auth-&gt;user('account_id');
              $this-&gt;request-&gt;data['Inquiry']['ip'] = env('REMOTE_ADDR');
              if($this-&gt;Inquiry-&gt;save($this-&gt;request-&gt;data)) {
  
                  #send SMS
                  if($this-&gt;sendSms) {                    
                      $smsToAso = array(
                          'number'=&gt;Configure::read('Inquiry.add.sms.number'),
                          'from'=&gt;Configure::read('Inquiry.add.sms.from'),
                          'carrier'=&gt;Configure::read('Inquiry.add.sms.carrier'),
                          'text'=&gt;Configure::read('Inquiry.add.sms.text').' https://www.airsatone.com/admin/inquiries/view/'.$this-&gt;Inquiry-&gt;id,
                      );
                      $this-&gt;Sms-&gt;send($smsToAso);                    
                  }
  
                  #send email
                  if($this-&gt;sendEmail){                       
                      #send user an email
                      $this-&gt;Email-&gt;template = Configure::read('Inquiry.add.email.template');
                      $this-&gt;Email-&gt;to = $this-&gt;request-&gt;data['Inquiry']['email'];
                      $this-&gt;Email-&gt;from = Configure::read('Inquiry.add.email.from');
                      $this-&gt;Email-&gt;subject = Configure::read('Inquiry.add.email.subject');
                      $this-&gt;Email-&gt;message = Configure::read('Inquiry.add.email.message');                   
                      $this-&gt;Email-&gt;send();
  
                      #send email to ASO                  
                      $message = '&lt;p&gt;ASO Inquiry from '.$this-&gt;request-&gt;data['Inquiry']['fname'].' '.$this-&gt;request-&gt;data['Inquiry']['lname'];
                      if(!empty($this-&gt;request-&gt;data['Inquiry']['company'])) $message .= ' at '.$this-&gt;request-&gt;data['Inquiry']['company'].'&lt;/p&gt;';
                      $message .= '&lt;p&gt;You may contact this individual by phone:'.$this-&gt;request-&gt;data['Inquiry']['phone'].' or email:'.$this-&gt;request-&gt;data['Inquiry']['email'].'&lt;/p&gt;';
                      $message .= $this-&gt;request-&gt;data['Inquiry']['inquiry'];
  
                      $this-&gt;Email-&gt;template = Configure::read('Inquiry.add.email_aso.template');
                      $this-&gt;Email-&gt;to = Configure::read('Inquiry.add.email_aso.to');
                      $this-&gt;Email-&gt;from = Configure::read('Inquiry.add.email_aso.from');
                      $this-&gt;Email-&gt;subject = Configure::read('Inquiry.add.email_aso.subject');
                      $this-&gt;Email-&gt;message = $message;
                      $this-&gt;Email-&gt;send();
                  }           
                   return $this-&gt;redirect(array('controller'=&gt;'inquiries', 'action'=&gt;'view', $this-&gt;Inquiry-&gt;id));            
                  //return $this-&gt;Redirect-&gt;flashSuccess('Thanks for your inquiry, XXXXXX will be contacting you regarding your inquiry shortly.', array('controller'=&gt;'inquiries', 'action'=&gt;'view', $this-&gt;Inquiry-&gt;id));
              } else {
   return $this-&gt;redirect($this-&gt;referer());
                  //return $this-&gt;Redirect-&gt;flashWarning('There was a problem submitting your form. Please check to make sure the form was filled out correctly.', $this-&gt;referer());
              }
          }
      }
  }
  
  
  class RedirectComponent extends Component {
  
      /*
       * Controller
       */
      public $controller = null;
  
      /*
       * Components
       */
      public $components = array('Session');
  
      /*
       * Settings
       */
      public $settings = array();
  
      /*
       * Success
       */
      public $success = null;
  
      /*
       * Warning
       */
      public $warning = null;
  
      /*
       * Notice
       */
      public $notice = null;
  
      /*
       * Config
       */
      public $config = array();
  
      /**
       * Initialize
       * @param $controller
       */
      public function initialize(&amp;$controller){
          $this-&gt;controller = $controller;
          $this-&gt;settings = array_merge(array('success'=&gt;'success', 'warning'=&gt;'warning', 'notice'=&gt;'notice'), $this-&gt;config);
          $this-&gt;success =  $this-&gt;settings['success'];
          $this-&gt;warning = $this-&gt;settings['warning'];
          $this-&gt;notice = $this-&gt;settings['notice'];
      }
  
      /**
       * Flash Success
       * 
       * Redirects a user with a success flash message
       * @param $msg
       * @param $url
       */
      public function flashSuccess($msg, $url=null){
          $this-&gt;Session-&gt;setFlash($msg, $this-&gt;success);
          if(!empty($url)){
              return $this-&gt;controller-&gt;redirect($url, null, true);
          }
      }
  
      /**
       * Flash Warning
       *  
       * Redirects a user with a warning flash message
       * @param $msg
       * @param $url
       */
      public function flashWarning($msg, $url=null) {
          $this-&gt;Session-&gt;setFlash($msg, $this-&gt;warning);
          if(!empty($url)){
              return $this-&gt;controller-&gt;redirect($url, null, true);
          }
      }
  
      /**
       * Flash Notice
       *  
       * Redirects a user with a notice flash message
       * @param $msg
       * @param $url
       */
      public function flashNotice($msg, $url=null){
          $this-&gt;Session-&gt;setFlash($msg, $this-&gt;notice);
          if(!empty($url)){
              return $this-&gt;controller-&gt;redirect($url, null, true);
          }
      }
  
      /**
       * Determines if an id is empty &amp; sends a flash arning if it is.
       * 
       * @param $id
       * @param $url
       * @param $msg
       */
      public function idEmpty($id=null, $url=null, $msg='Invalid Id'){
          if (!$id &amp;&amp; empty($this-&gt;controller-&gt;data)) {
              $this-&gt;flashWarning($msg, $url);
          }
      }
  }
  </code></pre>