From 61eb281692d8dd234edefd448498e411372abbb0 Mon Sep 17 00:00:00 2001 From: Ahmad Nassri Date: Wed, 17 Dec 2014 22:29:03 -0500 Subject: [PATCH] massive cleanups - renamed classes - updated to phpunit - more PSR compliance --- .scrutinizer.yml | 4 - .travis.yml | 7 +- composer.json | 7 +- lib/Unirest.php | 6 +- lib/Unirest/{HttpMethod.php => Method.php} | 2 +- lib/Unirest/{Unirest.php => Request.php} | 99 ++-- .../{HttpResponse.php => Response.php} | 6 +- phpunit.xml | 12 + test/.gitignore | 1 - test/Unirest.php | 27 - test/Unirest/UnirestTest.php | 520 +++++++++--------- .../test_upload.txt => fixtures/upload.txt} | 0 12 files changed, 326 insertions(+), 365 deletions(-) rename lib/Unirest/{HttpMethod.php => Method.php} (88%) rename lib/Unirest/{Unirest.php => Request.php} (72%) rename lib/Unirest/{HttpResponse.php => Response.php} (91%) create mode 100644 phpunit.xml delete mode 100644 test/.gitignore delete mode 100644 test/Unirest.php rename test/{Unirest/test_upload.txt => fixtures/upload.txt} (100%) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index f3b87fb..3e6cc9d 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -13,7 +13,3 @@ tools: php_cs_fixer: config: level: 'psr2' - -filter: - excluded_paths: - - 'test/*' diff --git a/.travis.yml b/.travis.yml index 98d9b2b..5bd57ec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/composer.json b/composer.json index 9cc280c..7657c65 100644 --- a/composer.json +++ b/composer.json @@ -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" } -} \ No newline at end of file +} diff --git a/lib/Unirest.php b/lib/Unirest.php index 49a8adf..a2d0a5a 100644 --- a/lib/Unirest.php +++ b/lib/Unirest.php @@ -1,5 +1,5 @@ $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; } } diff --git a/lib/Unirest/HttpResponse.php b/lib/Unirest/Response.php similarity index 91% rename from lib/Unirest/HttpResponse.php rename to lib/Unirest/Response.php index f252bbc..be6e2af 100644 --- a/lib/Unirest/HttpResponse.php +++ b/lib/Unirest/Response.php @@ -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; diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..37b77f9 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,12 @@ + + + + + ./test + + + + + + + diff --git a/test/.gitignore b/test/.gitignore deleted file mode 100644 index d875a72..0000000 --- a/test/.gitignore +++ /dev/null @@ -1 +0,0 @@ -simpletest diff --git a/test/Unirest.php b/test/Unirest.php deleted file mode 100644 index 031c389..0000000 --- a/test/Unirest.php +++ /dev/null @@ -1,27 +0,0 @@ -, 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'); - -?> \ No newline at end of file diff --git a/test/Unirest/UnirestTest.php b/test/Unirest/UnirestTest.php index 188fa85..688ece4 100644 --- a/test/Unirest/UnirestTest.php +++ b/test/Unirest/UnirestTest.php @@ -1,27 +1,33 @@ "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); } } diff --git a/test/Unirest/test_upload.txt b/test/fixtures/upload.txt similarity index 100% rename from test/Unirest/test_upload.txt rename to test/fixtures/upload.txt