Header management code added.

This commit is contained in:
Matt Butcher 2012-06-22 17:11:34 -05:00
parent 6013bf3b8a
commit fde9d44030
4 changed files with 70 additions and 1 deletions

View File

@ -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.
*

View File

@ -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) {

View File

@ -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));
}
}

View File

@ -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]);
}