Use CaseInsensitiveArray when setting headers

This commit is contained in:
Zach Borboa
2014-12-31 00:19:11 -08:00
parent 44469ecfe3
commit 5d7d25ac15
2 changed files with 25 additions and 3 deletions
+6 -1
View File
@@ -49,6 +49,7 @@ class Curl
$this->setDefaultUserAgent();
$this->setOpt(CURLINFO_HEADER_OUT, true);
$this->setOpt(CURLOPT_RETURNTRANSFER, true);
$this->headers = new CaseInsensitiveArray();
}
public function get($url_mixed, $data = array())
@@ -212,9 +213,13 @@ class Curl
public function setHeader($key, $value)
{
$this->headers[$key] = $value;
$headers = array();
foreach ($this->headers as $key => $value) {
$headers[$key] = $value;
}
$this->setOpt(CURLOPT_HTTPHEADER, array_map(function($value, $key) {
return $key . ': ' . $value;
}, $this->headers, array_keys($this->headers)));
}, $headers, array_keys($headers)));
}
public function unsetHeader($key)
+19 -2
View File
@@ -507,7 +507,24 @@ class CurlTest extends PHPUnit_Framework_TestCase
$this->assertEquals('HTTP/1.1 401 Unauthorized', $test->curl->error_message);
}
public function testHeaders()
public function testRequestHeaderCaseSensitivity()
{
$content_type = 'application/json';
$curl = new Curl();
$curl->setHeader('Content-Type', $content_type);
$reflector = new ReflectionClass('\Curl\Curl');
$property = $reflector->getProperty('headers');
$property->setAccessible(true);
$headers = $property->getValue($curl);
$this->assertEquals($content_type, $headers['Content-Type']);
$this->assertEquals($content_type, $headers['content-type']);
$this->assertEquals($content_type, $headers['CONTENT-TYPE']);
$this->assertEquals($content_type, $headers['cOnTeNt-TyPe']);
}
public function testResponseHeaders()
{
$test = new Test();
$test->curl->setHeader('Content-Type', 'application/json');
@@ -518,7 +535,7 @@ class CurlTest extends PHPUnit_Framework_TestCase
$this->assertEquals('application/json', $test->server('server', 'GET', array('key' => 'HTTP_ACCEPT')));
}
public function testHeaderCaseSensitivity()
public function testResponseHeaderCaseSensitivity()
{
$content_type = 'application/json';
$test = new Test();