improving tests

This commit is contained in:
Ahmad Nassri
2015-01-14 18:40:21 -05:00
parent be9bca1d5a
commit 4f156e59fd
4 changed files with 55 additions and 45 deletions

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true"> <phpunit bootstrap="tests/bootstrap.php" colors="true">
<testsuites> <testsuites>
<testsuite name="Unirest Test Suite"> <testsuite name="Unirest Test Suite">
<directory>./tests</directory> <directory>./tests</directory>

View File

@@ -0,0 +1,12 @@
<?php
use Unirest\File as File;
class UnirestFileTest extends \PHPUnit_Framework_TestCase
{
public function testCURLFile()
{
$file = File::add(UPLOAD_FIXTURE);
$this->assertTrue($file instanceof \CURLFile);
}
}

View File

@@ -1,17 +1,10 @@
<?php <?php
require_once __DIR__ . '/../../src/Unirest.php'; class UnirestRequestTest extends \PHPUnit_Framework_TestCase
define('UPLOAD_FIXTURE', dirname(__DIR__) . '/fixtures/upload.txt');
use Unirest\File as File;
use Unirest\Request as Request;
class UnirestTest extends \PHPUnit_Framework_TestCase
{ {
public function testGet() public function testGet()
{ {
$response = Request::get('http://httpbin.org/get?name=Mark', array( $response = Unirest\Request::get('http://httpbin.org/get?name=Mark', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'nick' => 'thefosk' 'nick' => 'thefosk'
@@ -26,7 +19,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testGetMultidimensionalArray() public function testGetMultidimensionalArray()
{ {
$response = Request::get('http://httpbin.org/get', array( $response = Unirest\Request::get('http://httpbin.org/get', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'key' => 'value', 'key' => 'value',
@@ -47,7 +40,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testGetWithDots() public function testGetWithDots()
{ {
$response = Request::get('http://httpbin.org/get', array( $response = Unirest\Request::get('http://httpbin.org/get', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'user.name' => 'Mark', 'user.name' => 'Mark',
@@ -63,7 +56,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testGetWithDots2() public function testGetWithDots2()
{ {
$response = Request::get('http://httpbin.org/get', array( $response = Unirest\Request::get('http://httpbin.org/get', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'user.name' => 'Mark Bond', 'user.name' => 'Mark Bond',
@@ -79,7 +72,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testPost() public function testPost()
{ {
$response = Request::post('http://httpbin.org/post', array( $response = Unirest\Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'name' => 'Mark', 'name' => 'Mark',
@@ -95,7 +88,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testPostWithEqualSign() public function testPostWithEqualSign()
{ {
$response = Request::post('http://httpbin.org/post', array( $response = Unirest\Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'name' => 'Mark=Hello' 'name' => 'Mark=Hello'
@@ -109,7 +102,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testGetWithEqualSign() public function testGetWithEqualSign()
{ {
$response = Request::get('http://httpbin.org/get', array( $response = Unirest\Request::get('http://httpbin.org/get', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'name' => 'Mark=Hello' 'name' => 'Mark=Hello'
@@ -120,7 +113,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
$args = $response->body->args; $args = $response->body->args;
$this->assertEquals('Mark=Hello', $args->name); $this->assertEquals('Mark=Hello', $args->name);
$response = Request::get('http://httpbin.org/get', array( $response = Unirest\Request::get('http://httpbin.org/get', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'name' => 'Mark=Hello=John' 'name' => 'Mark=Hello=John'
@@ -134,7 +127,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testPostArray() public function testPostArray()
{ {
$response = Request::post('http://httpbin.org/post', array( $response = Unirest\Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'name[0]' => 'Mark', 'name[0]' => 'Mark',
@@ -151,7 +144,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testGetArray() public function testGetArray()
{ {
$response = Request::get('http://httpbin.org/get', array(), array( $response = Unirest\Request::get('http://httpbin.org/get', array(), array(
'name[0]' => 'Mark', 'name[0]' => 'Mark',
'name[1]' => 'John' 'name[1]' => 'John'
)); ));
@@ -165,7 +158,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testPostWithDots() public function testPostWithDots()
{ {
$response = Request::post('http://httpbin.org/post', array( $response = Unirest\Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'user.name' => 'Mark', 'user.name' => 'Mark',
@@ -181,7 +174,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testRawPost() public function testRawPost()
{ {
$response = Request::post('http://httpbin.org/post', array( $response = Unirest\Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json', 'Accept' => 'application/json',
'Content-Type' => 'application/json' 'Content-Type' => 'application/json'
), json_encode(array( ), json_encode(array(
@@ -197,24 +190,24 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testHttpBuildQueryWhenCurlFile() public function testHttpBuildQueryWhenCurlFile()
{ {
$file = File::add(UPLOAD_FIXTURE); $file = Unirest\File::add(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
); );
$result = Request::buildHTTPCurlQuery($body); $result = Unirest\Request::buildHTTPCurlQuery($body);
$this->assertEquals($result['file'], $file); $this->assertEquals($result['file'], $file);
} }
public function testUpload() public function testUpload()
{ {
$response = Request::post('http://httpbin.org/post', array( $response = Unirest\Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'name' => 'Mark', 'name' => 'Mark',
'file' => File::add(UPLOAD_FIXTURE) 'file' => Unirest\File::add(UPLOAD_FIXTURE)
)); ));
$this->assertEquals(200, $response->code); $this->assertEquals(200, $response->code);
@@ -227,11 +220,11 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testUploadIfFilePartOfData() public function testUploadIfFilePartOfData()
{ {
$response = Request::post('http://httpbin.org/post', array( $response = Unirest\Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'name' => 'Mark', 'name' => 'Mark',
'files[owl.gif]' => File::add(UPLOAD_FIXTURE) 'files[owl.gif]' => Unirest\File::add(UPLOAD_FIXTURE)
)); ));
$this->assertEquals(200, $response->code); $this->assertEquals(200, $response->code);
@@ -244,7 +237,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testPostMultidimensionalArray() public function testPostMultidimensionalArray()
{ {
$response = Request::post('http://httpbin.org/post', array( $response = Unirest\Request::post('http://httpbin.org/post', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'key' => 'value', 'key' => 'value',
@@ -264,7 +257,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testPut() public function testPut()
{ {
$response = Request::put('http://httpbin.org/put', array( $response = Unirest\Request::put('http://httpbin.org/put', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'name' => 'Mark', 'name' => 'Mark',
@@ -280,7 +273,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testPatch() public function testPatch()
{ {
$response = Request::patch('http://httpbin.org/patch', array( $response = Unirest\Request::patch('http://httpbin.org/patch', array(
'Accept' => 'application/json' 'Accept' => 'application/json'
), array( ), array(
'name' => 'Mark', 'name' => 'Mark',
@@ -296,7 +289,7 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testDelete() public function testDelete()
{ {
$response = Request::delete('http://httpbin.org/delete', array( $response = Unirest\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(
@@ -314,42 +307,42 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
*/ */
public function testTimeoutFail() public function testTimeoutFail()
{ {
Request::timeout(1); Unirest\Request::timeout(1);
Request::get('http://httpbin.org/delay/3'); Unirest\Request::get('http://httpbin.org/delay/3');
Request::timeout(null); // Cleaning timeout for the other tests Unirest\Request::timeout(null); // Cleaning timeout for the other tests
} }
public function testTimeoutSuccess() public function testTimeoutSuccess()
{ {
Request::timeout(3); Unirest\Request::timeout(3);
$response = Request::get('http://httpbin.org/delay/1'); $response = Unirest\Request::get('http://httpbin.org/delay/1');
$this->assertEquals(200, $response->code); $this->assertEquals(200, $response->code);
Request::timeout(null); // Cleaning timeout for the other tests Unirest\Request::timeout(null); // Cleaning timeout for the other tests
} }
public function testDefaultHeader() public function testDefaultHeader()
{ {
Request::defaultHeader('Hello', 'custom'); Unirest\Request::defaultHeader('Hello', 'custom');
$response = Request::get('http://httpbin.org/get'); $response = Unirest\Request::get('http://httpbin.org/get');
$this->assertEquals(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->assertEquals('custom', $headers->Hello); $this->assertEquals('custom', $headers->Hello);
$response = Request::get('http://httpbin.org/get'); $response = Unirest\Request::get('http://httpbin.org/get');
$this->assertEquals(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->assertEquals('custom', $headers->Hello); $this->assertEquals('custom', $headers->Hello);
Request::clearDefaultHeaders(); Unirest\Request::clearDefaultHeaders();
$response = Request::get('http://httpbin.org/get'); $response = Unirest\Request::get('http://httpbin.org/get');
$this->assertEquals(200, $response->code); $this->assertEquals(200, $response->code);
$headers = $response->body->headers; $headers = $response->body->headers;
@@ -359,21 +352,21 @@ class UnirestTest extends \PHPUnit_Framework_TestCase
public function testGzip() public function testGzip()
{ {
$response = Request::get('http://httpbin.org/gzip'); $response = Unirest\Request::get('http://httpbin.org/gzip');
$args = $response->body; $args = $response->body;
$this->assertEquals(true, $args->gzipped); $this->assertEquals(true, $args->gzipped);
} }
public function testBasicAuthentication() public function testBasicAuthentication()
{ {
$response = Request::get('http://httpbin.org/get', array(), array(), 'user', 'password'); $response = Unirest\Request::get('http://httpbin.org/get', array(), array(), 'user', 'password');
$headers = $response->body->headers; $headers = $response->body->headers;
$this->assertEquals('Basic dXNlcjpwYXNzd29yZA==', $headers->Authorization); $this->assertEquals('Basic dXNlcjpwYXNzd29yZA==', $headers->Authorization);
} }
public function testCustomHeaders() public function testCustomHeaders()
{ {
$response = Request::get('http://httpbin.org/get', array( $response = Unirest\Request::get('http://httpbin.org/get', array(
'user-agent' => 'ciao', 'user-agent' => 'ciao',
)); ));

5
tests/bootstrap.php Normal file
View File

@@ -0,0 +1,5 @@
<?php
require_once __DIR__ . '/../src/Unirest.php';
define('UPLOAD_FIXTURE', __DIR__ . '/fixtures/upload.txt');