massive cleanups
- renamed classes - updated to phpunit - more PSR compliance
This commit is contained in:
@@ -13,7 +13,3 @@ tools:
|
|||||||
php_cs_fixer:
|
php_cs_fixer:
|
||||||
config:
|
config:
|
||||||
level: 'psr2'
|
level: 'psr2'
|
||||||
|
|
||||||
filter:
|
|
||||||
excluded_paths:
|
|
||||||
- 'test/*'
|
|
||||||
|
|||||||
@@ -4,8 +4,5 @@ php:
|
|||||||
- 5.3
|
- 5.3
|
||||||
- 5.4
|
- 5.4
|
||||||
- 5.5
|
- 5.5
|
||||||
|
- 5.6
|
||||||
before_script:
|
- hhvm
|
||||||
- sh -c "if [ '$TRAVIS_PHP_VERSION' = '5.2' ]; then wget http://iweb.dl.sourceforge.net/project/simpletest/simpletest/simpletest_1.1/simpletest_1.1.0.tar.gz; tar xf simpletest_1.1.0.tar.gz -C test; else composer install --dev --prefer-source; fi"
|
|
||||||
|
|
||||||
script: php test/Unirest.php
|
|
||||||
|
|||||||
@@ -15,10 +15,11 @@
|
|||||||
"require" : {
|
"require" : {
|
||||||
"php" : ">=5.3.0",
|
"php" : ">=5.3.0",
|
||||||
"ext-curl" : "*",
|
"ext-curl" : "*",
|
||||||
"ext-json" : "*"
|
"ext-json" : "*",
|
||||||
|
"ext-http" : "*"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"vierbergenlars/simpletest": "*"
|
"phpunit/phpunit": "*"
|
||||||
},
|
},
|
||||||
"autoload" : {
|
"autoload" : {
|
||||||
"psr-0" : {
|
"psr-0" : {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once dirname(__FILE__) . '/Unirest/HttpMethod.php';
|
require_once dirname(__FILE__) . '/Unirest/Method.php';
|
||||||
require_once dirname(__FILE__) . '/Unirest/HttpResponse.php';
|
require_once dirname(__FILE__) . '/Unirest/Response.php';
|
||||||
require_once dirname(__FILE__) . '/Unirest/Unirest.php';
|
require_once dirname(__FILE__) . '/Unirest/Request.php';
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Unirest;
|
namespace Unirest;
|
||||||
|
|
||||||
interface HttpMethod
|
interface Method
|
||||||
{
|
{
|
||||||
const DELETE = 'DELETE';
|
const DELETE = 'DELETE';
|
||||||
const GET = 'GET';
|
const GET = 'GET';
|
||||||
@@ -1,9 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use Unirest\HttpMethod;
|
use Unirest\method;
|
||||||
use Unirest\HttpResponse;
|
use Unirest\Response;
|
||||||
|
|
||||||
class Unirest
|
namespace Unirest;
|
||||||
|
|
||||||
|
class Request
|
||||||
{
|
{
|
||||||
private static $verifyPeer = true;
|
private static $verifyPeer = true;
|
||||||
private static $socketTimeout = null;
|
private static $socketTimeout = null;
|
||||||
@@ -56,7 +58,7 @@ class Unirest
|
|||||||
*/
|
*/
|
||||||
public static function get($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
public static function get($url, $headers = array(), $parameters = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
return self::request(HttpMethod::GET, $url, $parameters, $headers, $username, $password);
|
return self::request(Method::GET, $url, $parameters, $headers, $username, $password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -70,7 +72,7 @@ class Unirest
|
|||||||
*/
|
*/
|
||||||
public static function post($url, $headers = array(), $body = null, $username = null, $password = null)
|
public static function post($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
return self::request(HttpMethod::POST, $url, $body, $headers, $username, $password);
|
return self::request(Method::POST, $url, $body, $headers, $username, $password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -84,7 +86,7 @@ class Unirest
|
|||||||
*/
|
*/
|
||||||
public static function delete($url, $headers = array(), $body = null, $username = null, $password = null)
|
public static function delete($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
return self::request(HttpMethod::DELETE, $url, $body, $headers, $username, $password);
|
return self::request(Method::DELETE, $url, $body, $headers, $username, $password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -98,7 +100,7 @@ class Unirest
|
|||||||
*/
|
*/
|
||||||
public static function put($url, $headers = array(), $body = null, $username = null, $password = null)
|
public static function put($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
return self::request(HttpMethod::PUT, $url, $body, $headers, $username, $password);
|
return self::request(Method::PUT, $url, $body, $headers, $username, $password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -112,7 +114,7 @@ class Unirest
|
|||||||
*/
|
*/
|
||||||
public static function patch($url, $headers = array(), $body = null, $username = null, $password = null)
|
public static function patch($url, $headers = array(), $body = null, $username = null, $password = null)
|
||||||
{
|
{
|
||||||
return self::request(HttpMethod::PATCH, $url, $body, $headers, $username, $password);
|
return self::request(Method::PATCH, $url, $body, $headers, $username, $password);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -121,16 +123,16 @@ class Unirest
|
|||||||
*/
|
*/
|
||||||
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 {
|
||||||
return "@" . $path;
|
return '@' . $path;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
||||||
*/
|
*/
|
||||||
public static function buildHTTPCurlQuery($arrays, &$new = array(), $prefix = null)
|
public static function buildHTTPCurlQuery($arrays, &$new = array(), $prefix = null)
|
||||||
{
|
{
|
||||||
@@ -150,41 +152,41 @@ class Unirest
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a cURL request
|
* Send a cURL request
|
||||||
* @param string $httpMethod HTTP method to use (based off \Unirest\HttpMethod constants)
|
* @param Unirest\Method $method HTTP method to use
|
||||||
* @param string $url URL to send the request to
|
* @param string $url URL to send the request to
|
||||||
* @param mixed $body request body
|
* @param mixed $body request body
|
||||||
* @param array $headers additional headers to send
|
* @param array $headers additional headers to send
|
||||||
* @param string $username Basic Authentication username
|
* @param string $username Basic Authentication username
|
||||||
* @param string $password Basic Authentication password
|
* @param string $password Basic Authentication password
|
||||||
* @throws Exception if a cURL error occurs
|
* @throws Exception if a cURL error occurs
|
||||||
* @return HttpResponse
|
* @return Unirest\Response
|
||||||
*/
|
*/
|
||||||
private static function request($httpMethod, $url, $body = null, $headers = array(), $username = null, $password = null, $json_decode_assoc = false)
|
private static function request($method, $url, $body = null, $headers = array(), $username = null, $password = null)
|
||||||
{
|
{
|
||||||
if ($headers == null) {
|
if ($headers == null) {
|
||||||
$headers = array();
|
$headers = array();
|
||||||
}
|
}
|
||||||
|
|
||||||
$lowercaseHeaders = array();
|
$lowercaseHeaders = array();
|
||||||
$finalHeaders = array_merge($headers, Unirest::$defaultHeaders);
|
$finalHeaders = array_merge($headers, self::$defaultHeaders);
|
||||||
foreach ($finalHeaders as $key => $val) {
|
foreach ($finalHeaders as $key => $val) {
|
||||||
$lowercaseHeaders[] = self::getHeader($key, $val);
|
$lowercaseHeaders[] = self::getHeader($key, $val);
|
||||||
}
|
}
|
||||||
|
|
||||||
$lowerCaseFinalHeaders = array_change_key_case($finalHeaders);
|
$lowerCaseFinalHeaders = array_change_key_case($finalHeaders);
|
||||||
|
|
||||||
if (!array_key_exists("user-agent", $lowerCaseFinalHeaders)) {
|
if (!array_key_exists('user-agent', $lowerCaseFinalHeaders)) {
|
||||||
$lowercaseHeaders[] = "user-agent: unirest-php/1.1";
|
$lowercaseHeaders[] = 'user-agent: unirest-php/2.0';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!array_key_exists("expect", $lowerCaseFinalHeaders)) {
|
if (!array_key_exists('expect', $lowerCaseFinalHeaders)) {
|
||||||
$lowercaseHeaders[] = "expect:";
|
$lowercaseHeaders[] = 'expect:';
|
||||||
}
|
}
|
||||||
|
|
||||||
$ch = curl_init();
|
$ch = curl_init();
|
||||||
|
|
||||||
if ($httpMethod != HttpMethod::GET) {
|
if ($method != Method::GET) {
|
||||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
||||||
|
|
||||||
if (is_array($body) || $body instanceof Traversable) {
|
if (is_array($body) || $body instanceof Traversable) {
|
||||||
self::buildHTTPCurlQuery($body, $postBody);
|
self::buildHTTPCurlQuery($body, $postBody);
|
||||||
@@ -194,9 +196,9 @@ class Unirest
|
|||||||
}
|
}
|
||||||
} elseif (is_array($body)) {
|
} elseif (is_array($body)) {
|
||||||
if (strpos($url, '?') !== false) {
|
if (strpos($url, '?') !== false) {
|
||||||
$url .= "&";
|
$url .= '&';
|
||||||
} else {
|
} else {
|
||||||
$url .= "?";
|
$url .= '?';
|
||||||
}
|
}
|
||||||
|
|
||||||
self::buildHTTPCurlQuery($body, $postBody);
|
self::buildHTTPCurlQuery($body, $postBody);
|
||||||
@@ -210,39 +212,39 @@ class Unirest
|
|||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $lowercaseHeaders);
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $lowercaseHeaders);
|
||||||
curl_setopt($ch, CURLOPT_HEADER, true);
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
||||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, self::$verifyPeer);
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, self::$verifyPeer);
|
||||||
curl_setopt($ch, CURLOPT_ENCODING, ""); // If an empty string, "", is set, a header containing all supported encoding types is sent.
|
curl_setopt($ch, CURLOPT_ENCODING, ''); // If an empty string, '', is set, a header containing all supported encoding types is sent.
|
||||||
|
|
||||||
if (self::$socketTimeout != null) {
|
if (self::$socketTimeout != null) {
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
|
curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($username)) {
|
if (!empty($username)) {
|
||||||
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . ((empty($password)) ? "" : $password));
|
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . ((empty($password)) ? '' : $password));
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
$error = curl_error($ch);
|
$error = curl_error($ch);
|
||||||
|
|
||||||
if ($error) {
|
if ($error) {
|
||||||
throw new Exception($error);
|
throw new \Exception($error);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split the full response in its headers and body
|
// Split the full response in its headers and body
|
||||||
$curl_info = curl_getinfo($ch);
|
$curl_info = curl_getinfo($ch);
|
||||||
$header_size = $curl_info["header_size"];
|
$header_size = $curl_info['header_size'];
|
||||||
$header = substr($response, 0, $header_size);
|
$header = substr($response, 0, $header_size);
|
||||||
$body = substr($response, $header_size);
|
$body = substr($response, $header_size);
|
||||||
$httpCode = $curl_info["http_code"];
|
$httpCode = $curl_info['http_code'];
|
||||||
|
|
||||||
return new HttpResponse($httpCode, $body, $header, $json_decode_assoc);
|
return new Response($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, 2);
|
$nv = explode('=', $pair, 2);
|
||||||
$name = $nv[0];
|
$name = $nv[0];
|
||||||
$value = $nv[1];
|
$value = $nv[1];
|
||||||
$vars[$name] = $value;
|
$vars[$name] = $value;
|
||||||
@@ -269,8 +271,8 @@ class Unirest
|
|||||||
$query = '?' . http_build_query(self::getArrayFromQuerystring($url_parsed['query']));
|
$query = '?' . http_build_query(self::getArrayFromQuerystring($url_parsed['query']));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($port && $port[0] != ":") {
|
if ($port && $port[0] != ':') {
|
||||||
$port = ":" . $port;
|
$port = ':' . $port;
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = $scheme . $host . $port . $path . $query;
|
$result = $scheme . $host . $port . $path . $query;
|
||||||
@@ -280,35 +282,6 @@ class Unirest
|
|||||||
private static function getHeader($key, $val)
|
private static function getHeader($key, $val)
|
||||||
{
|
{
|
||||||
$key = trim(strtolower($key));
|
$key = trim(strtolower($key));
|
||||||
return $key . ": " . $val;
|
return $key . ': ' . $val;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!function_exists('http_chunked_decode')) {
|
|
||||||
/**
|
|
||||||
* Dechunk an http 'transfer-encoding: chunked' message
|
|
||||||
* @param string $chunk the encoded message
|
|
||||||
* @return string the decoded message
|
|
||||||
*/
|
|
||||||
function http_chunked_decode($chunk)
|
|
||||||
{
|
|
||||||
$pos = 0;
|
|
||||||
$len = strlen($chunk);
|
|
||||||
$dechunk = null;
|
|
||||||
|
|
||||||
while (($pos < $len) && ($chunkLenHex = substr($chunk, $pos, ($newlineAt = strpos($chunk, "\n", $pos + 1)) - $pos))) {
|
|
||||||
|
|
||||||
if (!ctype_xdigit($chunkLenHex)) {
|
|
||||||
trigger_error('Value is not properly chunk encoded', E_USER_WARNING);
|
|
||||||
return $chunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
$pos = $newlineAt + 1;
|
|
||||||
$chunkLen = hexdec(rtrim($chunkLenHex, "\r\n"));
|
|
||||||
$dechunk .= substr($chunk, $pos, $chunkLen);
|
|
||||||
$pos = strpos($chunk, "\n", $pos + $chunkLen) + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $dechunk;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Unirest;
|
namespace Unirest;
|
||||||
|
|
||||||
class HttpResponse
|
class Response
|
||||||
{
|
{
|
||||||
|
|
||||||
private $code;
|
private $code;
|
||||||
@@ -15,13 +15,13 @@ class HttpResponse
|
|||||||
* @param string $raw_body the raw body of the cURL response
|
* @param string $raw_body the raw body of the cURL response
|
||||||
* @param string $headers raw header string from cURL response
|
* @param string $headers raw header string from cURL response
|
||||||
*/
|
*/
|
||||||
public function __construct($code, $raw_body, $headers, $json_decode_assoc = false)
|
public function __construct($code, $raw_body, $headers)
|
||||||
{
|
{
|
||||||
$this->code = $code;
|
$this->code = $code;
|
||||||
$this->headers = $this->getHeadersFromCurlResponse($headers);
|
$this->headers = $this->getHeadersFromCurlResponse($headers);
|
||||||
$this->raw_body = $raw_body;
|
$this->raw_body = $raw_body;
|
||||||
$this->body = $raw_body;
|
$this->body = $raw_body;
|
||||||
$json = json_decode($raw_body, $json_decode_assoc);
|
$json = json_decode($raw_body);
|
||||||
|
|
||||||
if (json_last_error() === JSON_ERROR_NONE) {
|
if (json_last_error() === JSON_ERROR_NONE) {
|
||||||
$this->body = $json;
|
$this->body = $json;
|
||||||
12
phpunit.xml
Normal file
12
phpunit.xml
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<phpunit colors="true">
|
||||||
|
<testsuites>
|
||||||
|
<testsuite name="Unirest Test Suite">
|
||||||
|
<directory>./test</directory>
|
||||||
|
</testsuite>
|
||||||
|
</testsuites>
|
||||||
|
|
||||||
|
<php>
|
||||||
|
<ini name="date.timezone" value="UTC"/>
|
||||||
|
</php>
|
||||||
|
</phpunit>
|
||||||
1
test/.gitignore
vendored
1
test/.gitignore
vendored
@@ -1 +0,0 @@
|
|||||||
simpletest
|
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
<?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";
|
|
||||||
|
|
||||||
$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";
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Throw an exception on any error
|
|
||||||
function exception_error_handler($errno, $errstr, $errfile, $errline)
|
|
||||||
{
|
|
||||||
throw new ErrorException($errstr, $errno, 0, $errfile, $errline);
|
|
||||||
}
|
|
||||||
|
|
||||||
set_error_handler('exception_error_handler');
|
|
||||||
error_reporting(E_ALL | E_STRICT);
|
|
||||||
|
|
||||||
require_once(dirname(__FILE__) . '/../lib/Unirest.php');
|
|
||||||
|
|
||||||
require_once(dirname(__FILE__) . '/Unirest/UnirestTest.php');
|
|
||||||
|
|
||||||
?>
|
|
||||||
@@ -1,27 +1,33 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
class UnirestTest extends UnitTestCase
|
use Unirest\Request as Request;
|
||||||
|
|
||||||
|
namespace Unirest;
|
||||||
|
|
||||||
|
define('UPLOAD_FIXTURE', dirname(dirname(__FILE__)) . '/fixtures/upload.txt');
|
||||||
|
|
||||||
|
class UnirestTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
public function testGet()
|
public function testGet()
|
||||||
{
|
{
|
||||||
$response = Unirest::get("http://httpbin.org/get?name=Mark", array(
|
$response = Request::get('http://httpbin.org/get?name=Mark', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"nick" => "thefosk"
|
'nick' => 'thefosk'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$args = $response->body->args;
|
$args = $response->body->args;
|
||||||
$this->assertEqual("Mark", $args->name);
|
$this->assertEquals('Mark', $args->name);
|
||||||
$this->assertEqual("thefosk", $args->nick);
|
$this->assertEquals('thefosk', $args->nick);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetMultidimensionalArray()
|
public function testGetMultidimensionalArray()
|
||||||
{
|
{
|
||||||
$response = Unirest::get("http://httpbin.org/get", array(
|
$response = Request::get('http://httpbin.org/get', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
'key' => 'value',
|
'key' => 'value',
|
||||||
'items' => array(
|
'items' => array(
|
||||||
@@ -30,201 +36,203 @@ class UnirestTest extends UnitTestCase
|
|||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$args = $response->body->args;
|
$args = $response->body->args;
|
||||||
|
|
||||||
$this->assertEqual("value", $args->key);
|
$this->assertEquals('value', $args->key);
|
||||||
$this->assertEqual("item1", $args->{"items[0]"});
|
$this->assertEquals('item1', $args->{'items[0]'});
|
||||||
$this->assertEqual("item2", $args->{"items[1]"});
|
$this->assertEquals('item2', $args->{'items[1]'});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetWithDots()
|
public function testGetWithDots()
|
||||||
{
|
{
|
||||||
$response = Unirest::get("http://httpbin.org/get", array(
|
$response = Request::get('http://httpbin.org/get', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"user.name" => "Mark",
|
'user.name' => 'Mark',
|
||||||
"nick" => "thefosk"
|
'nick' => 'thefosk'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$args = $response->body->args;
|
$args = $response->body->args;
|
||||||
$this->assertEqual("Mark", $args->{"user.name"});
|
$this->assertEquals('Mark', $args->{'user.name'});
|
||||||
$this->assertEqual("thefosk", $args->nick);
|
$this->assertEquals('thefosk', $args->nick);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetWithDots2()
|
public function testGetWithDots2()
|
||||||
{
|
{
|
||||||
$response = Unirest::get("http://httpbin.org/get", array(
|
$response = Request::get('http://httpbin.org/get', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"user.name" => "Mark Bond",
|
'user.name' => 'Mark Bond',
|
||||||
"nick" => "thefosk"
|
'nick' => 'thefosk'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$args = $response->body->args;
|
$args = $response->body->args;
|
||||||
$this->assertEqual("Mark Bond", $args->{"user.name"});
|
$this->assertEquals('Mark Bond', $args->{'user.name'});
|
||||||
$this->assertEqual("thefosk", $args->nick);
|
$this->assertEquals('thefosk', $args->nick);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPost()
|
public function testPost()
|
||||||
{
|
{
|
||||||
$response = Unirest::post("http://httpbin.org/post", array(
|
$response = Request::post('http://httpbin.org/post', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"name" => "Mark",
|
'name' => 'Mark',
|
||||||
"nick" => "thefosk"
|
'nick' => 'thefosk'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$form = $response->body->form;
|
$form = $response->body->form;
|
||||||
$this->assertEqual("Mark", $form->name);
|
$this->assertEquals('Mark', $form->name);
|
||||||
$this->assertEqual("thefosk", $form->nick);
|
$this->assertEquals('thefosk', $form->nick);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPostWithEqualSign()
|
public function testPostWithEqualSign()
|
||||||
{
|
{
|
||||||
$response = Unirest::post("http://httpbin.org/post", array(
|
$response = Request::post('http://httpbin.org/post', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"name" => "Mark=Hello"
|
'name' => 'Mark=Hello'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$form = $response->body->form;
|
$form = $response->body->form;
|
||||||
$this->assertEqual("Mark=Hello", $form->name);
|
$this->assertEquals('Mark=Hello', $form->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetWithEqualSign()
|
public function testGetWithEqualSign()
|
||||||
{
|
{
|
||||||
$response = Unirest::get("http://httpbin.org/get", array(
|
$response = Request::get('http://httpbin.org/get', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"name" => "Mark=Hello"
|
'name' => 'Mark=Hello'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$args = $response->body->args;
|
$args = $response->body->args;
|
||||||
$this->assertEqual("Mark=Hello", $args->name);
|
$this->assertEquals('Mark=Hello', $args->name);
|
||||||
|
|
||||||
$response = Unirest::get("http://httpbin.org/get", array(
|
$response = Request::get('http://httpbin.org/get', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"name" => "Mark=Hello=John"
|
'name' => 'Mark=Hello=John'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$args = $response->body->args;
|
$args = $response->body->args;
|
||||||
$this->assertEqual("Mark=Hello=John", $args->name);
|
$this->assertEquals('Mark=Hello=John', $args->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPostArray()
|
public function testPostArray()
|
||||||
{
|
{
|
||||||
$response = Unirest::post("http://httpbin.org/post", array(
|
$response = Request::post('http://httpbin.org/post', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"name[0]" => "Mark",
|
'name[0]' => 'Mark',
|
||||||
"name[1]" => "John"
|
'name[1]' => 'John'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$form = $response->body->form;
|
$form = $response->body->form;
|
||||||
|
|
||||||
$this->assertEqual("Mark", $form->{"name[0]"});
|
$this->assertEquals('Mark', $form->{'name[0]'});
|
||||||
$this->assertEqual("John", $form->{"name[1]"});
|
$this->assertEquals('John', $form->{'name[1]'});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetArray()
|
public function testGetArray()
|
||||||
{
|
{
|
||||||
$response = Unirest::get("http://httpbin.org/get", array(), array(
|
$response = Request::get('http://httpbin.org/get', array(), array(
|
||||||
"name[0]" => "Mark",
|
'name[0]' => 'Mark',
|
||||||
"name[1]" => "John"
|
'name[1]' => 'John'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$args = $response->body->args;
|
$args = $response->body->args;
|
||||||
$this->assertEqual("Mark", $args->{"name[0]"});
|
$this->assertEquals('Mark', $args->{'name[0]'});
|
||||||
$this->assertEqual("John", $args->{"name[1]"});
|
$this->assertEquals('John', $args->{'name[1]'});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPostWithDots()
|
public function testPostWithDots()
|
||||||
{
|
{
|
||||||
$response = Unirest::post("http://httpbin.org/post", array(
|
$response = Request::post('http://httpbin.org/post', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"user.name" => "Mark",
|
'user.name' => 'Mark',
|
||||||
"nick" => "thefosk"
|
'nick' => 'thefosk'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$form = $response->body->form;
|
$form = $response->body->form;
|
||||||
$this->assertEqual("Mark", $form->{"user.name"});
|
$this->assertEquals('Mark', $form->{'user.name'});
|
||||||
$this->assertEqual("thefosk", $form->nick);
|
$this->assertEquals('thefosk', $form->nick);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRawPost()
|
public function testRawPost()
|
||||||
{
|
{
|
||||||
$response = Unirest::post("http://httpbin.org/post", array(
|
$response = Request::post('http://httpbin.org/post', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json',
|
||||||
|
'Content-Type' => 'application/json'
|
||||||
), json_encode(array(
|
), json_encode(array(
|
||||||
"author" => "Sam Sullivan"
|
'author' => 'Sam Sullivan'
|
||||||
)));
|
)));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$json = $response->body->json;
|
$json = $response->body->json;
|
||||||
$this->assertEqual("Sam Sullivan", $json->author);
|
|
||||||
|
$this->assertEquals('Sam Sullivan', $json->author);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUpload()
|
public function testUpload()
|
||||||
{
|
{
|
||||||
$response = Unirest::post("http://httpbin.org/post", array(
|
$response = Request::post('http://httpbin.org/post', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"name" => "Mark",
|
'name' => 'Mark',
|
||||||
"file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt")
|
'file' => Request::file(UPLOAD_FIXTURE)
|
||||||
));
|
));
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$files = $response->body->files;
|
$files = $response->body->files;
|
||||||
$this->assertEqual("This is a test", $files->file);
|
$this->assertEquals('This is a test', $files->file);
|
||||||
|
|
||||||
$form = $response->body->form;
|
$form = $response->body->form;
|
||||||
$this->assertEqual("Mark", $form->name);
|
$this->assertEquals('Mark', $form->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testUploadIfFilePartOfData()
|
public function testUploadIfFilePartOfData()
|
||||||
{
|
{
|
||||||
$response = Unirest::post("http://httpbin.org/post", array(
|
$response = Request::post('http://httpbin.org/post', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"name" => "Mark",
|
'name' => 'Mark',
|
||||||
"files[owl.gif]" => Unirest::file(dirname(__FILE__) . "/test_upload.txt")
|
'files[owl.gif]' => Request::file(UPLOAD_FIXTURE)
|
||||||
));
|
));
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
//$files = $response->body->files;
|
//$files = $response->body->files;
|
||||||
//$this->assertEqual("This is a test", $files->file);
|
//$this->assertEquals('This is a test', $files->file);
|
||||||
|
|
||||||
$form = $response->body->form;
|
$form = $response->body->form;
|
||||||
$this->assertEqual("Mark", $form->name);
|
$this->assertEquals('Mark', $form->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPostMultidimensionalArray()
|
public function testPostMultidimensionalArray()
|
||||||
{
|
{
|
||||||
$response = Unirest::post("http://httpbin.org/post", array(
|
$response = Request::post('http://httpbin.org/post', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
'key' => 'value',
|
'key' => 'value',
|
||||||
'items' => array(
|
'items' => array(
|
||||||
@@ -233,142 +241,144 @@ class UnirestTest extends UnitTestCase
|
|||||||
)
|
)
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$form = $response->body->form;
|
$form = $response->body->form;
|
||||||
$this->assertEqual("value", $form->key);
|
$this->assertEquals('value', $form->key);
|
||||||
$this->assertEqual("item1", $form->{"items[0]"});
|
$this->assertEquals('item1', $form->{'items[0]'});
|
||||||
$this->assertEqual("item2", $form->{"items[1]"});
|
$this->assertEquals('item2', $form->{'items[1]'});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPut()
|
public function testPut()
|
||||||
{
|
{
|
||||||
$response = Unirest::put("http://httpbin.org/put", array(
|
$response = Request::put('http://httpbin.org/put', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"name" => "Mark",
|
'name' => 'Mark',
|
||||||
"nick" => "thefosk"
|
'nick' => 'thefosk'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$form = $response->body->form;
|
$form = $response->body->form;
|
||||||
$this->assertEqual("Mark", $form->name);
|
$this->assertEquals('Mark', $form->name);
|
||||||
$this->assertEqual("thefosk", $form->nick);
|
$this->assertEquals('thefosk', $form->nick);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPatch()
|
public function testPatch()
|
||||||
{
|
{
|
||||||
$response = Unirest::patch("http://httpbin.org/patch", array(
|
$response = Request::patch('http://httpbin.org/patch', array(
|
||||||
"Accept" => "application/json"
|
'Accept' => 'application/json'
|
||||||
), array(
|
), array(
|
||||||
"name" => "Mark",
|
'name' => 'Mark',
|
||||||
"nick" => "thefosk"
|
'nick' => 'thefosk'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$form = $response->body->form;
|
$form = $response->body->form;
|
||||||
$this->assertEqual("Mark", $form->name);
|
$this->assertEquals('Mark', $form->name);
|
||||||
$this->assertEqual("thefosk", $form->nick);
|
$this->assertEquals('thefosk', $form->nick);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDelete()
|
public function testDelete()
|
||||||
{
|
{
|
||||||
$response = Unirest::delete("http://httpbin.org/delete", array(
|
$response = Request::delete('http://httpbin.org/delete', array(
|
||||||
"Accept" => "application/json",
|
'Accept' => 'application/json',
|
||||||
"Content-Type" => "application/x-www-form-urlencoded"
|
'Content-Type' => 'application/x-www-form-urlencoded'
|
||||||
), array(
|
), array(
|
||||||
"name" => "Mark",
|
'name' => 'Mark',
|
||||||
"nick" => "thefosk"
|
'nick' => 'thefosk'
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
$data = $response->body->data;
|
$data = $response->body->data;
|
||||||
$this->assertFalse(empty($data));
|
$this->assertTrue(empty($data));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException Exception
|
||||||
|
*/
|
||||||
public function testTimeoutFail()
|
public function testTimeoutFail()
|
||||||
{
|
{
|
||||||
Unirest::timeout(1);
|
Request::timeout(1);
|
||||||
|
|
||||||
$this->expectException();
|
$response = Request::get('http://httpbin.org/delay/3');
|
||||||
$response = Unirest::get("http://httpbin.org/delay/3");
|
|
||||||
|
|
||||||
Unirest::timeout(null); // Cleaning timeout for the other tests
|
Request::timeout(null); // Cleaning timeout for the other tests
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testTimeoutSuccess()
|
public function testTimeoutSuccess()
|
||||||
{
|
{
|
||||||
Unirest::timeout(3);
|
Request::timeout(3);
|
||||||
|
|
||||||
$response = Unirest::get("http://httpbin.org/delay/1");
|
$response = Request::get('http://httpbin.org/delay/1');
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
Unirest::timeout(null); // Cleaning timeout for the other tests
|
Request::timeout(null); // Cleaning timeout for the other tests
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testDefaultHeader()
|
public function testDefaultHeader()
|
||||||
{
|
{
|
||||||
Unirest::defaultHeader("Hello", "custom");
|
Request::defaultHeader('Hello', 'custom');
|
||||||
$response = Unirest::get("http://httpbin.org/get");
|
$response = Request::get('http://httpbin.org/get');
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
$headers = $response->body->headers;
|
$headers = $response->body->headers;
|
||||||
$properties = get_object_vars($headers);
|
$properties = get_object_vars($headers);
|
||||||
$this->assertTrue(array_key_exists("Hello", $properties));
|
$this->assertTrue(array_key_exists('Hello', $properties));
|
||||||
$this->assertEqual("custom", $headers->Hello);
|
$this->assertEquals('custom', $headers->Hello);
|
||||||
$response = Unirest::get("http://httpbin.org/get");
|
$response = Request::get('http://httpbin.org/get');
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
$headers = $response->body->headers;
|
$headers = $response->body->headers;
|
||||||
$properties = get_object_vars($headers);
|
$properties = get_object_vars($headers);
|
||||||
$this->assertTrue(array_key_exists("Hello", $properties));
|
$this->assertTrue(array_key_exists('Hello', $properties));
|
||||||
$this->assertEqual("custom", $headers->Hello);
|
$this->assertEquals('custom', $headers->Hello);
|
||||||
Unirest::clearDefaultHeaders();
|
Request::clearDefaultHeaders();
|
||||||
$response = Unirest::get("http://httpbin.org/get");
|
$response = Request::get('http://httpbin.org/get');
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
$headers = $response->body->headers;
|
$headers = $response->body->headers;
|
||||||
$properties = get_object_vars($headers);
|
$properties = get_object_vars($headers);
|
||||||
$this->assertFalse(array_key_exists("Hello", $properties));
|
$this->assertFalse(array_key_exists('Hello', $properties));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGzip()
|
public function testGzip()
|
||||||
{
|
{
|
||||||
$response = Unirest::get("http://httpbin.org/gzip");
|
$response = Request::get('http://httpbin.org/gzip');
|
||||||
$args = $response->body;
|
$args = $response->body;
|
||||||
$this->assertEqual(true, $args->gzipped);
|
$this->assertEquals(true, $args->gzipped);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testBasicAuthentication()
|
public function testBasicAuthentication()
|
||||||
{
|
{
|
||||||
$response = Unirest::get("http://httpbin.org/get", null, null, "user", "password");
|
$response = Request::get('http://httpbin.org/get', null, null, 'user', 'password');
|
||||||
$headers = $response->body->headers;
|
$headers = $response->body->headers;
|
||||||
$this->assertEqual("Basic dXNlcjpwYXNzd29yZA==", $headers->Authorization);
|
$this->assertEquals('Basic dXNlcjpwYXNzd29yZA==', $headers->Authorization);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testCustomHeaders()
|
public function testCustomHeaders()
|
||||||
{
|
{
|
||||||
$response = Unirest::get('http://httpbin.org/get', array(
|
$response = Request::get('http://httpbin.org/get', array(
|
||||||
'user-agent' => 'ciao',
|
'user-agent' => 'ciao',
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEqual(200, $response->code);
|
$this->assertEquals(200, $response->code);
|
||||||
|
|
||||||
$headers = $response->body->headers;
|
$headers = $response->body->headers;
|
||||||
$this->assertEqual("ciao", $headers->{'User-Agent'});
|
$this->assertEquals('ciao', $headers->{'User-Agent'});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testHttpBuildQueryWhenCurlFile()
|
public function testHttpBuildQueryWhenCurlFile()
|
||||||
{
|
{
|
||||||
$file = Unirest::file(dirname(__FILE__) . "/test_upload.txt");
|
$file = Request::file(UPLOAD_FIXTURE);
|
||||||
$body = array(
|
$body = array(
|
||||||
"to" => "mail@mailinator.com",
|
'to' => 'mail@mailinator.com',
|
||||||
"from" => "mail@mailinator.com",
|
'from' => 'mail@mailinator.com',
|
||||||
"file" => $file
|
'file' => $file
|
||||||
);
|
);
|
||||||
Unirest::http_build_query_for_curl($body, $postBody);
|
Request::buildHTTPCurlQuery($body, $postBody);
|
||||||
$this->assertEqual($postBody['file'], $file);
|
$this->assertEquals($postBody['file'], $file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user