3 class ProgressObserver1
implements SplObserver
{
4 function update(SplSubject
$r) {
5 if ($r->getProgress()) $r->pi
.= "-";
8 class ProgressObserver2
implements SplObserver
{
9 function update(SplSubject
$r) {
10 if ($r->getProgress()) $r->pi
.= ".";
13 class CallbackObserver
implements SplObserver
{
15 function __construct($callback) {
16 $this->callback
= $callback;
18 function update(SplSubject
$r) {
19 call_user_func($this->callback
, $r);
23 class RequestTest
extends PHPUnit_Framework_TestCase
31 $f = new http\Client\Factory
;
32 $this->r
= $f->createClient();
35 "connecttimeout" => 30,
46 "connecttimeout" => 30,
49 $this->r
->getOptions()
53 function testClone() {
55 $this->assertNotSame($this->r
, $c);
58 function testObserver() {
60 $this->r
->attach($o1 = new ProgressObserver1
);
61 $this->r
->attach($o2 = new ProgressObserver2
);
63 $o3 = new CallbackObserver(
64 function ($r) use ($test) {
65 $p = (array) $r->getProgress();
66 $test->assertArrayHasKey("started", $p);
67 $test->assertArrayHasKey("finished", $p);
68 $test->assertArrayHasKey("dlnow", $p);
69 $test->assertArrayHasKey("ulnow", $p);
70 $test->assertArrayHasKey("dltotal", $p);
71 $test->assertArrayHasKey("ultotal", $p);
72 $test->assertArrayHasKey("info", $p);
76 $this->r
->setRequest(new http\Client\
Request("GET", "http://dev.iworks.at/ext-http/"))->send(null);
77 $this->assertRegexp("/(\.-)+/", $this->r
->pi
);
78 $this->assertCount(3, $this->r
->getObservers());
79 $this->r
->detach($o1);
80 $this->assertCount(2, $this->r
->getObservers());
81 $this->r
->detach($o2);
82 $this->assertCount(1, $this->r
->getObservers());
83 $this->r
->detach($o3);
84 $this->assertCount(0, $this->r
->getObservers());
87 function testCookies() {
88 $this->r
->setRequest(new http\Client\
Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"))->send(null);
89 $this->assertNotContains("Cookie", (string) $this->r
->getRequestMessage());
91 $this->assertNotContains("Cookie", (string) $this->r
->getRequestMessage());
92 $this->r
->enableCookies()->send(null);
93 $this->assertNotContains("Cookie", (string) $this->r
->getRequestMessage());
95 $this->assertContains("Cookie", (string) $this->r
->getRequestMessage());
96 $cookies = $this->r
->getResponseMessage()->getCookies(0, array("extra"));
97 $this->assertCount(2, $cookies);
98 foreach ($cookies as $cookie) {
99 if ($cookie->getCookie("perm")) {
100 $this->assertTrue(0 < $cookie->getExpires());
102 if ($cookie->getCookie("temp")) {
103 $this->assertEquals(-1, $cookie->getExpires());
106 $this->r
->send(new http\Client\
Request("GET", "http://dev.iworks.at/ext-http/.cookie1.php"));
107 $cookies = $this->r
->getResponseMessage()->getCookies(0, array("bar"));
108 $this->assertCount(1, $cookies);
109 $cookies = $cookies[0];
110 $this->assertEquals(array("bar"=>"foo"), $cookies->getExtras());
111 $this->assertEquals(array("foo"=>"bar"), $cookies->getCookies());
112 $cookies = $this->r
->getResponseMessage()->getCookies(0, array("foo"));
113 $this->assertCount(1, $cookies);
114 $cookies = $cookies[0];
115 $this->assertEquals(array("foo"=>"bar","bar"=>"foo"), $cookies->getCookies());
116 $this->assertEquals(array(), $cookies->getExtras());
119 function testResetCookies() {
120 $this->r
->setRequest(new http\Client\
Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"));
122 $this->r
->enableCookies();
123 $this->r
->send(null);
125 $f = function ($a) { return $a->getCookies(); };
126 $c = array_map($f, $this->r
->getResponseMessage()->getCookies());
128 $this->r
->send(null);
129 $this->assertEquals($c, array_map($f, $this->r
->getResponseMessage()->getCookies()));
131 $this->r
->resetCookies();
132 $this->r
->send(null);
133 $this->assertNotEquals($c, array_map($f, $this->r
->getResponseMessage()->getCookies()));
137 $this->r
->setRequest(new http\Client\
Request("GET", "https://twitter.com/"));
138 $this->r
->setSslOptions(array("verify_peer" => true));
139 $this->r
->addSslOptions(array("verify_host" => 2));
142 "verify_peer" => true,
145 $this->r
->getSslOptions()
148 $ti = $this->r
->getTransferInfo();
149 $this->assertArrayHasKey("ssl_engines", $ti);
150 $this->assertGreaterThan(0, count($ti["ssl_engines"]));
153 function testHistory() {
154 $body = new http\Message\Body
;
155 $body->append("foobar");
157 $request = new http\Client\Request
;
158 $request->setBody($body);
159 $request->setRequestMethod("POST");
160 $request->setRequestUrl("http://dev.iworks.at/ext-http/.print_request.php");
162 $this->r
->recordHistory
= true;
163 $this->r
->send($request);
165 $this->assertStringMatchesFormat(<<<HTTP
166 POST /ext-http/.print_request.php HTTP/1.1
171 Content
-Type
: application
/x
-www
-form
-urlencoded
172 X
-Original
-Content
-Length
: 6
180 Content
-Type
: text
/html
181 X
-Original
-Content
-Length
: 19
186 , str_replace("\r", "", $this->r
->getHistory()->toString(true))
190 $this->r
->send($request);
192 $this->assertStringMatchesFormat(<<<HTTP
193 POST /ext-http/.print_request.php HTTP/1.1
198 Content
-Type
: application
/x
-www
-form
-urlencoded
199 X
-Original
-Content
-Length
: 6
207 Content
-Type
: text
/html
208 X
-Original
-Content
-Length
: 19
212 POST
/ext
-http
/.print_request
.php HTTP
/1.1
217 Content
-Type
: application
/x
-www
-form
-urlencoded
218 X
-Original
-Content
-Length
: 6
226 Content
-Type
: text
/html
227 X
-Original
-Content
-Length
: 19
232 , str_replace("\r", "", $this->r
->getHistory()->toString(true))