--- /dev/null
+<?php
+
+use http\Client\Request;
+use http\Message\Body;
+
+class ClientRequestTest extends PHPUnit_Framework_TestCase
+{
+ function testStandard() {
+ $r = new Request($m = "POST", $u = "http://localhost/foo", $h = array("header", "value"),
+ $b = new Body(fopen(__FILE__, "r+b"))
+ );
+
+ $this->assertEquals($b, $r->getBody());
+ $this->assertEquals($h, $r->getHeaders());
+ $this->assertEquals($u, $r->getRequestUrl());
+ $this->assertEquals($m, $r->getRequestMethod());
+ }
+
+ function testContentType() {
+ $r = new Request("POST", "http://localhost/");
+ $this->assertEquals($r, $r->setContentType($ct = "text/plain; charset=utf-8"));
+ $this->assertEquals($ct, $r->getContentType());
+ }
+
+ function testQuery() {
+ $r = new Request("GET", "http://localhost/");
+ $this->assertEquals(null, $r->getQuery());
+ $this->assertEquals($r, $r->setQuery($q = "foo=bar"));
+ $this->assertEquals($q, $r->getQuery());
+ $this->assertEquals($r, $r->addQuery("a[]=1&a[]=2"));
+ $this->assertEquals("foo=bar&a%5B0%5D=1&a%5B1%5D=2", $r->getQuery());
+ $this->assertEquals(null, $r->setQuery(null)->getQuery());
+ }
+
+ function testOptions() {
+ $r = new Request("GET", "http://localhost");
+ $this->assertEquals($r, $r->setOptions($o = array("redirect"=>5, "timeout"=>5)));
+ $this->assertEquals($o, $r->getOptions());
+ $this->assertEquals($r, $r->setSslOptions($o = array("verify_peer"=>false)));
+ $this->assertEquals($o, $r->getSslOptions());
+ }
+}
+
--- /dev/null
+<?php
+
+class DataShareTest extends PHPUnit_Framework_TestCase
+{
+ function testStandard() {
+ foreach (http\Client\Factory::getAvailableDrivers() as $driver) {
+ $f = new http\Client\Factory(compact("driver"));
+ $s = $f->createDataShare();
+
+ $this->assertFalse($s->dns, "dns");
+ $this->assertFalse($s->cookie, "cookie");
+ $this->assertFalse($s->ssl, "ssl");
+
+ $s->dns = true;
+ $s->cookie = true;
+ $s->ssl = true;
+
+ $this->assertTrue($s->dns, "dns");
+ $this->assertTrue($s->cookie, "cookie");
+ $this->assertTrue($s->ssl, "ssl");
+ }
+ }
+
+ function testAttach() {
+ foreach (http\Client\Factory::getAvailableDrivers() as $driver) {
+ $f = new http\Client\Factory(compact("driver"));
+ $s = $f->createDataShare();
+ $s->dns = $s->ssl = $s->cookie = true;
+ $c = $f->createClient();
+ $s->attach($c);
+ $c->setRequest(new http\Client\Request("GET", "https://twitter.com/"));
+ $s->attach($c);
+ $cc = clone $c;
+ $s->attach($cc);
+
+ $this->assertEquals(3, count($s));
+ }
+ }
+
+ function testCurl() {
+ $client = new http\Curl\Client;
+ $client->setRequest(new http\Client\Request("GET", "https://twitter.com/"));
+ $share = new http\Curl\Client\DataShare;
+ $share->ssl = $share->dns = $share->cookie = true;
+ $share->attach($client);
+ $share->attach($client2 = clone $client);
+ $share->attach($client3 = clone $client);
+
+ $this->assertEquals(3, count($share));
+ $client->send();
+ $client2->send();
+ $client3->send();
+
+ $share->detach($client);
+ $share->reset();
+ }
+}
+
--- /dev/null
+<?php
+
+class PoolTest extends PHPUnit_Framework_TestCase
+{
+ /*function testStandard() {
+ foreach (http\Client\Factory::getAvailableDrivers() as $driver) {
+ $f = new http\Client\Factory(compact("driver"));
+ $p = $f->createPool();
+
+ $this->assertFalse($p->dns, "dns");
+ $this->assertFalse($p->cookie, "cookie");
+ $this->assertFalse($p->ssl, "ssl");
+
+ $p->dns = true;
+ $p->cookie = true;
+ $p->ssl = true;
+
+ $this->assertTrue($p->dns, "dns");
+ $this->assertTrue($p->cookie, "cookie");
+ $this->assertTrue($p->ssl, "ssl");
+ }
+ }*/
+
+ function testAttach() {
+ foreach (http\Client\Factory::getAvailableDrivers() as $driver) {
+ $f = new http\Client\Factory(compact("driver"));
+
+ $p = $f->createPool();
+ $c = $f->createClient();
+
+ $c->setRequest(new http\Client\Request("GET", "https://twitter.com/"));
+ $p->attach($c);
+
+ try {
+ $p->attach($c);
+ } catch (http\Exception $e) {
+ $this->assertEquals("Could not attach request to pool: Invalid easy handle", $e->getMessage());
+ }
+
+ $cc = clone $c;
+ $p->attach($cc);
+
+ $this->assertEquals(2, count($p));
+ }
+ }
+
+ function testCurl() {
+ $client = new http\Curl\Client;
+ $client->setRequest(new http\Client\Request("GET", "https://twitter.com/"));
+ $pool = new http\Curl\Client\Pool;
+ $pool->attach($client);
+ $pool->attach($client2 = clone $client);
+ $pool->attach($client3 = clone $client);
+
+ $this->assertEquals(3, count($pool));
+ $pool->send();
+
+ $pool->detach($client);
+ $this->assertEquals(2, count($pool));
+
+ $pool->reset();
+ $this->assertEquals(0, count($pool));
+ }
+
+ function testCurlEvents() {
+ $client = new http\Curl\Client;
+ $pool = new http\Curl\Client\Pool;
+
+ $client->setRequest(new http\Client\Request("GET", "https://twitter.com/"));
+
+ $pool->attach($client);
+ $pool->attach(clone $client);
+ $pool->attach(clone $client);
+
+ $pool->enableEvents();
+ $pool->send();
+ }
+}
+
"timeout" => 300,
)
);
+ $this->r->setOptions(
+ array(
+ "timeout" => 600
+ )
+ );
+ $this->assertEquals(
+ array(
+ "connecttimeout" => 30,
+ "timeout" => 600,
+ ),
+ $this->r->getOptions()
+ );
}
function testClone() {
}
function testObserver() {
- $this->r->attach(new ProgressObserver1);
- $this->r->attach(new ProgressObserver2);
+ $this->r->attach($o1 = new ProgressObserver1);
+ $this->r->attach($o2 = new ProgressObserver2);
$this->r->attach(
- new CallbackObserver(
+ $o3 = new CallbackObserver(
function ($r) {
$p = (array) $r->getProgress();
$this->assertArrayHasKey("started", $p);
$this->r->setRequest(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/"))->send(null);
$this->assertRegexp("/(\.-)+/", $this->r->pi);
$this->assertCount(3, $this->r->getObservers());
+ $this->r->detach($o1);
+ $this->assertCount(2, $this->r->getObservers());
+ $this->r->detach($o2);
+ $this->assertCount(1, $this->r->getObservers());
+ $this->r->detach($o3);
+ $this->assertCount(0, $this->r->getObservers());
}
function testCookies() {
$this->r->send(null);
$this->assertNotEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
}
+
+ function testSsl() {
+ $this->r->setRequest(new http\Client\Request("GET", "https://twitter.com/"));
+ $this->r->setSslOptions(array("verify_peer" => true));
+ $this->r->addSslOptions(array("verify_host" => 2));
+ $this->assertEquals(
+ array(
+ "verify_peer" => true,
+ "verify_host" => 2,
+ ),
+ $this->r->getSslOptions()
+ );
+ $this->r->send();
+ $ti = $this->r->getTransferInfo();
+ $this->assertArrayHasKey("ssl_engines", $ti);
+ $this->assertGreaterThan(0, count($ti["ssl_engines"]));
+ }
function testHistory() {
$body = new http\Message\Body;
--- /dev/null
+--TEST--
+http\Env\Request grabbing $_FILES
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--POST_RAW--
+Content-Type: multipart/form-data;boundary=--123
+
+----123
+Content-Disposition: form-data;filename="foo.bar"
+
+foo
+bar
+
+----123
+Content-Disposition: form-data;filename="bar.foo"
+
+bar
+foo
+
+----123--
+--FILE--
+<?php
+echo "TEST\n";
+
+$r = new http\Env\Request;
+$f = $r->getFiles();
+
+foreach ($_FILES as $i => $file) {
+ foreach (array("name","type","size","error","file") as $key) {
+ if ($file[$key == "file" ? "tmp_name" : $key] != $f[$i][$key]) {
+ printf("%d.%s differs: '%s' != '%s'\n", $i, $key, $f[$i][$key], $file[$key]);
+ }
+ }
+}
+
+?>
+DONE
+--EXPECT--
+TEST
+DONE
--- /dev/null
+--TEST--
+http\Env\Request grabbing $_FILES from array
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--POST_RAW--
+Content-Type: multipart/form-data;boundary=--123
+
+----123
+Content-Disposition: form-data;filename=file1;name=file[]
+
+first
+----123
+Content-Disposition: form-data;filename=file2;name=file[]
+
+second
+----123
+Content-Disposition: form-data;filename=file3;name=file[]
+
+third
+----123--
+--FILE--
+<?php
+echo "TEST\n";
+
+$r = new http\Env\Request;
+$f = array();
+
+foreach ($_FILES as $name => $data) {
+ foreach ($data["tmp_name"] as $i => $file) {
+ $f[$name][$i] = array(
+ "file" => $file,
+ "name" => $data["name"][$i],
+ "size" => $data["size"][$i],
+ "type" => $data["type"][$i],
+ "error"=> $data["error"][$i]
+ );
+ }
+}
+
+var_dump($f == $r->getFiles());
+
+?>
+DONE
+--EXPECT--
+TEST
+bool(true)
+DONE
--- /dev/null
+--TEST--
+http\Env\Request getForm
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--POST--
+a=b&b=c&r[]=1&r[]=2
+--FILE--
+<?php
+echo "TEST\n";
+
+$r = new http\Env\Request;
+printf("%s\n", $r->getForm());
+printf("%s\n", $r->getForm("b", "s", null, true));
+printf("%s\n", $r->getForm("x", "s", "nooo"));
+printf("%s\n", $r->getForm());
+?>
+DONE
+--EXPECT--
+TEST
+a=b&b=c&r%5B0%5D=1&r%5B1%5D=2
+c
+nooo
+a=b&r%5B0%5D=1&r%5B1%5D=2
+DONE
--- /dev/null
+--TEST--
+http\Env\Request getQuery
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--GET--
+a=b&b=c&r[]=1&r[]=2
+--FILE--
+<?php
+echo "TEST\n";
+
+$r = new http\Env\Request;
+printf("%s\n", $r->getQuery());
+printf("%s\n", $r->getQuery("b", "s", null, true));
+printf("%s\n", $r->getQuery("x", "s", "nooo"));
+printf("%s\n", $r->getQuery());
+?>
+DONE
+--EXPECT--
+TEST
+a=b&b=c&r%5B0%5D=1&r%5B1%5D=2
+c
+nooo
+a=b&r%5B0%5D=1&r%5B1%5D=2
+DONE
--- /dev/null
+--TEST--
+query parser
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+$p = new http\Params("foo=bar&arr[]=1&arr[]=2", array("&",";"), "", "=", http\Params::PARSE_QUERY);
+
+var_dump($p);
+
+echo $p, "\n";
+?>
+DONE
+--EXPECTF--
+object(http\Params)#%d (6) {
+ ["errorHandling":protected]=>
+ NULL
+ ["params"]=>
+ array(2) {
+ ["foo"]=>
+ array(2) {
+ ["value"]=>
+ string(3) "bar"
+ ["arguments"]=>
+ array(0) {
+ }
+ }
+ ["arr"]=>
+ array(2) {
+ ["value"]=>
+ array(2) {
+ [0]=>
+ string(1) "1"
+ [1]=>
+ string(1) "2"
+ }
+ ["arguments"]=>
+ array(0) {
+ }
+ }
+ }
+ ["param_sep"]=>
+ array(2) {
+ [0]=>
+ string(1) "&"
+ [1]=>
+ string(1) ";"
+ }
+ ["arg_sep"]=>
+ string(0) ""
+ ["val_sep"]=>
+ string(1) "="
+ ["flags"]=>
+ int(12)
+}
+foo=bar&arr%5B0%5D=1&arr%5B1%5D=2
+DONE
+
--- /dev/null
+--TEST--
+serialization
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--FILE--
+<?php
+
+class Client extends \http\Client\AbstractClient
+{
+ function __construct() {
+ parent::__construct();
+ $this->request = new \http\Client\Request("GET", "http://localhost");
+ }
+ function send($request) {
+ }
+}
+
+$ext = new ReflectionExtension("http");
+foreach ($ext->getClasses() as $class) {
+ if ($class->isInstantiable()) {
+ #printf("%s\n", $class->getName());
+ $instance = $class->newInstance();
+ $serialized = serialize($instance);
+ $unserialized = unserialize($serialized);
+
+ foreach (["toString", "toArray"] as $m) {
+ if ($class->hasMethod($m)) {
+ #printf("%s#%s\n", $class->getName(), $m);
+ $unserialized->$m();
+ }
+ }
+ if ($class->hasMethod("attach") && !$class->implementsInterface("\\SplSubject")) {
+ #printf("%s#%s\n", $class->getName(), "attach");
+ $unserialized->attach((new http\Curl\Client)->setRequest(new http\Client\Request("GET", "http://localhost")));
+ }
+ }
+}
+?>
+DONE
+--EXPECTF--
+DONE