diff --git a/src/HPCloud/Storage/ObjectStorage/Object.php b/src/HPCloud/Storage/ObjectStorage/Object.php index c03237f..90b87a9 100644 --- a/src/HPCloud/Storage/ObjectStorage/Object.php +++ b/src/HPCloud/Storage/ObjectStorage/Object.php @@ -451,6 +451,28 @@ class Object { return $this->additionalHeaders; } + /** + * Remove headers. + * + * This takes an array of header names, and removes + * any matching headers. Typically, only headers set + * by setAdditionalHeaders() are removed from an Object. + * (RemoteObject works differently). + * + * @attention + * Many headers are generated automatically, such as + * Content-Type and Content-Length. Removing these + * will simply result in their being regenerated. + * + * @param array $keys + * The header names to be removed. + */ + public function removeHeaders($keys) { + foreach ($keys as $k) { + unset($this->additionalHeaders[$k]); + } + } + /** * This object should be transmitted in chunks. * diff --git a/src/HPCloud/Storage/ObjectStorage/RemoteObject.php b/src/HPCloud/Storage/ObjectStorage/RemoteObject.php index a7291f1..08b0891 100644 --- a/src/HPCloud/Storage/ObjectStorage/RemoteObject.php +++ b/src/HPCloud/Storage/ObjectStorage/RemoteObject.php @@ -296,8 +296,22 @@ class RemoteObject extends Object { } /** - * Given an array of keys, remove the headers. + * Given an array of header names. * + * This will remove the given headers from the existing headers. + * Both additional headers and the original headers from the + * server are affected here. + * + * Note that you cannot remove metadata through this mechanism, + * as it is managed using the metadata() methods. + * + * @attention + * Many headers are generated automatically, such as + * Content-Type and Content-Length. Removing these + * will simply result in their being regenerated. + * + * @param array $keys + * The header names to be removed. */ public function removeHeaders($keys) { foreach ($keys as $key) { diff --git a/test/Tests/ObjectTest.php b/test/Tests/ObjectTest.php index c7a1d3c..2e18716 100644 --- a/test/Tests/ObjectTest.php +++ b/test/Tests/ObjectTest.php @@ -131,4 +131,24 @@ class ObjectTest extends \HPCloud\Tests\TestCase { $this->assertEquals('Leibniz', $got['Gottfried']); } + + public function testAdditionalHeaders() { + $o = $this->basicObjectFixture(); + + $extra = array( + 'a' => 'b', + 'aaa' => 'bbb', + 'ccc' => 'bbb', + ); + $o->setAdditionalHeaders($extra); + + $got = $o->additionalHeaders(); + $this->assertEquals(3, count($got)); + + $o->removeHeaders(array('ccc')); + + + $got = $o->additionalHeaders(); + $this->assertEquals(2, count($got)); + } } diff --git a/test/Tests/RemoteObjectTest.php b/test/Tests/RemoteObjectTest.php index 3d6768b..916db9a 100644 --- a/test/Tests/RemoteObjectTest.php +++ b/test/Tests/RemoteObjectTest.php @@ -53,6 +53,10 @@ class RemoteObjectTest extends \HPCloud\Tests\TestCase { $object->setMetadata(array(self::FMETA_NAME => self::FMETA_VALUE)); $object->setDisposition(self::FDISPOSITION); $object->setEncoding(self::FENCODING); + $object->setAdditionalHeaders(array( + 'Access-Control-Allow-Origin' => 'http://example.com', + 'Access-control-allow-origin' => 'http://example.com', + )); // Need some headers that Swift actually stores and returns. This // one does not seem to be returned ever. @@ -146,6 +150,15 @@ class RemoteObjectTest extends \HPCloud\Tests\TestCase { $headers = $obj->headers(); $this->assertTrue(count($headers) > 1); + fwrite(STDOUT, print_r($headers, TRUE)); + + $this->assertNotEmpty($headers['Date']); + + $obj->removeHeaders(array('Date')); + + $headers = $obj->headers(); + $this->assertFalse(isset($headers['Date'])); + // Swift doesn't return CORS headers even though it is supposed to. //$this->assertEquals(self::FCORS_VALUE, $headers[self::FCORS_NAME]); }