From 05ae9a5d01e29d7174152b1b46a5af09833d71ef Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Sun, 9 Dec 2012 22:24:33 +0000 Subject: [PATCH] tests++ --- phpunit/DataShareTest.php | 17 ++++ phpunit/EncodingTest.php | 3 +- phpunit/MessageTest.php | 159 ++++++++++++++++++++++++++++++++++++++ phpunit/RequestTest.php | 22 +++++- tests/clientpool002.phpt | 82 ++++++++++++++++++++ tests/info.phpt | 19 +++++ tests/phpunit.phpt | 1 + tests/response004.phpt | 27 +++++++ tests/response005.phpt | 23 ++++++ 9 files changed, 351 insertions(+), 2 deletions(-) create mode 100644 phpunit/MessageTest.php create mode 100644 tests/clientpool002.phpt create mode 100644 tests/info.phpt create mode 100644 tests/response004.phpt create mode 100644 tests/response005.phpt diff --git a/phpunit/DataShareTest.php b/phpunit/DataShareTest.php index 0a50727..f4a0453 100644 --- a/phpunit/DataShareTest.php +++ b/phpunit/DataShareTest.php @@ -1,5 +1,11 @@ detach($client); $share->reset(); } + + function testCurlIncompatible() { + $client = new UserClient; + $client->setRequest(new http\Client\Request("GET", "https://twitter.com")); + + $share = new http\Curl\Client\DataShare; + $this->setExpectedException("PHPUnit_Framework_Error_Warning"); + $share->attach($client); + $this->setExpectedException("PHPUnit_Framework_Error_Warning"); + $share->detach($client); + } } diff --git a/phpunit/EncodingTest.php b/phpunit/EncodingTest.php index 5fb2df3..b57e2a4 100644 --- a/phpunit/EncodingTest.php +++ b/phpunit/EncodingTest.php @@ -19,10 +19,11 @@ class EncodingStreamTest extends PHPUnit_Framework_TestCase { } function testChunkNotEncodedNotice() { + error_reporting(E_ALL); $this->setExpectedException("PHPUnit_Framework_Error_Notice", "Data does not seem to be chunked encoded"); $s = "this is apparently not encodded\n"; - http\Encoding\Stream\Dechunk::decode($s); + $this->assertEquals($s, http\Encoding\Stream\Dechunk::decode($s)); } function testChunkNotEncodedFail() { diff --git a/phpunit/MessageTest.php b/phpunit/MessageTest.php new file mode 100644 index 0000000..6f42524 --- /dev/null +++ b/phpunit/MessageTest.php @@ -0,0 +1,159 @@ +str = $str; + } + function __toString() { + return (string) $this->str; + } +} + +class message extends http\Message { + function __call($m, $args) { + if (preg_match("/^test(get|set)(.*)\$/i", $m, $match)) { + list(, $getset, $prop) = $match; + $prop = strtolower($prop[0]).substr($prop,1); + switch(strtolower($getset)) { + case "get": + return $this->$prop; + case "set": + $this->$prop = current($args); + break; + } + } + } +} + +class MessageTest extends PHPUnit_Framework_TestCase +{ + function testProperties() { + $test = new message; + $this->assertEquals(0, $test->testGetType()); + $this->assertEquals(null, $test->testGetBody()); + $this->assertEquals("", $test->testGetRequestMethod()); + $this->assertEquals("", $test->testGetRequestUrl()); + $this->assertEquals("", $test->testGetResponseStatus()); + $this->assertEquals(0, $test->testGetResponseCode()); + $this->assertEquals("1.1", $test->testGetHttpVersion()); + $this->assertEquals(array(), $test->testGetHeaders()); + $this->assertEquals(null, $test->testGetParentMessage()); + $test->testSetType(http\Message::TYPE_REQUEST); + $this->assertEquals(http\Message::TYPE_REQUEST, $test->testGetType()); + $this->assertEquals(http\Message::TYPE_REQUEST, $test->getType()); + $body = new http\Message\Body; + $test->testSetBody($body); + $this->assertEquals($body, $test->testGetBody()); + $this->assertEquals($body, $test->getBody()); + $test->testSetRequestMethod("HEAD"); + $this->assertEquals("HEAD", $test->testGetRequestMethod()); + $this->assertEquals("HEAD", $test->getRequestMethod()); + $test->testSetRequestUrl("/"); + $this->assertEquals("/", $test->testGetRequestUrl()); + $this->assertEquals("/", $test->getRequestUrl()); + $this->assertEquals("HEAD / HTTP/1.1", $test->getInfo()); + $test->testSetType(http\Message::TYPE_RESPONSE); + $test->setResponseStatus("Created"); + $this->assertEquals("Created", $test->testGetResponseStatus()); + $this->assertEquals("Created", $test->getResponseStatus()); + $test->setResponseCode(201); + $this->assertEquals(201, $test->testGetResponseCode()); + $this->assertEquals(201, $test->getResponseCode()); + $test->testSetResponseStatus("Ok"); + $this->assertEquals("Ok", $test->testGetResponseStatus()); + $this->assertEquals("Ok", $test->getResponseStatus()); + $test->testSetResponseCode(200); + $this->assertEquals(200, $test->testGetResponseCode()); + $this->assertEquals(200, $test->getResponseCode()); + $test->testSetHttpVersion("1.0"); + $this->assertEquals("1.0", $test->testGetHttpVersion()); + $this->assertEquals("1.0", $test->getHttpVersion()); + $this->assertEquals("HTTP/1.0 200 OK", $test->getInfo()); + $test->setHttpVersion("1.1"); + $this->assertEquals("1.1", $test->testGetHttpVersion()); + $this->assertEquals("1.1", $test->getHttpVersion()); + $this->assertEquals("HTTP/1.1 200 OK", $test->getInfo()); + $test->setInfo("HTTP/1.1 201 Created"); + $this->assertEquals("Created", $test->testGetResponseStatus()); + $this->assertEquals("Created", $test->getResponseStatus()); + $this->assertEquals(201, $test->testGetResponseCode()); + $this->assertEquals(201, $test->getResponseCode()); + $this->assertEquals("1.1", $test->testGetHttpVersion()); + $this->assertEquals("1.1", $test->getHttpVersion()); + $test->testSetHeaders(array("Foo" => "bar")); + $this->assertEquals(array("Foo" => "bar"), $test->testGetHeaders()); + $this->assertEquals(array("Foo" => "bar"), $test->getHeaders()); + $this->assertEquals("bar", $test->getHeader("foo")); + $this->assertEquals(null, $test->getHeader("bar")); + $parent = new message; + $test->testSetParentMessage($parent); + $this->assertEquals($parent, $test->testGetParentMessage()); + $this->assertEquals($parent, $test->getParentMessage()); + } + + function testAddBody() { + $m = new http\Message; + $body = new http\Message\Body; + $body->append("foo"); + $m->addBody($body); + $body = new http\Message\Body; + $body->append("bar"); + $m->addBody($body); + $this->assertEquals("foobar", (string) $m->getBody()); + } + + function testAddHeaders() { + $m = new http\Message; + $m->addHeaders(array("foo"=>"bar","bar"=>"foo")); + $this->assertEquals(array("Foo"=>"bar","Bar"=>"foo"), $m->getHeaders()); + $m->addHeaders(array("key"=>"val","more"=>"Stuff")); + $this->assertEquals(array("Foo"=>"bar","Bar"=>"foo","Key"=>"val","More"=>"Stuff"), $m->getHeaders()); + } + + function testArrayHeaders() { + $m = new http\Message("GET / HTTP/1.1"); + $m->addHeader("Accept", "text/html"); + $m->addHeader("Accept", "text/xml;q=0"); + $m->addHeader("Accept", "text/plain;q=0.5"); + $this->assertEquals( + "GET / HTTP/1.1\r\n". + "Accept: text/html, text/xml;q=0, text/plain;q=0.5\r\n", + $m->toString() + ); + } + + function testHeaderValueTypes() { + $m = new http\Message("HTTP/1.1 200 Ok"); + $m->addHeader("Bool", true); + $m->addHeader("Int", 123); + $m->addHeader("Float", 1.23); + $m->addHeader("Array", array(1,2,3)); + $m->addHeader("Object", new strval("test")); + $m->addHeader("Set-Cookie", + array( + array( + "cookies" => array("foo" => "bar"), + "expires" => date_create("2012-12-31 23:59:59")->format( + DateTime::COOKIE + ), + "path" => "/somewhere" + ) + ) + ); + $m->addHeader("Set-Cookie", "val=0"); + + $this->assertEquals( + "HTTP/1.1 200 Ok\r\n". + "Bool: true\r\n". + "Int: 123\r\n". + "Float: 1.23\r\n". + "Array: 1, 2, 3\r\n". + "Object: test\r\n". + "Set-Cookie: foo=bar; path=/somewhere; expires=Mon, 31 Dec 2012 22:59:59 GMT; \r\n". + "Set-Cookie: val=0\r\n", + $m->toString() + ); + } +} + diff --git a/phpunit/RequestTest.php b/phpunit/RequestTest.php index 136b29f..0072714 100644 --- a/phpunit/RequestTest.php +++ b/phpunit/RequestTest.php @@ -93,7 +93,27 @@ class RequestTest extends PHPUnit_Framework_TestCase $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage()); $this->r->send(null); $this->assertContains("Cookie", (string) $this->r->getRequestMessage()); - $this->assertCount(2, $this->r->getResponseMessage()->getCookies()); + $cookies = $this->r->getResponseMessage()->getCookies(0, array("extra")); + $this->assertCount(2, $cookies); + foreach ($cookies as $cookie) { + if ($cookie->getCookie("perm")) { + $this->assertTrue(0 < $cookie->getExpires()); + } + if ($cookie->getCookie("temp")) { + $this->assertEquals(-1, $cookie->getExpires()); + } + } + $this->r->send(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/.cookie1.php")); + $cookies = $this->r->getResponseMessage()->getCookies(0, array("bar")); + $this->assertCount(1, $cookies); + $cookies = $cookies[0]; + $this->assertEquals(array("bar"=>"foo"), $cookies->getExtras()); + $this->assertEquals(array("foo"=>"bar"), $cookies->getCookies()); + $cookies = $this->r->getResponseMessage()->getCookies(0, array("foo")); + $this->assertCount(1, $cookies); + $cookies = $cookies[0]; + $this->assertEquals(array("foo"=>"bar","bar"=>"foo"), $cookies->getCookies()); + $this->assertEquals(array(), $cookies->getExtras()); } function testResetCookies() { diff --git a/tests/clientpool002.phpt b/tests/clientpool002.phpt new file mode 100644 index 0000000..731dd2b --- /dev/null +++ b/tests/clientpool002.phpt @@ -0,0 +1,82 @@ +--TEST-- +pool iteration +--SKIPIF-- + +--FILE-- +getAttached() as $c) { + if ($c !== $client) { + echo "."; + } +} +echo "\n"; +echo "Finished: "; +foreach ($pool->getFinished() as $c) { + echo "."; +} +echo "\n"; + +echo "USER\n"; + +$client = new UserClient(); +$pool = new UserPool(); + +echo "Iterator: "; +foreach ($pool as $c) { + if ($c !== $client) { + echo "."; + } +} +echo "\n"; +echo "Attached: "; +foreach ($pool->getAttached() as $c) { + if ($c !== $client) { + echo "."; + } +} +echo "\n"; +echo "Finished: "; +foreach ($pool->getFinished() as $c) { + echo "."; +} +echo "\n"; +?> +Done +--EXPECT-- +Test +CURL +Iterator: +Attached: +Finished: +USER +Iterator: +Attached: +Finished: +Done \ No newline at end of file diff --git a/tests/info.phpt b/tests/info.phpt new file mode 100644 index 0000000..373cb45 --- /dev/null +++ b/tests/info.phpt @@ -0,0 +1,19 @@ +--TEST-- +phpinfo +--SKIPIF-- + +--FILE-- + +Done +--EXPECTF-- +Test +%a +HTTP Support => enabled +Extension Version => 2.%s +%a +Done diff --git a/tests/phpunit.phpt b/tests/phpunit.phpt index 9c106f0..22f41e5 100644 --- a/tests/phpunit.phpt +++ b/tests/phpunit.phpt @@ -2,6 +2,7 @@ unit tests --SKIPIF-- --FILE-- diff --git a/tests/response004.phpt b/tests/response004.phpt new file mode 100644 index 0000000..9d0d3eb --- /dev/null +++ b/tests/response004.phpt @@ -0,0 +1,27 @@ +--TEST-- +reponse callback +--SKIPIF-- + +--GET-- +dummy=1 +--FILE-- +setCacheControl("public,must-revalidate,max-age=0"); +$r->setThrottleRate(1, 0.1); +ob_start($r); + +echo "foo"; +echo "bar"; + +ob_end_flush(); +$r->send(); +--EXPECTHEADERS-- +Accept-Ranges: bytes +Cache-Control: public,must-revalidate,max-age=0 +ETag: "9ef61f95" +--EXPECTF-- +foobar diff --git a/tests/response005.phpt b/tests/response005.phpt new file mode 100644 index 0000000..f4f747d --- /dev/null +++ b/tests/response005.phpt @@ -0,0 +1,23 @@ +--TEST-- +http response cache positive +--SKIPIF-- + +--GET-- +a=b +--ENV-- +HTTP_IF_MODIFIED_SINCE=Fri, 13 Feb 2009 23:31:32 GMT +--FILE-- +setBody(new http\Message\Body(fopen(__FILE__,"rb"))); +$r->setEtag("abc"); +$r->setLastModified(1234567891); +$r->isCachedByEtag("If-None-Match") and die("Huh? etag? really?\n"); +$r->isCachedByLastModified("If-Modified-Since") or die("yep, I should be cached"); +$r->send(); +?> +--EXPECTHEADERS-- +HTTP/1.1 304 Not Modified +ETag: "abc" +Last-Modified: Fri, 13 Feb 2009 23:31:31 GMT +--EXPECT-- -- 2.30.2