瀏覽代碼

ZF-6038: allow removing headers from response; patch courtesy Wil Moore

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19767 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 16 年之前
父節點
當前提交
406f3cf38d
共有 2 個文件被更改,包括 68 次插入0 次删除
  1. 39 0
      library/Zend/Controller/Response/Abstract.php
  2. 29 0
      tests/Zend/Controller/Response/HttpTest.php

+ 39 - 0
library/Zend/Controller/Response/Abstract.php

@@ -184,6 +184,27 @@ abstract class Zend_Controller_Response_Abstract
         return $this;
     }
 
+	/**
+	 * Clears the specified HTTP header
+	 *
+	 * @param  string $name
+	 * @return Zend_Controller_Response_Abstract
+	 */
+	public function clearHeader($name)
+	{
+		if (! count($this->_headers)) {
+			return $this;
+		}
+
+		foreach ($this->_headers as $index => $header) {
+			if ($name == $header['name']) {
+				unset($this->_headers[$index]);
+			}
+		}
+
+		return $this;
+	}
+
     /**
      * Set raw HTTP header
      *
@@ -223,6 +244,24 @@ abstract class Zend_Controller_Response_Abstract
         return $this;
     }
 
+	/**
+	 * Clears the specified raw HTTP header
+	 *
+	 * @param  string $headerRaw
+	 * @return Zend_Controller_Response_Abstract
+	 */
+	public function clearRawHeader($headerRaw)
+	{
+		if (! count($this->_headersRaw)) {
+			return $this;
+		}
+
+		$key = array_search($headerRaw, $this->_headersRaw);
+		unset($this->_headersRaw[$key]);
+
+		return $this;
+	}
+
     /**
      * Clear all headers, normal and raw
      *

+ 29 - 0
tests/Zend/Controller/Response/HttpTest.php

@@ -124,6 +124,20 @@ class Zend_Controller_Response_HttpTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(0, count($headers));
     }
 
+	/**
+	 * @group ZF-6038
+	 */
+    public function testClearHeader()
+    {
+        $this->_response->setHeader('Connection', 'keep-alive');
+        $original_headers = $this->_response->getHeaders();
+
+        $this->_response->clearHeader('Connection');
+        $updated_headers  = $this->_response->getHeaders();
+        
+        $this->assertFalse($original_headers == $updated_headers);
+    }
+
     public function testSetRawHeader()
     {
         $this->_response->setRawHeader('HTTP/1.0 404 Not Found');
@@ -142,6 +156,21 @@ class Zend_Controller_Response_HttpTest extends PHPUnit_Framework_TestCase
         $this->assertTrue(empty($headers));
     }
 
+	/**
+	 * @group ZF-6038
+	 */
+    public function testClearRawHeader()
+    {
+        $this->_response->setRawHeader('HTTP/1.0 404 Not Found');
+        $this->_response->setRawHeader('HTTP/1.0 401 Unauthorized');
+        $originalHeadersRaw = $this->_response->getRawHeaders();
+
+        $this->_response->clearRawHeader('HTTP/1.0 404 Not Found');
+        $updatedHeadersRaw  = $this->_response->getRawHeaders();
+        
+        $this->assertFalse($originalHeadersRaw == $updatedHeadersRaw);
+    }
+
     public function testClearAllHeaders()
     {
         $this->_response->setRawHeader('HTTP/1.0 404 Not Found');