From: Michael Wallner Date: Sun, 28 Oct 2012 20:21:24 +0000 (+0000) Subject: 80%+ test coverage X-Git-Tag: RELEASE_2_1_0_RC3~10^2^2~104 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=commitdiff_plain;h=575f4ce7a82c2c2bf4fb597f6a3a3de077403910;ds=sidebyside 80%+ test coverage --- diff --git a/phpunit/ClientRequestTest.php b/phpunit/ClientRequestTest.php new file mode 100644 index 0000000..f14f49e --- /dev/null +++ b/phpunit/ClientRequestTest.php @@ -0,0 +1,43 @@ +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()); + } +} + diff --git a/phpunit/DataShareTest.php b/phpunit/DataShareTest.php new file mode 100644 index 0000000..0a50727 --- /dev/null +++ b/phpunit/DataShareTest.php @@ -0,0 +1,58 @@ +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(); + } +} + diff --git a/phpunit/PoolTest.php b/phpunit/PoolTest.php new file mode 100644 index 0000000..33dbf9b --- /dev/null +++ b/phpunit/PoolTest.php @@ -0,0 +1,79 @@ +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(); + } +} + diff --git a/phpunit/RequestTest.php b/phpunit/RequestTest.php index 56053ee..a327f6d 100644 --- a/phpunit/RequestTest.php +++ b/phpunit/RequestTest.php @@ -35,6 +35,18 @@ class RequestTest extends PHPUnit_Framework_TestCase "timeout" => 300, ) ); + $this->r->setOptions( + array( + "timeout" => 600 + ) + ); + $this->assertEquals( + array( + "connecttimeout" => 30, + "timeout" => 600, + ), + $this->r->getOptions() + ); } function testClone() { @@ -43,10 +55,10 @@ class RequestTest extends PHPUnit_Framework_TestCase } 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); @@ -62,6 +74,12 @@ class RequestTest extends PHPUnit_Framework_TestCase $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() { @@ -92,6 +110,23 @@ class RequestTest extends PHPUnit_Framework_TestCase $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; diff --git a/tests/envrequestfiles001.phpt b/tests/envrequestfiles001.phpt new file mode 100644 index 0000000..5b1cd2e --- /dev/null +++ b/tests/envrequestfiles001.phpt @@ -0,0 +1,40 @@ +--TEST-- +http\Env\Request grabbing $_FILES +--SKIPIF-- + +--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-- +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 diff --git a/tests/envrequestfiles002.phpt b/tests/envrequestfiles002.phpt new file mode 100644 index 0000000..85b44db --- /dev/null +++ b/tests/envrequestfiles002.phpt @@ -0,0 +1,47 @@ +--TEST-- +http\Env\Request grabbing $_FILES from array +--SKIPIF-- + +--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-- + $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 diff --git a/tests/envrequestform.phpt b/tests/envrequestform.phpt new file mode 100644 index 0000000..c2086bb --- /dev/null +++ b/tests/envrequestform.phpt @@ -0,0 +1,24 @@ +--TEST-- +http\Env\Request getForm +--SKIPIF-- + +--POST-- +a=b&b=c&r[]=1&r[]=2 +--FILE-- +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 diff --git a/tests/envrequestquery.phpt b/tests/envrequestquery.phpt new file mode 100644 index 0000000..87f168d --- /dev/null +++ b/tests/envrequestquery.phpt @@ -0,0 +1,24 @@ +--TEST-- +http\Env\Request getQuery +--SKIPIF-- + +--GET-- +a=b&b=c&r[]=1&r[]=2 +--FILE-- +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 diff --git a/tests/params002.phpt b/tests/params002.phpt new file mode 100644 index 0000000..66e514c --- /dev/null +++ b/tests/params002.phpt @@ -0,0 +1,60 @@ +--TEST-- +query parser +--SKIPIF-- + +--FILE-- + +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 + diff --git a/tests/serialize001.phpt b/tests/serialize001.phpt new file mode 100644 index 0000000..2e06afe --- /dev/null +++ b/tests/serialize001.phpt @@ -0,0 +1,41 @@ +--TEST-- +serialization +--SKIPIF-- + +--FILE-- +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