massive cleanups

- renamed classes
- updated to phpunit
- more PSR compliance
This commit is contained in:
Ahmad Nassri
2014-12-17 22:29:03 -05:00
parent ea3995225c
commit 61eb281692
12 changed files with 326 additions and 365 deletions

View File

@@ -13,7 +13,3 @@ tools:
php_cs_fixer:
config:
level: 'psr2'
filter:
excluded_paths:
- 'test/*'

View File

@@ -4,8 +4,5 @@ php:
- 5.3
- 5.4
- 5.5
before_script:
- 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
- 5.6
- hhvm

View File

@@ -15,10 +15,11 @@
"require" : {
"php" : ">=5.3.0",
"ext-curl" : "*",
"ext-json" : "*"
"ext-json" : "*",
"ext-http" : "*"
},
"require-dev": {
"vierbergenlars/simpletest": "*"
"phpunit/phpunit": "*"
},
"autoload" : {
"psr-0" : {
@@ -28,4 +29,4 @@
"support" : {
"email" : "support@mashape.com"
}
}
}

View File

@@ -1,5 +1,5 @@
<?php
require_once dirname(__FILE__) . '/Unirest/HttpMethod.php';
require_once dirname(__FILE__) . '/Unirest/HttpResponse.php';
require_once dirname(__FILE__) . '/Unirest/Unirest.php';
require_once dirname(__FILE__) . '/Unirest/Method.php';
require_once dirname(__FILE__) . '/Unirest/Response.php';
require_once dirname(__FILE__) . '/Unirest/Request.php';

View File

@@ -2,7 +2,7 @@
namespace Unirest;
interface HttpMethod
interface Method
{
const DELETE = 'DELETE';
const GET = 'GET';

View File

@@ -1,9 +1,11 @@
<?php
use Unirest\HttpMethod;
use Unirest\HttpResponse;
use Unirest\method;
use Unirest\Response;
class Unirest
namespace Unirest;
class Request
{
private static $verifyPeer = true;
private static $socketTimeout = null;
@@ -56,7 +58,7 @@ class Unirest
*/
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)
{
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)
{
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)
{
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)
{
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)
{
if (function_exists("curl_file_create")) {
if (function_exists('curl_file_create')) {
return curl_file_create($path);
} else {
return "@" . $path;
return '@' . $path;
}
}
/**
* 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)
{
@@ -150,41 +152,41 @@ class Unirest
/**
* 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 mixed $body request body
* @param array $headers additional headers to send
* @param string $username Basic Authentication username
* @param string $password Basic Authentication password
* @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) {
$headers = array();
}
$lowercaseHeaders = array();
$finalHeaders = array_merge($headers, Unirest::$defaultHeaders);
$finalHeaders = array_merge($headers, self::$defaultHeaders);
foreach ($finalHeaders as $key => $val) {
$lowercaseHeaders[] = self::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('user-agent', $lowerCaseFinalHeaders)) {
$lowercaseHeaders[] = 'user-agent: unirest-php/2.0';
}
if (!array_key_exists("expect", $lowerCaseFinalHeaders)) {
$lowercaseHeaders[] = "expect:";
if (!array_key_exists('expect', $lowerCaseFinalHeaders)) {
$lowercaseHeaders[] = 'expect:';
}
$ch = curl_init();
if ($httpMethod != HttpMethod::GET) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
if ($method != Method::GET) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
if (is_array($body) || $body instanceof Traversable) {
self::buildHTTPCurlQuery($body, $postBody);
@@ -194,9 +196,9 @@ class Unirest
}
} elseif (is_array($body)) {
if (strpos($url, '?') !== false) {
$url .= "&";
$url .= '&';
} else {
$url .= "?";
$url .= '?';
}
self::buildHTTPCurlQuery($body, $postBody);
@@ -210,39 +212,39 @@ class Unirest
curl_setopt($ch, CURLOPT_HTTPHEADER, $lowercaseHeaders);
curl_setopt($ch, CURLOPT_HEADER, true);
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) {
curl_setopt($ch, CURLOPT_TIMEOUT, self::$socketTimeout);
}
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);
$error = curl_error($ch);
if ($error) {
throw new Exception($error);
throw new \Exception($error);
}
// Split the full response in its headers and body
$curl_info = curl_getinfo($ch);
$header_size = $curl_info["header_size"];
$header_size = $curl_info['header_size'];
$header = substr($response, 0, $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)
{
$pairs = explode("&", $querystring);
$pairs = explode('&', $querystring);
$vars = array();
foreach ($pairs as $pair) {
$nv = explode("=", $pair, 2);
$nv = explode('=', $pair, 2);
$name = $nv[0];
$value = $nv[1];
$vars[$name] = $value;
@@ -269,8 +271,8 @@ class Unirest
$query = '?' . http_build_query(self::getArrayFromQuerystring($url_parsed['query']));
}
if ($port && $port[0] != ":") {
$port = ":" . $port;
if ($port && $port[0] != ':') {
$port = ':' . $port;
}
$result = $scheme . $host . $port . $path . $query;
@@ -280,35 +282,6 @@ class Unirest
private static function getHeader($key, $val)
{
$key = trim(strtolower($key));
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;
return $key . ': ' . $val;
}
}

View File

@@ -2,7 +2,7 @@
namespace Unirest;
class HttpResponse
class Response
{
private $code;
@@ -15,13 +15,13 @@ class HttpResponse
* @param string $raw_body the raw body of the 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->headers = $this->getHeadersFromCurlResponse($headers);
$this->raw_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) {
$this->body = $json;

12
phpunit.xml Normal file
View 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
View File

@@ -1 +0,0 @@
simpletest

View File

@@ -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');
?>

View File

@@ -1,27 +1,33 @@
<?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()
{
$response = Unirest::get("http://httpbin.org/get?name=Mark", array(
"Accept" => "application/json"
$response = Request::get('http://httpbin.org/get?name=Mark', array(
'Accept' => 'application/json'
), array(
"nick" => "thefosk"
'nick' => 'thefosk'
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$args = $response->body->args;
$this->assertEqual("Mark", $args->name);
$this->assertEqual("thefosk", $args->nick);
$this->assertEquals('Mark', $args->name);
$this->assertEquals('thefosk', $args->nick);
}
public function testGetMultidimensionalArray()
{
$response = Unirest::get("http://httpbin.org/get", array(
"Accept" => "application/json"
$response = Request::get('http://httpbin.org/get', array(
'Accept' => 'application/json'
), array(
'key' => 'value',
'items' => array(
@@ -29,202 +35,204 @@ class UnirestTest extends UnitTestCase
'item2'
)
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$args = $response->body->args;
$this->assertEqual("value", $args->key);
$this->assertEqual("item1", $args->{"items[0]"});
$this->assertEqual("item2", $args->{"items[1]"});
$this->assertEquals('value', $args->key);
$this->assertEquals('item1', $args->{'items[0]'});
$this->assertEquals('item2', $args->{'items[1]'});
}
public function testGetWithDots()
{
$response = Unirest::get("http://httpbin.org/get", array(
"Accept" => "application/json"
$response = Request::get('http://httpbin.org/get', array(
'Accept' => 'application/json'
), array(
"user.name" => "Mark",
"nick" => "thefosk"
'user.name' => 'Mark',
'nick' => 'thefosk'
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$args = $response->body->args;
$this->assertEqual("Mark", $args->{"user.name"});
$this->assertEqual("thefosk", $args->nick);
$this->assertEquals('Mark', $args->{'user.name'});
$this->assertEquals('thefosk', $args->nick);
}
public function testGetWithDots2()
{
$response = Unirest::get("http://httpbin.org/get", array(
"Accept" => "application/json"
$response = Request::get('http://httpbin.org/get', array(
'Accept' => 'application/json'
), array(
"user.name" => "Mark Bond",
"nick" => "thefosk"
'user.name' => 'Mark Bond',
'nick' => 'thefosk'
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$args = $response->body->args;
$this->assertEqual("Mark Bond", $args->{"user.name"});
$this->assertEqual("thefosk", $args->nick);
$this->assertEquals('Mark Bond', $args->{'user.name'});
$this->assertEquals('thefosk', $args->nick);
}
public function testPost()
{
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
$response = Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json'
), array(
"name" => "Mark",
"nick" => "thefosk"
'name' => 'Mark',
'nick' => 'thefosk'
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$form = $response->body->form;
$this->assertEqual("Mark", $form->name);
$this->assertEqual("thefosk", $form->nick);
$this->assertEquals('Mark', $form->name);
$this->assertEquals('thefosk', $form->nick);
}
public function testPostWithEqualSign()
{
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
$response = Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json'
), array(
"name" => "Mark=Hello"
'name' => 'Mark=Hello'
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$form = $response->body->form;
$this->assertEqual("Mark=Hello", $form->name);
$this->assertEquals('Mark=Hello', $form->name);
}
public function testGetWithEqualSign()
{
$response = Unirest::get("http://httpbin.org/get", array(
"Accept" => "application/json"
$response = Request::get('http://httpbin.org/get', array(
'Accept' => 'application/json'
), array(
"name" => "Mark=Hello"
'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);
$this->assertEquals(200, $response->code);
$args = $response->body->args;
$this->assertEqual("Mark=Hello=John", $args->name);
$this->assertEquals('Mark=Hello', $args->name);
$response = Request::get('http://httpbin.org/get', array(
'Accept' => 'application/json'
), array(
'name' => 'Mark=Hello=John'
));
$this->assertEquals(200, $response->code);
$args = $response->body->args;
$this->assertEquals('Mark=Hello=John', $args->name);
}
public function testPostArray()
{
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
$response = Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json'
), array(
"name[0]" => "Mark",
"name[1]" => "John"
'name[0]' => 'Mark',
'name[1]' => 'John'
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$form = $response->body->form;
$this->assertEqual("Mark", $form->{"name[0]"});
$this->assertEqual("John", $form->{"name[1]"});
$this->assertEquals('Mark', $form->{'name[0]'});
$this->assertEquals('John', $form->{'name[1]'});
}
public function testGetArray()
{
$response = Unirest::get("http://httpbin.org/get", array(), array(
"name[0]" => "Mark",
"name[1]" => "John"
$response = Request::get('http://httpbin.org/get', array(), array(
'name[0]' => 'Mark',
'name[1]' => 'John'
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$args = $response->body->args;
$this->assertEqual("Mark", $args->{"name[0]"});
$this->assertEqual("John", $args->{"name[1]"});
$this->assertEquals('Mark', $args->{'name[0]'});
$this->assertEquals('John', $args->{'name[1]'});
}
public function testPostWithDots()
{
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
$response = Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json'
), array(
"user.name" => "Mark",
"nick" => "thefosk"
'user.name' => 'Mark',
'nick' => 'thefosk'
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$form = $response->body->form;
$this->assertEqual("Mark", $form->{"user.name"});
$this->assertEqual("thefosk", $form->nick);
$this->assertEquals('Mark', $form->{'user.name'});
$this->assertEquals('thefosk', $form->nick);
}
public function testRawPost()
{
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
$response = Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json',
'Content-Type' => 'application/json'
), json_encode(array(
"author" => "Sam Sullivan"
'author' => 'Sam Sullivan'
)));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$json = $response->body->json;
$this->assertEqual("Sam Sullivan", $json->author);
$this->assertEquals('Sam Sullivan', $json->author);
}
public function testUpload()
{
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
{
$response = Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json'
), array(
"name" => "Mark",
"file" => Unirest::file(dirname(__FILE__) . "/test_upload.txt")
'name' => 'Mark',
'file' => Request::file(UPLOAD_FIXTURE)
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$files = $response->body->files;
$this->assertEqual("This is a test", $files->file);
$this->assertEquals('This is a test', $files->file);
$form = $response->body->form;
$this->assertEqual("Mark", $form->name);
$this->assertEquals('Mark', $form->name);
}
public function testUploadIfFilePartOfData()
{
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
{
$response = Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json'
), array(
"name" => "Mark",
"files[owl.gif]" => Unirest::file(dirname(__FILE__) . "/test_upload.txt")
'name' => 'Mark',
'files[owl.gif]' => Request::file(UPLOAD_FIXTURE)
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
//$files = $response->body->files;
//$this->assertEqual("This is a test", $files->file);
//$this->assertEquals('This is a test', $files->file);
$form = $response->body->form;
$this->assertEqual("Mark", $form->name);
$this->assertEquals('Mark', $form->name);
}
public function testPostMultidimensionalArray()
{
$response = Unirest::post("http://httpbin.org/post", array(
"Accept" => "application/json"
$response = Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json'
), array(
'key' => 'value',
'items' => array(
@@ -232,143 +240,145 @@ class UnirestTest extends UnitTestCase
'item2'
)
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$form = $response->body->form;
$this->assertEqual("value", $form->key);
$this->assertEqual("item1", $form->{"items[0]"});
$this->assertEqual("item2", $form->{"items[1]"});
}
public function testPut()
{
$response = Unirest::put("http://httpbin.org/put", array(
"Accept" => "application/json"
), array(
"name" => "Mark",
"nick" => "thefosk"
));
$this->assertEqual(200, $response->code);
$form = $response->body->form;
$this->assertEqual("Mark", $form->name);
$this->assertEqual("thefosk", $form->nick);
}
public function testPatch()
{
$response = Unirest::patch("http://httpbin.org/patch", array(
"Accept" => "application/json"
), array(
"name" => "Mark",
"nick" => "thefosk"
));
$this->assertEqual(200, $response->code);
$form = $response->body->form;
$this->assertEqual("Mark", $form->name);
$this->assertEqual("thefosk", $form->nick);
}
public function testDelete()
{
$response = Unirest::delete("http://httpbin.org/delete", array(
"Accept" => "application/json",
"Content-Type" => "application/x-www-form-urlencoded"
), array(
"name" => "Mark",
"nick" => "thefosk"
));
$this->assertEqual(200, $response->code);
$data = $response->body->data;
$this->assertFalse(empty($data));
}
public function testTimeoutFail()
{
Unirest::timeout(1);
$this->expectException();
$response = Unirest::get("http://httpbin.org/delay/3");
Unirest::timeout(null); // Cleaning timeout for the other tests
}
public function testTimeoutSuccess()
{
Unirest::timeout(3);
$response = Unirest::get("http://httpbin.org/delay/1");
$this->assertEqual(200, $response->code);
Unirest::timeout(null); // Cleaning timeout for the other tests
}
public function testDefaultHeader()
{
Unirest::defaultHeader("Hello", "custom");
$response = Unirest::get("http://httpbin.org/get");
$this->assertEqual(200, $response->code);
$headers = $response->body->headers;
$properties = get_object_vars($headers);
$this->assertTrue(array_key_exists("Hello", $properties));
$this->assertEqual("custom", $headers->Hello);
$response = Unirest::get("http://httpbin.org/get");
$this->assertEqual(200, $response->code);
$headers = $response->body->headers;
$properties = get_object_vars($headers);
$this->assertTrue(array_key_exists("Hello", $properties));
$this->assertEqual("custom", $headers->Hello);
Unirest::clearDefaultHeaders();
$response = Unirest::get("http://httpbin.org/get");
$this->assertEqual(200, $response->code);
$headers = $response->body->headers;
$properties = get_object_vars($headers);
$this->assertFalse(array_key_exists("Hello", $properties));
}
public function testGzip()
{
$response = Unirest::get("http://httpbin.org/gzip");
$args = $response->body;
$this->assertEqual(true, $args->gzipped);
}
public function testBasicAuthentication()
{
$response = Unirest::get("http://httpbin.org/get", null, null, "user", "password");
$headers = $response->body->headers;
$this->assertEqual("Basic dXNlcjpwYXNzd29yZA==", $headers->Authorization);
$this->assertEquals('value', $form->key);
$this->assertEquals('item1', $form->{'items[0]'});
$this->assertEquals('item2', $form->{'items[1]'});
}
public function testCustomHeaders()
public function testPut()
{
$response = Unirest::get('http://httpbin.org/get', array(
$response = Request::put('http://httpbin.org/put', array(
'Accept' => 'application/json'
), array(
'name' => 'Mark',
'nick' => 'thefosk'
));
$this->assertEquals(200, $response->code);
$form = $response->body->form;
$this->assertEquals('Mark', $form->name);
$this->assertEquals('thefosk', $form->nick);
}
public function testPatch()
{
$response = Request::patch('http://httpbin.org/patch', array(
'Accept' => 'application/json'
), array(
'name' => 'Mark',
'nick' => 'thefosk'
));
$this->assertEquals(200, $response->code);
$form = $response->body->form;
$this->assertEquals('Mark', $form->name);
$this->assertEquals('thefosk', $form->nick);
}
public function testDelete()
{
$response = Request::delete('http://httpbin.org/delete', array(
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded'
), array(
'name' => 'Mark',
'nick' => 'thefosk'
));
$this->assertEquals(200, $response->code);
$data = $response->body->data;
$this->assertTrue(empty($data));
}
/**
* @expectedException Exception
*/
public function testTimeoutFail()
{
Request::timeout(1);
$response = Request::get('http://httpbin.org/delay/3');
Request::timeout(null); // Cleaning timeout for the other tests
}
public function testTimeoutSuccess()
{
Request::timeout(3);
$response = Request::get('http://httpbin.org/delay/1');
$this->assertEquals(200, $response->code);
Request::timeout(null); // Cleaning timeout for the other tests
}
public function testDefaultHeader()
{
Request::defaultHeader('Hello', 'custom');
$response = Request::get('http://httpbin.org/get');
$this->assertEquals(200, $response->code);
$headers = $response->body->headers;
$properties = get_object_vars($headers);
$this->assertTrue(array_key_exists('Hello', $properties));
$this->assertEquals('custom', $headers->Hello);
$response = Request::get('http://httpbin.org/get');
$this->assertEquals(200, $response->code);
$headers = $response->body->headers;
$properties = get_object_vars($headers);
$this->assertTrue(array_key_exists('Hello', $properties));
$this->assertEquals('custom', $headers->Hello);
Request::clearDefaultHeaders();
$response = Request::get('http://httpbin.org/get');
$this->assertEquals(200, $response->code);
$headers = $response->body->headers;
$properties = get_object_vars($headers);
$this->assertFalse(array_key_exists('Hello', $properties));
}
public function testGzip()
{
$response = Request::get('http://httpbin.org/gzip');
$args = $response->body;
$this->assertEquals(true, $args->gzipped);
}
public function testBasicAuthentication()
{
$response = Request::get('http://httpbin.org/get', null, null, 'user', 'password');
$headers = $response->body->headers;
$this->assertEquals('Basic dXNlcjpwYXNzd29yZA==', $headers->Authorization);
}
public function testCustomHeaders()
{
$response = Request::get('http://httpbin.org/get', array(
'user-agent' => 'ciao',
));
$this->assertEqual(200, $response->code);
$this->assertEquals(200, $response->code);
$headers = $response->body->headers;
$this->assertEqual("ciao", $headers->{'User-Agent'});
$this->assertEquals('ciao', $headers->{'User-Agent'});
}
public function testHttpBuildQueryWhenCurlFile()
{
$file = Unirest::file(dirname(__FILE__) . "/test_upload.txt");
$file = Request::file(UPLOAD_FIXTURE);
$body = array(
"to" => "mail@mailinator.com",
"from" => "mail@mailinator.com",
"file" => $file
'to' => 'mail@mailinator.com',
'from' => 'mail@mailinator.com',
'file' => $file
);
Unirest::http_build_query_for_curl($body, $postBody);
$this->assertEqual($postBody['file'], $file);
Request::buildHTTPCurlQuery($body, $postBody);
$this->assertEquals($postBody['file'], $file);
}
}