This commit is contained in:
thefosk
2014-03-19 12:18:52 -07:00
parent 6bb573b1f6
commit a7797328fa
2 changed files with 41 additions and 12 deletions

View File

@@ -164,16 +164,20 @@ class Unirest
{ {
if ($headers == NULL) if ($headers == NULL)
$headers = array(); $headers = array();
$lowercaseHeaders = array(); $lowercaseHeaders = array();
foreach ($headers as $key => $val) { $finalHeaders = array_merge($headers, Unirest::$defaultHeaders);
$lowercaseHeaders[] = Unirest::getHeader($key, $val); foreach ($finalHeaders as $key => $val) {
}
foreach (Unirest::$defaultHeaders as $key => $val) {
$lowercaseHeaders[] = Unirest::getHeader($key, $val); $lowercaseHeaders[] = Unirest::getHeader($key, $val);
} }
$lowercaseHeaders[] = "user-agent: unirest-php/1.1"; $lowerCaseFinalHeaders = array_change_key_case($finalHeaders);
$lowercaseHeaders[] = "expect:"; 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(); $ch = curl_init();
if ($httpMethod != HttpMethod::GET) { if ($httpMethod != HttpMethod::GET) {
@@ -191,7 +195,7 @@ class Unirest
$url .= "?"; $url .= "?";
} }
Unirest::http_build_query_for_curl($body, $postBody); 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)); curl_setopt($ch, CURLOPT_URL, Unirest::encodeUrl($url));
@@ -267,8 +271,6 @@ class Unirest
private static function getHeader($key, $val) private static function getHeader($key, $val)
{ {
$key = trim(strtolower($key)); $key = trim(strtolower($key));
if ($key == "user-agent" || $key == "expect")
continue;
return $key . ": " . $val; return $key . ": " . $val;
} }

View File

@@ -2,6 +2,7 @@
class UnirestTest extends UnitTestCase class UnirestTest extends UnitTestCase
{ {
public function testGet() public function testGet()
{ {
$response = Unirest::get("http://httpbin.org/get?name=Mark", array( $response = Unirest::get("http://httpbin.org/get?name=Mark", array(
@@ -34,8 +35,8 @@ class UnirestTest extends UnitTestCase
$args = $response->body->args; $args = $response->body->args;
$this->assertEqual("value", $args->key); $this->assertEqual("value", $args->key);
$this->assertEqual("item1", $args->{"items%5B0%5D"}); $this->assertEqual("item1", $args->{"items[0]"});
$this->assertEqual("item2", $args->{"items%5B1%5D"}); $this->assertEqual("item2", $args->{"items[1]"});
} }
public function testGetWithDots() public function testGetWithDots()
@@ -66,7 +67,7 @@ class UnirestTest extends UnitTestCase
$this->assertEqual(200, $response->code); $this->assertEqual(200, $response->code);
$args = $response->body->args; $args = $response->body->args;
$this->assertEqual("Mark+Bond", $args->{"user.name"}); $this->assertEqual("Mark Bond", $args->{"user.name"});
$this->assertEqual("thefosk", $args->nick); $this->assertEqual("thefosk", $args->nick);
} }
@@ -102,6 +103,20 @@ class UnirestTest extends UnitTestCase
$this->assertEqual("Mark", $form->{"name[0]"}); $this->assertEqual("Mark", $form->{"name[0]"});
$this->assertEqual("John", $form->{"name[1]"}); $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() public function testPostWithDots()
{ {
@@ -276,5 +291,17 @@ class UnirestTest extends UnitTestCase
$headers = $response->body->headers; $headers = $response->body->headers;
$this->assertEqual("Basic dXNlcjpwYXNzd29yZA==", $headers->Authorization); $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'});
}
} }