11 Commits
1.1 ... 1.2.1

Author SHA1 Message Date
Marco Palladino
ff13801b7b Merge pull request #41 from scottmotte/master
It needs to go to root namespace
2014-04-05 16:28:21 -07:00
scottmotte
3951a37c2a It needs to go to root namespace 2014-04-04 12:24:36 -07:00
Marco Palladino
9ee16508d2 Merge pull request #39 from scottmotte/master
Fixes file attachments/uploads
2014-03-27 11:41:22 -07:00
scottmotte
0fe3d04e21 Fixes failing spec around CurlFile in 5.5 php and also adds additional spec 2014-03-27 10:47:09 -07:00
scottmotte
1f22deee76 Merge remote-tracking branch 'upstream/master' 2014-03-26 22:31:03 -07:00
thefosk
099fbca53b test 2014-03-19 12:25:27 -07:00
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
scottmotte
a332765a9b Minor spelling fix 2014-01-07 14:32:51 -08:00
5 changed files with 752 additions and 615 deletions

View File

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

View File

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

View File

@@ -1,22 +1,19 @@
<?php
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";
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";
$ok = @include_once(dirname(__FILE__) . '/simpletest/autorun.php');
if (!$ok) {
$ok = @include_once(dirname(__FILE__) . '/../vendor/vierbergenlars/simpletest/autorun.php');
}
if (!$ok) {
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";
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";
exit(1);
}
// 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);
}

View File

@@ -2,10 +2,12 @@
class UnirestTest extends UnitTestCase
{
public function testGet()
{
$response = Unirest::get("http://httpbin.org/get?name=Mark", array( "Accept" => "application/json" ),
array(
$response = Unirest::get("http://httpbin.org/get?name=Mark", array(
"Accept" => "application/json"
), array(
"nick" => "thefosk"
));
@@ -18,22 +20,30 @@ class UnirestTest extends UnitTestCase
public function testGetMultidimensionalArray()
{
$response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ),
array('key'=>'value','items'=>array('item1','item2')));
$response = Unirest::get("http://httpbin.org/get", array(
"Accept" => "application/json"
), array(
'key' => 'value',
'items' => array(
'item1',
'item2'
)
));
$this->assertEqual(200, $response->code);
$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()
{
$response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ),
array(
$response = Unirest::get("http://httpbin.org/get", array(
"Accept" => "application/json"
), array(
"user.name" => "Mark",
"nick" => "thefosk"
));
@@ -47,8 +57,9 @@ class UnirestTest extends UnitTestCase
public function testGetWithDots2()
{
$response = Unirest::get("http://httpbin.org/get", array( "Accept" => "application/json" ),
array(
$response = Unirest::get("http://httpbin.org/get", array(
"Accept" => "application/json"
), array(
"user.name" => "Mark Bond",
"nick" => "thefosk"
));
@@ -56,14 +67,15 @@ 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);
}
public function testPost()
{
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
array(
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
), array(
"name" => "Mark",
"nick" => "thefosk"
));
@@ -75,10 +87,81 @@ class UnirestTest extends UnitTestCase
$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);
$response = Unirest::get("http://httpbin.org/get", array(
"Accept" => "application/json"
), array(
"name" => "Mark=Hello=John"
));
$this->assertEqual(200, $response->code);
$args = $response->body->args;
$this->assertEqual("Mark=Hello=John", $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()
{
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
array(
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
), array(
"user.name" => "Mark",
"nick" => "thefosk"
));
@@ -92,8 +175,9 @@ class UnirestTest extends UnitTestCase
public function testRawPost()
{
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
json_encode(array(
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
), json_encode(array(
"author" => "Sam Sullivan"
)));
@@ -103,31 +187,51 @@ class UnirestTest extends UnitTestCase
$this->assertEqual("Sam Sullivan", $json->author);
}
public function testUpload() {
var_dump(file_get_contents(dirname(__FILE__) . "/test_upload.txt"));
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
array(
public function testUpload()
{
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
), array(
"name" => "Mark",
"file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt")
)
);
));
$this->assertEqual(200, $response->code);
$files = $response->body->files;
var_dump($response->body);
var_dump($files);
$this->assertEqual("This is a test", $files->file);
$form = $response->body->form;
$this->assertEqual("Mark", $form->name);
}
public function testUploadIfFilePartOfData()
{
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
), array(
"name" => "Mark",
"files[owl.gif]" => Unirest::file(dirname(__FILE__) . "/test_upload.txt")
));
$this->assertEqual(200, $response->code);
//$files = $response->body->files;
//$this->assertEqual("This is a test", $files->file);
$form = $response->body->form;
$this->assertEqual("Mark", $form->name);
}
public function testPostMultidimensionalArray()
{
$response = Unirest::post("http://httpbin.org/post", array( "Accept" => "application/json" ),
array('key'=>'value','items'=>array('item1','item2')));
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
), array(
'key' => 'value',
'items' => array(
'item1',
'item2'
)
));
$this->assertEqual(200, $response->code);
@@ -139,8 +243,9 @@ class UnirestTest extends UnitTestCase
public function testPut()
{
$response = Unirest::put("http://httpbin.org/put", array( "Accept" => "application/json" ),
array(
$response = Unirest::put("http://httpbin.org/put", array(
"Accept" => "application/json"
), array(
"name" => "Mark",
"nick" => "thefosk"
));
@@ -154,8 +259,9 @@ class UnirestTest extends UnitTestCase
public function testPatch()
{
$response = Unirest::patch("http://httpbin.org/patch", array( "Accept" => "application/json" ),
array(
$response = Unirest::patch("http://httpbin.org/patch", array(
"Accept" => "application/json"
), array(
"name" => "Mark",
"nick" => "thefosk"
));
@@ -169,8 +275,10 @@ class UnirestTest extends UnitTestCase
public function testDelete()
{
$response = Unirest::delete("http://httpbin.org/delete", array( "Accept" => "application/json", "Content-Type" => "application/x-www-form-urlencoded" ),
array(
$response = Unirest::delete("http://httpbin.org/delete", array(
"Accept" => "application/json",
"Content-Type" => "application/x-www-form-urlencoded"
), array(
"name" => "Mark",
"nick" => "thefosk"
));
@@ -240,4 +348,27 @@ 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'});
}
public function testHttpBuildQueryWhenCurlFile()
{
$file = Unirest::file(dirname(__FILE__) . "/test_upload.txt");
$body = array(
"to" => "mail@mailinator.com",
"from" => "mail@mailinator.com",
"file" => $file
);
Unirest::http_build_query_for_curl($body, $postBody);
$this->assertEqual($postBody['file'], $file);
}
}