@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$lowerCaseFinalHeaders = array_change_key_case($finalHeaders);
|
||||||
|
if (!array_key_exists("user-agent", $lowerCaseFinalHeaders)) {
|
||||||
$lowercaseHeaders[] = "user-agent: unirest-php/1.1";
|
$lowercaseHeaders[] = "user-agent: unirest-php/1.1";
|
||||||
|
}
|
||||||
|
if (!array_key_exists("expect", $lowerCaseFinalHeaders)) {
|
||||||
$lowercaseHeaders[] = "expect:";
|
$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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,6 +104,20 @@ class UnirestTest extends UnitTestCase
|
|||||||
$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()
|
||||||
{
|
{
|
||||||
$response = Unirest::post("http://httpbin.org/post", array(
|
$response = Unirest::post("http://httpbin.org/post", array(
|
||||||
@@ -277,4 +292,16 @@ class UnirestTest extends UnitTestCase
|
|||||||
$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'});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user