4 Commits
1.1 ... 1.2

Author SHA1 Message Date
thefosk
4d7aabfa47 closes #34 2014-03-19 12:23:55 -07:00
thefosk
a7797328fa closes #36 and #37 2014-03-19 12:18:52 -07:00
thefosk
6bb573b1f6 tests 2014-03-18 11:23:33 -07:00
thefosk
2fb13154e2 formatting 2014-03-18 11:15:38 -07:00
5 changed files with 714 additions and 616 deletions

View File

@@ -12,4 +12,3 @@
const PATCH = "PATCH"; const PATCH = "PATCH";
} }

View File

@@ -7,7 +7,6 @@
{ {
private static $verifyPeer = true; private static $verifyPeer = true;
private static $socketTimeout = null; private static $socketTimeout = null;
private static $defaultHeaders = array(); private static $defaultHeaders = array();
@@ -15,7 +14,8 @@
* Verify SSL peer * Verify SSL peer
* @param bool $enabled enable SSL verification, by default is true * @param bool $enabled enable SSL verification, by default is true
*/ */
public static function verifyPeer($enabled) { public static function verifyPeer($enabled)
{
Unirest::$verifyPeer = $enabled; Unirest::$verifyPeer = $enabled;
} }
@@ -23,7 +23,8 @@
* Set a timeout * Set a timeout
* @param integer $seconds timeout value in seconds * @param integer $seconds timeout value in seconds
*/ */
public static function timeout($seconds) { public static function timeout($seconds)
{
Unirest::$socketTimeout = $seconds; Unirest::$socketTimeout = $seconds;
} }
@@ -32,14 +33,16 @@
* @param string $name header name * @param string $name header name
* @param string $value header value * @param string $value header value
*/ */
public static function defaultHeader($name, $value) { public static function defaultHeader($name, $value)
{
Unirest::$defaultHeaders[$name] = $value; Unirest::$defaultHeaders[$name] = $value;
} }
/** /**
* Clear all the default headers * Clear all the default headers
*/ */
public static function clearDefaultHeaders() { public static function clearDefaultHeaders()
{
Unirest::$defaultHeaders = array(); Unirest::$defaultHeaders = array();
} }
@@ -117,7 +120,8 @@
* Prepares a file for upload. To be used inside the parameters declaration for a request. * Prepares a file for upload. To be used inside the parameters declaration for a request.
* @param string $path The file path * @param string $path The file path
*/ */
public static function file($path) { public static function file($path)
{
if (function_exists("curl_file_create")) { if (function_exists("curl_file_create")) {
return curl_file_create($path); return curl_file_create($path);
} else { } else {
@@ -129,7 +133,8 @@
* This function is useful for serializing multidimensional arrays, and avoid getting * This function is useful for serializing multidimensional arrays, and avoid getting
* the "Array to string conversion" notice * the "Array to string conversion" notice
*/ */
private static function http_build_query_for_curl( $arrays, &$new = array(), $prefix = null ) { private static function http_build_query_for_curl($arrays, &$new = array(), $prefix = null)
{
if (is_object($arrays)) { if (is_object($arrays)) {
$arrays = get_object_vars($arrays); $arrays = get_object_vars($arrays);
} }
@@ -157,17 +162,22 @@
*/ */
private static function request($httpMethod, $url, $body = NULL, $headers = array(), $username = NULL, $password = NULL) private static function request($httpMethod, $url, $body = NULL, $headers = array(), $username = NULL, $password = NULL)
{ {
if ($headers == NULL) $headers = array(); if ($headers == NULL)
$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) {
@@ -185,7 +195,7 @@
$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));
@@ -219,11 +229,12 @@
return new HttpResponse($httpCode, $body, $header); return new HttpResponse($httpCode, $body, $header);
} }
private static function getArrayFromQuerystring($querystring) { private static function getArrayFromQuerystring($querystring)
{
$pairs = explode("&", $querystring); $pairs = explode("&", $querystring);
$vars = array(); $vars = array();
foreach ($pairs as $pair) { foreach ($pairs as $pair) {
$nv = explode("=", $pair); $nv = explode("=", $pair, 2);
$name = $nv[0]; $name = $nv[0];
$value = $nv[1]; $value = $nv[1];
$vars[$name] = $value; $vars[$name] = $value;
@@ -257,9 +268,9 @@
return $result; return $result;
} }
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;
} }
@@ -277,8 +288,7 @@
$len = strlen($chunk); $len = strlen($chunk);
$dechunk = null; $dechunk = null;
while (($pos < $len) while (($pos < $len) && ($chunkLenHex = substr($chunk, $pos, ($newlineAt = strpos($chunk, "\n", $pos + 1)) - $pos))) {
&& ($chunkLenHex = substr($chunk, $pos, ($newlineAt = strpos($chunk, "\n", $pos + 1)) - $pos))) {
if (!is_hex($chunkLenHex)) { if (!is_hex($chunkLenHex)) {
trigger_error('Value is not properly chunk encoded', E_USER_WARNING); trigger_error('Value is not properly chunk encoded', E_USER_WARNING);

View File

@@ -1,22 +1,19 @@
<?php <?php
echo "Running the Unirest-PHP bindings test suite.\n". echo "Running the Unirest-PHP bindings test suite.\n" . "If you're trying to use the Unirest-PHP bindings you'll probably want " . "to require('lib/Unirest.php'); instead of this file\n";
"If you're trying to use the Unirest-PHP bindings you'll probably want ".
"to require('lib/Unirest.php'); instead of this file\n";
$ok = @include_once(dirname(__FILE__) . '/simpletest/autorun.php'); $ok = @include_once(dirname(__FILE__) . '/simpletest/autorun.php');
if (!$ok) { if (!$ok) {
$ok = @include_once(dirname(__FILE__) . '/../vendor/vierbergenlars/simpletest/autorun.php'); $ok = @include_once(dirname(__FILE__) . '/../vendor/vierbergenlars/simpletest/autorun.php');
} }
if (!$ok) { if (!$ok) {
echo "MISSING DEPENDENCY: The Unirest-PHP test cases depend on SimpleTest. ". echo "MISSING DEPENDENCY: The Unirest-PHP test cases depend on SimpleTest. " . "Download it at <http://www.simpletest.org/>, and either install it " . "in your PHP include_path or put it in the test/ directory.\n";
"Download it at <http://www.simpletest.org/>, and either install it ".
"in your PHP include_path or put it in the test/ directory.\n";
exit(1); exit(1);
} }
// Throw an exception on any error // Throw an exception on any error
function exception_error_handler($errno, $errstr, $errfile, $errline) { function exception_error_handler($errno, $errstr, $errfile, $errline)
{
throw new ErrorException($errstr, $errno, 0, $errfile, $errline); throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
} }

View File

@@ -2,10 +2,12 @@
class UnirestTest extends UnitTestCase class UnirestTest extends UnitTestCase
{ {
public function testGet() public function testGet()
{ {
$response = Unirest::get("http://httpbin.org/get?name=Mark", array( "Accept" => "application/json" ), $response = Unirest::get("http://httpbin.org/get?name=Mark", array(
array( "Accept" => "application/json"
), array(
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -18,22 +20,30 @@ class UnirestTest extends UnitTestCase
public function testGetMultidimensionalArray() public function testGetMultidimensionalArray()
{ {
$response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), $response = Unirest::get("http://httpbin.org/get", array(
array('key'=>'value','items'=>array('item1','item2'))); "Accept" => "application/json"
), array(
'key' => 'value',
'items' => array(
'item1',
'item2'
)
));
$this->assertEqual(200, $response->code); $this->assertEqual(200, $response->code);
$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()
{ {
$response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), $response = Unirest::get("http://httpbin.org/get", array(
array( "Accept" => "application/json"
), array(
"user.name" => "Mark", "user.name" => "Mark",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -47,8 +57,9 @@ class UnirestTest extends UnitTestCase
public function testGetWithDots2() public function testGetWithDots2()
{ {
$response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ), $response = Unirest::get("http://httpbin.org/get", array(
array( "Accept" => "application/json"
), array(
"user.name" => "Mark Bond", "user.name" => "Mark Bond",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -56,14 +67,15 @@ 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);
} }
public function testPost() public function testPost()
{ {
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), $response = Unirest::post("http://httpbin.org/post", array(
array( "Accept" => "application/json"
), array(
"name" => "Mark", "name" => "Mark",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -75,10 +87,70 @@ class UnirestTest extends UnitTestCase
$this->assertEqual("thefosk", $form->nick); $this->assertEqual("thefosk", $form->nick);
} }
public function testPostWithEqualSign()
{
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
), array(
"name" => "Mark=Hello"
));
$this->assertEqual(200, $response->code);
$form = $response->body->form;
$this->assertEqual("Mark=Hello", $form->name);
}
public function testGetWithEqualSign()
{
$response = Unirest::get("http://httpbin.org/get", array(
"Accept" => "application/json"
), array(
"name" => "Mark=Hello"
));
$this->assertEqual(200, $response->code);
$args = $response->body->args;
$this->assertEqual("Mark=Hello", $args->name);
}
public function testPostArray()
{
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
), array(
"name[0]" => "Mark",
"name[1]" => "John"
));
$this->assertEqual(200, $response->code);
$form = $response->body->form;
$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() public function testPostWithDots()
{ {
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), $response = Unirest::post("http://httpbin.org/post", array(
array( "Accept" => "application/json"
), array(
"user.name" => "Mark", "user.name" => "Mark",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -92,8 +164,9 @@ class UnirestTest extends UnitTestCase
public function testRawPost() public function testRawPost()
{ {
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), $response = Unirest::post("http://httpbin.org/post", array(
json_encode(array( "Accept" => "application/json"
), json_encode(array(
"author" => "Sam Sullivan" "author" => "Sam Sullivan"
))); )));
@@ -103,21 +176,17 @@ class UnirestTest extends UnitTestCase
$this->assertEqual("Sam Sullivan", $json->author); $this->assertEqual("Sam Sullivan", $json->author);
} }
public function testUpload() { public function testUpload()
{
var_dump(file_get_contents(dirname(__FILE__) . "/test_upload.txt")); $response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), ), array(
array(
"name" => "Mark", "name" => "Mark",
"file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt") "file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt")
) ));
);
$this->assertEqual(200, $response->code); $this->assertEqual(200, $response->code);
$files = $response->body->files; $files = $response->body->files;
var_dump($response->body);
var_dump($files);
$this->assertEqual("This is a test", $files->file); $this->assertEqual("This is a test", $files->file);
$form = $response->body->form; $form = $response->body->form;
@@ -126,8 +195,15 @@ class UnirestTest extends UnitTestCase
public function testPostMultidimensionalArray() public function testPostMultidimensionalArray()
{ {
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ), $response = Unirest::post("http://httpbin.org/post", array(
array('key'=>'value','items'=>array('item1','item2'))); "Accept" => "application/json"
), array(
'key' => 'value',
'items' => array(
'item1',
'item2'
)
));
$this->assertEqual(200, $response->code); $this->assertEqual(200, $response->code);
@@ -139,8 +215,9 @@ class UnirestTest extends UnitTestCase
public function testPut() public function testPut()
{ {
$response = Unirest::put("http://httpbin.org/put", array( "Accept" => "application/json" ), $response = Unirest::put("http://httpbin.org/put", array(
array( "Accept" => "application/json"
), array(
"name" => "Mark", "name" => "Mark",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -154,8 +231,9 @@ class UnirestTest extends UnitTestCase
public function testPatch() public function testPatch()
{ {
$response = Unirest::patch("http://httpbin.org/patch", array( "Accept" => "application/json" ), $response = Unirest::patch("http://httpbin.org/patch", array(
array( "Accept" => "application/json"
), array(
"name" => "Mark", "name" => "Mark",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -169,8 +247,10 @@ class UnirestTest extends UnitTestCase
public function testDelete() public function testDelete()
{ {
$response = Unirest::delete("http://httpbin.org/delete", array( "Accept" => "application/json", "Content-Type" => "application/x-www-form-urlencoded" ), $response = Unirest::delete("http://httpbin.org/delete", array(
array( "Accept" => "application/json",
"Content-Type" => "application/x-www-form-urlencoded"
), array(
"name" => "Mark", "name" => "Mark",
"nick" => "thefosk" "nick" => "thefosk"
)); ));
@@ -240,4 +320,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'});
}
} }