I started to write some unit test for an app in zend framework. Looking around I came to the PHPunit and then to the Zend_Test (which in turn extends the PHPunit).
So I opted to write tests with Zend_Test since it gives me some zend framework specific assertions...
All went fine until I had to check in a test the response headers...
My intuition was to use the $this->assertHeader($headerNameToCheckIfItExists)
assertion. Unfortunately it failed.
So I started to look around why it fails... I checked that my application sets the header in the response (and it sets it correctly) and after that I checked with curl
the raw response Http headers. It appears that the setted header by the application is present in the response!
So what is the problem?
The problem is that in zend application the headers can be set with $this->setHeader($headerName, $headerValue)
method as well as with $this->setRawHeader($headerNameAndHeaderValue)
method. There is also the $this->getHeaders()
and the $this->getRawHeaders()
to get the headers as an array of key value pair or as an array of plain string, respectively. (Looking in the source code I discovered that there are 2 distinct arrays of headers - one with key value elements and other with plain string elements)
In my app I used the $this->setRawHeader($headerNameAndHeaderValue)
and the $this->assertHeader($headerNameToCheckIfItExists)
fails. If I use $this->setHeader($headerName, $headerValue)
the $this->assertHeader($headerNameToCheckIfItExists)
does not fail...
So it appears that the $this->assertHeader($headerNameToCheckIfItExists)
checks if the $headerNameToCheckIfItExists
header name exists by looking only in the $this->getResponse()->getHeaders()
!
Please, if some one knows something about this "drawback" answer at this post.
The workaround I made is to overload the $this->assertHeader($headerNameToCheckIfItExists)
in my test case file:
public function assertHeader($header, $message='') {
$all_headers = array();
foreach ($this->getResponse()->getRawHeaders() as $str) {
$components = explode(":", $str);
$all_headers[] = array('name' => $components[0],
'value' => trim($components[1]),
'replace' => false);
}
$all_headers = array_merge($all_headers, $this->getResponse()->getHeaders());
foreach ($all_headers as $h)
if ($h['name'] == $header)
return $this->assertTrue(true, $message);
$this->fail('no header with name: '.$header);
}
My zend framework version:
zf show version
Zend Framework Version: 1.10.6