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 $this->r
= (new http\Client\Factory
)->createClient();
34 "connecttimeout" => 30,
45 "connecttimeout" => 30,
48 $this->r
->getOptions()
52 function testClone() {
54 $this->assertNotSame($this->r
, $c);
57 function testObserver() {
58 $this->r
->attach($o1 = new ProgressObserver1
);
59 $this->r
->attach($o2 = new ProgressObserver2
);
61 $o3 = new CallbackObserver(
63 $p = (array) $r->getProgress();
64 $this->assertArrayHasKey("started", $p);
65 $this->assertArrayHasKey("finished", $p);
66 $this->assertArrayHasKey("dlnow", $p);
67 $this->assertArrayHasKey("ulnow", $p);
68 $this->assertArrayHasKey("dltotal", $p);
69 $this->assertArrayHasKey("ultotal", $p);
70 $this->assertArrayHasKey("info", $p);
74 $this->r
->setRequest(new http\Client\
Request("GET", "http://dev.iworks.at/ext-http/"))->send(null);
75 $this->assertRegexp("/(\.-)+/", $this->r
->pi
);
76 $this->assertCount(3, $this->r
->getObservers());
77 $this->r
->detach($o1);
78 $this->assertCount(2, $this->r
->getObservers());
79 $this->r
->detach($o2);
80 $this->assertCount(1, $this->r
->getObservers());
81 $this->r
->detach($o3);
82 $this->assertCount(0, $this->r
->getObservers());
85 function testCookies() {
86 $this->r
->setRequest(new http\Client\
Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"))->send(null);
87 $this->assertNotContains("Cookie", (string) $this->r
->getRequestMessage());
89 $this->assertNotContains("Cookie", (string) $this->r
->getRequestMessage());
90 $this->r
->enableCookies()->send(null);
91 $this->assertNotContains("Cookie", (string) $this->r
->getRequestMessage());
93 $this->assertContains("Cookie", (string) $this->r
->getRequestMessage());
94 $this->assertCount(2, $this->r
->getResponseMessage()->getCookies());
97 function testResetCookies() {
98 $this->r
->setRequest(new http\Client\
Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"));
100 $this->r
->enableCookies();
101 $this->r
->send(null);
103 $f = function ($a) { return $a->getCookies(); };
104 $c = array_map($f, $this->r
->getResponseMessage()->getCookies());
106 $this->r
->send(null);
107 $this->assertEquals($c, array_map($f, $this->r
->getResponseMessage()->getCookies()));
109 $this->r
->resetCookies();
110 $this->r
->send(null);
111 $this->assertNotEquals($c, array_map($f, $this->r
->getResponseMessage()->getCookies()));
115 $this->r
->setRequest(new http\Client\
Request("GET", "https://twitter.com/"));
116 $this->r
->setSslOptions(array("verify_peer" => true));
117 $this->r
->addSslOptions(array("verify_host" => 2));
120 "verify_peer" => true,
123 $this->r
->getSslOptions()
126 $ti = $this->r
->getTransferInfo();
127 $this->assertArrayHasKey("ssl_engines", $ti);
128 $this->assertGreaterThan(0, count($ti["ssl_engines"]));
131 function testHistory() {
132 $body = new http\Message\Body
;
133 $body->append("foobar");
135 $request = new http\Client\Request
;
136 $request->setBody($body);
137 $request->setRequestMethod("POST");
138 $request->setRequestUrl("http://dev.iworks.at/ext-http/.print_request.php");
140 $this->r
->recordHistory
= true;
141 $this->r
->send($request);
143 $this->assertStringMatchesFormat(<<<HTTP
144 POST /ext-http/.print_request.php HTTP/1.1
149 Content
-Type
: application
/x
-www
-form
-urlencoded
150 X
-Original
-Content
-Length
: 6
158 Content
-Type
: text
/html
159 X
-Original
-Content
-Length
: 19
164 , str_replace("\r", "", $this->r
->getHistory()->toString(true))
168 $this->r
->send($request);
170 $this->assertStringMatchesFormat(<<<HTTP
171 POST /ext-http/.print_request.php HTTP/1.1
176 Content
-Type
: application
/x
-www
-form
-urlencoded
177 X
-Original
-Content
-Length
: 6
185 Content
-Type
: text
/html
186 X
-Original
-Content
-Length
: 19
190 POST
/ext
-http
/.print_request
.php HTTP
/1.1
195 Content
-Type
: application
/x
-www
-form
-urlencoded
196 X
-Original
-Content
-Length
: 6
204 Content
-Type
: text
/html
205 X
-Original
-Content
-Length
: 19
210 , str_replace("\r", "", $this->r
->getHistory()->toString(true))