From 6678c1406467e36be8619def36908289ae68ebde Mon Sep 17 00:00:00 2001 From: matthew Date: Fri, 18 Dec 2009 21:46:30 +0000 Subject: [PATCH] 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 --- library/Zend/Controller/Response/Abstract.php | 39 +++++++++++++++++++++++++++ tests/Zend/Controller/Response/HttpTest.php | 29 ++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/library/Zend/Controller/Response/Abstract.php b/library/Zend/Controller/Response/Abstract.php index c35483d97..2fda1866e 100644 --- a/library/Zend/Controller/Response/Abstract.php +++ b/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 * diff --git a/tests/Zend/Controller/Response/HttpTest.php b/tests/Zend/Controller/Response/HttpTest.php index aa42767f2..015295ef8 100644 --- a/tests/Zend/Controller/Response/HttpTest.php +++ b/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'); -- 2.11.4.GIT