tests
authorMichael Wallner <mike@php.net>
Mon, 13 Feb 2012 13:31:13 +0000 (13:31 +0000)
committerMichael Wallner <mike@php.net>
Mon, 13 Feb 2012 13:31:13 +0000 (13:31 +0000)
phpunit/RequestTest.php [new file with mode: 0644]
tests/persistenthandles001.phpt [new file with mode: 0644]
tests/requestpool001.phpt
tests/response002.phpt [new file with mode: 0644]

diff --git a/phpunit/RequestTest.php b/phpunit/RequestTest.php
new file mode 100644 (file)
index 0000000..1126328
--- /dev/null
@@ -0,0 +1,178 @@
+<?php
+
+class ProgressObserver1 implements SplObserver {
+    function update(SplSubject $r) {
+        if ($r->getProgress()) $r->pi .= "-";
+    }
+}
+class ProgressObserver2 implements SplObserver {
+    function update(SplSubject $r) {
+        if ($r->getProgress()) $r->pi .= ".";
+    }
+}
+class CallbackObserver implements SplObserver {
+    public $callback;
+    function __construct($callback) {
+        $this->callback = $callback;
+    }
+    function update(SplSubject $r) {
+        call_user_func($this->callback, $r);
+    } 
+}
+
+class RequestTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * @var http\Request
+     */
+    protected $r;
+
+    function setUp() {
+        $this->r = (new http\Request\Factory)->createRequest();
+        $this->r->setOptions(
+            array(
+                "connecttimeout"    => 30,
+                "timeout"           => 300,
+            )
+        );
+    }
+
+    function testClone() {
+        $this->r->setUrl("http://dev.iworks.at/ext-http/.print_request.php");
+        $c = clone $this->r;
+        $c->setUrl("http://dev.iworks.at/ext-http/.print_headers.php");
+        $this->assertNotEquals($this->r->send(), $c->send());
+    }
+
+    function testObserver() {
+        $this->r->attach(new ProgressObserver1);
+        $this->r->attach(new ProgressObserver2);
+        $this->r->attach(
+            new CallbackObserver(
+                function ($r) {
+                    $p = (array) $r->getProgress();
+                    $this->assertArrayHasKey("started", $p);
+                    $this->assertArrayHasKey("finished", $p);
+                    $this->assertArrayHasKey("dlnow", $p);
+                    $this->assertArrayHasKey("ulnow", $p);
+                    $this->assertArrayHasKey("dltotal", $p);
+                    $this->assertArrayHasKey("ultotal", $p);
+                    $this->assertArrayHasKey("info", $p);
+                }
+            )
+        );
+        $this->r->setUrl("http://dev.iworks.at/ext-http/")->send();
+        $this->assertRegexp("/(\.-)+/", $this->r->pi);
+        $this->assertCount(3, $this->r->getObservers());
+    }
+
+    function testCookies() {
+        $this->r->setUrl("http://dev.iworks.at/ext-http/.cookie.php")->send();
+        $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
+        $this->r->send();
+        $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
+        $this->r->enableCookies()->send();
+        $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
+        $this->r->send();
+        $this->assertContains("Cookie", (string) $this->r->getRequestMessage());
+        $this->assertCount(2, $this->r->getResponseCookies());
+    }
+
+    function testResetCookies() {
+        $this->r->setUrl("http://dev.iworks.at/ext-http/.cookie.php");
+
+        $this->r->enableCookies();
+        $this->r->send();
+
+        $f = function ($a) { return $a->getCookies(); };
+        $c = array_map($f, $this->r->getResponseCookies());
+
+        $this->r->send();
+        $this->assertEquals($c, array_map($f, $this->r->getResponseCookies()));
+        
+        $this->r->resetCookies();
+        $this->r->send();
+        $this->assertNotEquals($c, array_map($f, $this->r->getResponseCookies()));
+    }
+
+    function testHistory() {
+        $body = new http\Message\Body;
+        $body->append("foobar");
+        $this->r->setBody($body);
+
+        $this->r->recordHistory = true;
+
+        $this->r->setMethod(http\Request\Method::POST);
+        $this->r->setUrl("http://dev.iworks.at/ext-http/.print_request.php");
+
+        $this->r->send();
+        $this->assertStringMatchesFormat(<<<HTTP
+POST /ext-http/.print_request.php HTTP/1.1
+User-Agent: %s
+Host: dev.iworks.at
+Accept: */*
+Content-Length: 6
+Content-Type: application/x-www-form-urlencoded
+X-Original-Content-Length: 6
+
+foobar
+HTTP/1.1 200 OK
+Date: %s
+Server: %s
+Vary: %s
+Content-Length: 19
+Content-Type: text/html
+X-Original-Content-Length: 19
+
+string(6) "foobar"
+
+HTTP
+        , str_replace("\r", "", $this->r->getHistory()->toString(true))
+        );
+
+        $this->r->send();
+        $this->assertStringMatchesFormat(<<<HTTP
+POST /ext-http/.print_request.php HTTP/1.1
+User-Agent: %s
+Host: dev.iworks.at
+Accept: */*
+Content-Length: 6
+Content-Type: application/x-www-form-urlencoded
+X-Original-Content-Length: 6
+
+foobar
+HTTP/1.1 200 OK
+Date: %s
+Server: %s
+Vary: %s
+Content-Length: 19
+Content-Type: text/html
+X-Original-Content-Length: 19
+
+string(6) "foobar"
+
+POST /ext-http/.print_request.php HTTP/1.1
+User-Agent: %s
+Host: dev.iworks.at
+Accept: */*
+Content-Length: 6
+Content-Type: application/x-www-form-urlencoded
+X-Original-Content-Length: 6
+
+foobar
+HTTP/1.1 200 OK
+Date: %s
+Server: %s
+Vary: %s
+Content-Length: 19
+Content-Type: text/html
+X-Original-Content-Length: 19
+
+string(6) "foobar"
+
+HTTP
+        , str_replace("\r", "", $this->r->getHistory()->toString(true))
+        );
+    }
+}
+
diff --git a/tests/persistenthandles001.phpt b/tests/persistenthandles001.phpt
new file mode 100644 (file)
index 0000000..dd7b8c7
--- /dev/null
@@ -0,0 +1,86 @@
+--TEST--
+persistent handles
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--FILE--
+<?php
+(new http\Request\Factory(array("persistentHandleId" => "foo")))
+    ->createRequest("http://dev.iworks.at")
+    ->setOptions(array("connecttimeout"=> 90, "timeout" =>300))
+    ->send(); 
+$r = (new http\Request\Factory(array("persistentHandleId" => "bar")))
+    ->createRequest("http://dev.iworks.at")
+    ->setOptions(array("connecttimeout"=> 90, "timeout" =>300));
+    
+var_dump(http\Env::statPersistentHandles()); 
+http\Env::cleanPersistentHandles(); 
+var_dump(http\Env::statPersistentHandles());
+
+$r->send();
+
+var_dump(http\Env::statPersistentHandles());
+?>
+DONE
+--EXPECTF--
+object(stdClass)#%d (3) {
+  ["http_request_datashare.curl"]=>
+  array(0) {
+  }
+  ["http_request_pool.curl"]=>
+  array(0) {
+  }
+  ["http_request.curl"]=>
+  array(2) {
+    ["foo"]=>
+    array(2) {
+      ["used"]=>
+      int(0)
+      ["free"]=>
+      int(1)
+    }
+    ["bar"]=>
+    array(2) {
+      ["used"]=>
+      int(1)
+      ["free"]=>
+      int(0)
+    }
+  }
+}
+object(stdClass)#%d (3) {
+  ["http_request_datashare.curl"]=>
+  array(0) {
+  }
+  ["http_request_pool.curl"]=>
+  array(0) {
+  }
+  ["http_request.curl"]=>
+  array(1) {
+    ["bar"]=>
+    array(2) {
+      ["used"]=>
+      int(1)
+      ["free"]=>
+      int(0)
+    }
+  }
+}
+object(stdClass)#%d (3) {
+  ["http_request_datashare.curl"]=>
+  array(0) {
+  }
+  ["http_request_pool.curl"]=>
+  array(0) {
+  }
+  ["http_request.curl"]=>
+  array(1) {
+    ["bar"]=>
+    array(2) {
+      ["used"]=>
+      int(1)
+      ["free"]=>
+      int(0)
+    }
+  }
+}
+DONE
index a65ada45894b0105b33d3d5dbea0f1317f0e29e8..462af710759852cb0e42ea905719625795847711 100644 (file)
@@ -10,8 +10,8 @@ include 'skipif.inc';
 use http\request\Factory as HttpRequestFactory;
 use http\request\Pool as HttpRequestPool;
 use http\request\Method as HttpRequestMethod;
-use http\RequestException as HttpRequestException;
-use http\SocketException as HttpSocketException;
+use http\Exception as HttpRequestException;
+use http\Exception as HttpSocketException;
 
 echo "-TEST\n";
 
@@ -87,7 +87,12 @@ class Pool extends HttpRequestPool
                        
                        $u = $r->getUrl();
                        $c = $r->getResponseCode();
-                       $b = $r->getResponseBody();
+            try {
+                       $b = $r->getResponseBody();
+            } catch (\Exception $e) {
+                echo $e->getMessage(), "\n";
+                $b = "";
+            }
                        
                        printf("%d %s %d\n", $c, $u, strlen($b));
                        
@@ -117,7 +122,7 @@ class Pool extends HttpRequestPool
 }
 
 define('GZIP', true);
-define('TOUT', 50);
+define('TOUT', 300);
 define('RMAX', 10);
 chdir(__DIR__);
 
diff --git a/tests/response002.phpt b/tests/response002.phpt
new file mode 100644 (file)
index 0000000..0a815ad
--- /dev/null
@@ -0,0 +1,28 @@
+--TEST--
+http response cache negative
+--SKIPIF--
+<?php include "skipif.inc"; ?>
+--POST--
+a=b
+--ENV--
+HTTP_IF_MODIFIED_SINCE=Fri, 13 Feb 2009 23:31:30 GMT
+HTTP_IF_NONE_MATCH=0000-00-0000
+--FILE--
+<?php
+$r = new http\Env\Response;
+$r->setBody(new http\Message\Body(fopen(__FILE__,"rb")));
+$r->setEtag("abc");
+$r->setLastModified(1234567891);
+$r->send();
+?>
+--EXPECTHEADERS--
+ETag: "abc"
+Last-Modified: Fri, 13 Feb 2009 23:31:31 GMT
+--EXPECT--
+<?php
+$r = new http\Env\Response;
+$r->setBody(new http\Message\Body(fopen(__FILE__,"rb")));
+$r->setEtag("abc");
+$r->setLastModified(1234567891);
+$r->send();
+?>