douhuan6065 2013-04-15 16:52
浏览 43

Zend_Test_PHPUnit_ControllerTestCase中的assertHeader无法正常工作?

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
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。