tests++
[m6w6/ext-http] / phpunit / RequestTest.php
1 <?php
2
3 class ProgressObserver1 implements SplObserver {
4 function update(SplSubject $r) {
5 if ($r->getProgress()) $r->pi .= "-";
6 }
7 }
8 class ProgressObserver2 implements SplObserver {
9 function update(SplSubject $r) {
10 if ($r->getProgress()) $r->pi .= ".";
11 }
12 }
13 class CallbackObserver implements SplObserver {
14 public $callback;
15 function __construct($callback) {
16 $this->callback = $callback;
17 }
18 function update(SplSubject $r) {
19 call_user_func($this->callback, $r);
20 }
21 }
22
23 class RequestTest extends PHPUnit_Framework_TestCase
24 {
25 /**
26 * @var http\Request
27 */
28 protected $r;
29
30 function setUp() {
31 $f = new http\Client\Factory;
32 $this->r = $f->createClient();
33 $this->r->setOptions(
34 array(
35 "connecttimeout" => 30,
36 "timeout" => 300,
37 )
38 );
39 $this->r->setOptions(
40 array(
41 "timeout" => 600
42 )
43 );
44 $this->assertEquals(
45 array(
46 "connecttimeout" => 30,
47 "timeout" => 600,
48 ),
49 $this->r->getOptions()
50 );
51 }
52
53 function testClone() {
54 $c = clone $this->r;
55 $this->assertNotSame($this->r, $c);
56 }
57
58 function testObserver() {
59 $test = $this;
60 $this->r->attach($o1 = new ProgressObserver1);
61 $this->r->attach($o2 = new ProgressObserver2);
62 $this->r->attach(
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);
73 }
74 )
75 );
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());
85 }
86
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());
90 $this->r->send(null);
91 $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
92 $this->r->enableCookies()->send(null);
93 $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
94 $this->r->send(null);
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());
101 }
102 if ($cookie->getCookie("temp")) {
103 $this->assertEquals(-1, $cookie->getExpires());
104 }
105 }
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());
117 }
118
119 function testResetCookies() {
120 $this->r->setRequest(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"));
121
122 $this->r->enableCookies();
123 $this->r->send(null);
124
125 $f = function ($a) { return $a->getCookies(); };
126 $c = array_map($f, $this->r->getResponseMessage()->getCookies());
127
128 $this->r->send(null);
129 $this->assertEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
130
131 $this->r->resetCookies();
132 $this->r->send(null);
133 $this->assertNotEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
134 }
135
136 function testSsl() {
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));
140 $this->assertEquals(
141 array(
142 "verify_peer" => true,
143 "verify_host" => 2,
144 ),
145 $this->r->getSslOptions()
146 );
147 $this->r->send();
148 $ti = $this->r->getTransferInfo();
149 $this->assertArrayHasKey("ssl_engines", $ti);
150 $this->assertGreaterThan(0, count($ti["ssl_engines"]));
151 }
152
153 function testHistory() {
154 $body = new http\Message\Body;
155 $body->append("foobar");
156
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");
161
162 $this->r->recordHistory = true;
163 $this->r->send($request);
164
165 $this->assertStringMatchesFormat(<<<HTTP
166 POST /ext-http/.print_request.php HTTP/1.1
167 User-Agent: %s
168 Host: dev.iworks.at
169 Accept: */*
170 Content-Length: 6
171 Content-Type: application/x-www-form-urlencoded
172 X-Original-Content-Length: 6
173
174 foobar
175 HTTP/1.1 200 OK
176 Date: %s
177 Server: %s
178 Vary: %s
179 Content-Length: 19
180 Content-Type: text/html
181 X-Original-Content-Length: 19
182
183 string(6) "foobar"
184
185 HTTP
186 , str_replace("\r", "", $this->r->getHistory()->toString(true))
187 );
188
189
190 $this->r->send($request);
191
192 $this->assertStringMatchesFormat(<<<HTTP
193 POST /ext-http/.print_request.php HTTP/1.1
194 User-Agent: %s
195 Host: dev.iworks.at
196 Accept: */*
197 Content-Length: 6
198 Content-Type: application/x-www-form-urlencoded
199 X-Original-Content-Length: 6
200
201 foobar
202 HTTP/1.1 200 OK
203 Date: %s
204 Server: %s
205 Vary: %s
206 Content-Length: 19
207 Content-Type: text/html
208 X-Original-Content-Length: 19
209
210 string(6) "foobar"
211
212 POST /ext-http/.print_request.php HTTP/1.1
213 User-Agent: %s
214 Host: dev.iworks.at
215 Accept: */*
216 Content-Length: 6
217 Content-Type: application/x-www-form-urlencoded
218 X-Original-Content-Length: 6
219
220 foobar
221 HTTP/1.1 200 OK
222 Date: %s
223 Server: %s
224 Vary: %s
225 Content-Length: 19
226 Content-Type: text/html
227 X-Original-Content-Length: 19
228
229 string(6) "foobar"
230
231 HTTP
232 , str_replace("\r", "", $this->r->getHistory()->toString(true))
233 );
234 }
235 }
236