diff --git a/lib/Unirest/Unirest.php b/lib/Unirest/Unirest.php index 4f75700..86beea3 100644 --- a/lib/Unirest/Unirest.php +++ b/lib/Unirest/Unirest.php @@ -164,16 +164,20 @@ class Unirest { if ($headers == NULL) $headers = array(); + $lowercaseHeaders = array(); - foreach ($headers as $key => $val) { - $lowercaseHeaders[] = Unirest::getHeader($key, $val); - } - foreach (Unirest::$defaultHeaders as $key => $val) { + $finalHeaders = array_merge($headers, Unirest::$defaultHeaders); + foreach ($finalHeaders as $key => $val) { $lowercaseHeaders[] = Unirest::getHeader($key, $val); } - $lowercaseHeaders[] = "user-agent: unirest-php/1.1"; - $lowercaseHeaders[] = "expect:"; + $lowerCaseFinalHeaders = array_change_key_case($finalHeaders); + if (!array_key_exists("user-agent", $lowerCaseFinalHeaders)) { + $lowercaseHeaders[] = "user-agent: unirest-php/1.1"; + } + if (!array_key_exists("expect", $lowerCaseFinalHeaders)) { + $lowercaseHeaders[] = "expect:"; + } $ch = curl_init(); if ($httpMethod != HttpMethod::GET) { @@ -191,7 +195,7 @@ class Unirest $url .= "?"; } Unirest::http_build_query_for_curl($body, $postBody); - $url .= http_build_query($postBody); + $url .= urldecode(http_build_query($postBody)); } curl_setopt($ch, CURLOPT_URL, Unirest::encodeUrl($url)); @@ -267,8 +271,6 @@ class Unirest private static function getHeader($key, $val) { $key = trim(strtolower($key)); - if ($key == "user-agent" || $key == "expect") - continue; return $key . ": " . $val; } diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index 02f10d5..8dbd2f7 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -2,6 +2,7 @@ class UnirestTest extends UnitTestCase { + public function testGet() { $response = Unirest::get("http://httpbin.org/get?name=Mark", array( @@ -34,8 +35,8 @@ class UnirestTest extends UnitTestCase $args = $response->body->args; $this->assertEqual("value", $args->key); - $this->assertEqual("item1", $args->{"items%5B0%5D"}); - $this->assertEqual("item2", $args->{"items%5B1%5D"}); + $this->assertEqual("item1", $args->{"items[0]"}); + $this->assertEqual("item2", $args->{"items[1]"}); } public function testGetWithDots() @@ -66,7 +67,7 @@ class UnirestTest extends UnitTestCase $this->assertEqual(200, $response->code); $args = $response->body->args; - $this->assertEqual("Mark+Bond", $args->{"user.name"}); + $this->assertEqual("Mark Bond", $args->{"user.name"}); $this->assertEqual("thefosk", $args->nick); } @@ -102,6 +103,20 @@ class UnirestTest extends UnitTestCase $this->assertEqual("Mark", $form->{"name[0]"}); $this->assertEqual("John", $form->{"name[1]"}); } + + public function testGetArray() + { + $response = Unirest::get("http://httpbin.org/get", array(), array( + "name[0]" => "Mark", + "name[1]" => "John" + )); + + $this->assertEqual(200, $response->code); + + $args = $response->body->args; + $this->assertEqual("Mark", $args->{"name[0]"}); + $this->assertEqual("John", $args->{"name[1]"}); + } public function testPostWithDots() { @@ -276,5 +291,17 @@ class UnirestTest extends UnitTestCase $headers = $response->body->headers; $this->assertEqual("Basic dXNlcjpwYXNzd29yZA==", $headers->Authorization); } + + public function testCustomHeaders() + { + $response = Unirest::get('http://httpbin.org/get', array( + 'user-agent' => 'ciao', + )); + + $this->assertEqual(200, $response->code); + + $headers = $response->body->headers; + $this->assertEqual("ciao", $headers->{'User-Agent'}); + } } \ No newline at end of file