tests++
[m6w6/ext-http] / phpunit / RequestTest.php
index fccad01edfec3b93feda3119c8f5c9f6ff9e08c8..0072714c4c953789cd389da55125afd242ba3e76 100644 (file)
@@ -28,84 +28,140 @@ class RequestTest extends PHPUnit_Framework_TestCase
     protected $r;
 
     function setUp() {
-        $this->r = (new http\Request\Factory)->createRequest();
+               $f = new http\Client\Factory;
+        $this->r = $f->createClient();
         $this->r->setOptions(
             array(
                 "connecttimeout"    => 30,
                 "timeout"           => 300,
             )
         );
+        $this->r->setOptions(
+               array(
+                       "timeout" => 600
+               )
+        );
+        $this->assertEquals(
+               array(
+                       "connecttimeout" => 30,
+                       "timeout" => 600,
+               ),
+               $this->r->getOptions()
+        );
     }
 
     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());
+        $this->assertNotSame($this->r, $c);
     }
 
     function testObserver() {
-        $this->r->attach(new ProgressObserver1);
-        $this->r->attach(new ProgressObserver2);
+               $test = $this;
+        $this->r->attach($o1 = new ProgressObserver1);
+        $this->r->attach($o2 = new ProgressObserver2);
         $this->r->attach(
-            new CallbackObserver(
-                function ($r) {
+            $o3 = new CallbackObserver(
+                function ($r) use ($test) {
                     $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);
+                    $test->assertArrayHasKey("started", $p);
+                    $test->assertArrayHasKey("finished", $p);
+                    $test->assertArrayHasKey("dlnow", $p);
+                    $test->assertArrayHasKey("ulnow", $p);
+                    $test->assertArrayHasKey("dltotal", $p);
+                    $test->assertArrayHasKey("ultotal", $p);
+                    $test->assertArrayHasKey("info", $p);
                 }
             )
         );
-        $this->r->setUrl("http://dev.iworks.at/ext-http/")->send();
+        $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->setUrl("http://dev.iworks.at/ext-http/.cookie.php")->send();
+        $this->r->setRequest(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"))->send(null);
         $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
-        $this->r->send();
+        $this->r->send(null);
         $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
-        $this->r->enableCookies()->send();
+        $this->r->enableCookies()->send(null);
         $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
-        $this->r->send();
+        $this->r->send(null);
         $this->assertContains("Cookie", (string) $this->r->getRequestMessage());
-        $this->assertCount(2, $this->r->getResponseCookies());
+        $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() {
-        $this->r->setUrl("http://dev.iworks.at/ext-http/.cookie.php");
+        $this->r->setRequest(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"));
 
         $this->r->enableCookies();
-        $this->r->send();
+        $this->r->send(null);
 
         $f = function ($a) { return $a->getCookies(); };
-        $c = array_map($f, $this->r->getResponseCookies());
+        $c = array_map($f, $this->r->getResponseMessage()->getCookies());
 
-        $this->r->send();
-        $this->assertEquals($c, array_map($f, $this->r->getResponseCookies()));
+        $this->r->send(null);
+        $this->assertEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
         
         $this->r->resetCookies();
-        $this->r->send();
-        $this->assertNotEquals($c, array_map($f, $this->r->getResponseCookies()));
+        $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;
         $body->append("foobar");
-        $this->r->setBody($body);
 
-        $this->r->recordHistory = true;
+        $request = new http\Client\Request;
+        $request->setBody($body);
+        $request->setRequestMethod("POST");
+        $request->setRequestUrl("http://dev.iworks.at/ext-http/.print_request.php");
 
-        $this->r->setMethod("POST");
-        $this->r->setUrl("http://dev.iworks.at/ext-http/.print_request.php");
+        $this->r->recordHistory = true;
+        $this->r->send($request);
 
-        $this->r->send();
         $this->assertStringMatchesFormat(<<<HTTP
 POST /ext-http/.print_request.php HTTP/1.1
 User-Agent: %s
@@ -130,7 +186,9 @@ HTTP
         , str_replace("\r", "", $this->r->getHistory()->toString(true))
         );
 
-        $this->r->send();
+
+        $this->r->send($request);
+
         $this->assertStringMatchesFormat(<<<HTTP
 POST /ext-http/.print_request.php HTTP/1.1
 User-Agent: %s