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

View File

@@ -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);
}
@@ -103,6 +104,20 @@ class UnirestTest extends UnitTestCase
$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()
{
$response = Unirest::post("http://httpbin.org/post", array(
@@ -277,4 +292,16 @@ class UnirestTest extends UnitTestCase
$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'});
}
}