gzip support; default request headers; customizable timeout; basic authentication; bugfixes; closes #5 #15 #16 #18

This commit is contained in:
thefosk
2013-11-01 19:26:22 -07:00
parent 1725812c01
commit 10fc78dff8
5 changed files with 261 additions and 23 deletions

1
test/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
simpletest

30
test/Unirest.php Normal file
View File

@@ -0,0 +1,30 @@
<?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

@@ -0,0 +1,135 @@
<?php
class UnirestTest extends UnitTestCase
{
public function testGet()
{
$response = Unirest::get("http://httpbin.org/get?name=Mark", array( "Accept" => "application/json" ),
array(
"nick" => "thefosk"
));
$this->assertEqual(200, $response->code);
$args = $response->body->args;
$this->assertEqual("Mark", $args->name);
$this->assertEqual("thefosk", $args->nick);
}
public function testPost()
{
$response = Unirest::post("http://httpbin.org/post", 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 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");
}
public function testTimeoutSuccess()
{
Unirest::timeout(3);
$response = Unirest::get("http://httpbin.org/delay/1");
$this->assertEqual(200, $response->code);
}
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);
}
}