tests++
authorMichael Wallner <mike@php.net>
Sun, 9 Dec 2012 22:24:33 +0000 (22:24 +0000)
committerMichael Wallner <mike@php.net>
Sun, 9 Dec 2012 22:24:33 +0000 (22:24 +0000)
phpunit/DataShareTest.php
phpunit/EncodingTest.php
phpunit/MessageTest.php [new file with mode: 0644]
phpunit/RequestTest.php
tests/clientpool002.phpt [new file with mode: 0644]
tests/info.phpt [new file with mode: 0644]
tests/phpunit.phpt
tests/response004.phpt [new file with mode: 0644]
tests/response005.phpt [new file with mode: 0644]

index 0a50727..f4a0453 100644 (file)
@@ -1,5 +1,11 @@
 <?php
 
+class UserClient extends http\Client\AbstractClient {
+       function send($request = null) {
+       }
+}
+       
+
 class DataShareTest extends PHPUnit_Framework_TestCase
 {
     function testStandard() {
@@ -54,5 +60,16 @@ class DataShareTest extends PHPUnit_Framework_TestCase
        $share->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);
+    }
 }
 
index 5fb2df3..b57e2a4 100644 (file)
@@ -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 (file)
index 0000000..6f42524
--- /dev/null
@@ -0,0 +1,159 @@
+<?php
+
+class strval {
+       private $str;
+       function __construct($str) {
+               $this->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()
+               );
+       }
+}
+
index 136b29f..0072714 100644 (file)
@@ -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 (file)
index 0000000..731dd2b
--- /dev/null
@@ -0,0 +1,82 @@
+--TEST--
+pool iteration
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+
+use http\Client;
+use http\Curl;
+
+class UserPool extends Client\Pool\AbstractPool {
+}
+class UserClient extends Client\AbstractClient {
+       function send($request = null) {
+       }
+}
+
+echo "CURL\n";
+
+$client = new Curl\Client();
+$pool = new Curl\Client\Pool();
+
+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";
+
+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 (file)
index 0000000..373cb45
--- /dev/null
@@ -0,0 +1,19 @@
+--TEST--
+phpinfo
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--FILE--
+<?php
+echo "Test\n";
+phpinfo(INFO_MODULES);
+?>
+Done
+--EXPECTF--
+Test
+%a
+HTTP Support => enabled
+Extension Version => 2.%s
+%a
+Done
index 9c106f0..22f41e5 100644 (file)
@@ -2,6 +2,7 @@
 unit tests
 --SKIPIF--
 <?php
+include "skipif.inc";
 if (!@include_once "PHPUnit/Autoload.php") die("skip need PHPUnit in include_path");
 ?>
 --FILE--
diff --git a/tests/response004.phpt b/tests/response004.phpt
new file mode 100644 (file)
index 0000000..9d0d3eb
--- /dev/null
@@ -0,0 +1,27 @@
+--TEST--
+reponse callback
+--SKIPIF--
+<?php
+include "skipif.inc";
+?>
+--GET--
+dummy=1
+--FILE--
+<?php
+
+$r = new http\Env\Response;
+$r->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 (file)
index 0000000..f4f747d
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+http response cache positive
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--GET--
+a=b
+--ENV--
+HTTP_IF_MODIFIED_SINCE=Fri, 13 Feb 2009 23:31:32 GMT
+--FILE--
+<?php
+$r = new http\Env\Response;
+$r->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--