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 $this->assertCount(2, $this->r
->getResponseMessage()->getCookies());
99 function testResetCookies() {
100 $this->r
->setRequest(new http\Client\
Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"));
102 $this->r
->enableCookies();
103 $this->r
->send(null);
105 $f = function ($a) { return $a->getCookies(); };
106 $c = array_map($f, $this->r
->getResponseMessage()->getCookies());
108 $this->r
->send(null);
109 $this->assertEquals($c, array_map($f, $this->r
->getResponseMessage()->getCookies()));
111 $this->r
->resetCookies();
112 $this->r
->send(null);
113 $this->assertNotEquals($c, array_map($f, $this->r
->getResponseMessage()->getCookies()));
117 $this->r
->setRequest(new http\Client\
Request("GET", "https://twitter.com/"));
118 $this->r
->setSslOptions(array("verify_peer" => true));
119 $this->r
->addSslOptions(array("verify_host" => 2));
122 "verify_peer" => true,
125 $this->r
->getSslOptions()
128 $ti = $this->r
->getTransferInfo();
129 $this->assertArrayHasKey("ssl_engines", $ti);
130 $this->assertGreaterThan(0, count($ti["ssl_engines"]));
133 function testHistory() {
134 $body = new http\Message\Body
;
135 $body->append("foobar");
137 $request = new http\Client\Request
;
138 $request->setBody($body);
139 $request->setRequestMethod("POST");
140 $request->setRequestUrl("http://dev.iworks.at/ext-http/.print_request.php");
142 $this->r
->recordHistory
= true;
143 $this->r
->send($request);
145 $this->assertStringMatchesFormat(<<<HTTP
146 POST /ext-http/.print_request.php HTTP/1.1
151 Content
-Type
: application
/x
-www
-form
-urlencoded
152 X
-Original
-Content
-Length
: 6
160 Content
-Type
: text
/html
161 X
-Original
-Content
-Length
: 19
166 , str_replace("\r", "", $this->r
->getHistory()->toString(true))
170 $this->r
->send($request);
172 $this->assertStringMatchesFormat(<<<HTTP
173 POST /ext-http/.print_request.php HTTP/1.1
178 Content
-Type
: application
/x
-www
-form
-urlencoded
179 X
-Original
-Content
-Length
: 6
187 Content
-Type
: text
/html
188 X
-Original
-Content
-Length
: 19
192 POST
/ext
-http
/.print_request
.php HTTP
/1.1
197 Content
-Type
: application
/x
-www
-form
-urlencoded
198 X
-Original
-Content
-Length
: 6
206 Content
-Type
: text
/html
207 X
-Original
-Content
-Length
: 19
212 , str_replace("\r", "", $this->r
->getHistory()->toString(true))