136b29f7433ecfcfb6809223916a027beb75d4d3
[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 $this->assertCount(2, $this->r->getResponseMessage()->getCookies());
97 }
98
99 function testResetCookies() {
100 $this->r->setRequest(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"));
101
102 $this->r->enableCookies();
103 $this->r->send(null);
104
105 $f = function ($a) { return $a->getCookies(); };
106 $c = array_map($f, $this->r->getResponseMessage()->getCookies());
107
108 $this->r->send(null);
109 $this->assertEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
110
111 $this->r->resetCookies();
112 $this->r->send(null);
113 $this->assertNotEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
114 }
115
116 function testSsl() {
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));
120 $this->assertEquals(
121 array(
122 "verify_peer" => true,
123 "verify_host" => 2,
124 ),
125 $this->r->getSslOptions()
126 );
127 $this->r->send();
128 $ti = $this->r->getTransferInfo();
129 $this->assertArrayHasKey("ssl_engines", $ti);
130 $this->assertGreaterThan(0, count($ti["ssl_engines"]));
131 }
132
133 function testHistory() {
134 $body = new http\Message\Body;
135 $body->append("foobar");
136
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");
141
142 $this->r->recordHistory = true;
143 $this->r->send($request);
144
145 $this->assertStringMatchesFormat(<<<HTTP
146 POST /ext-http/.print_request.php HTTP/1.1
147 User-Agent: %s
148 Host: dev.iworks.at
149 Accept: */*
150 Content-Length: 6
151 Content-Type: application/x-www-form-urlencoded
152 X-Original-Content-Length: 6
153
154 foobar
155 HTTP/1.1 200 OK
156 Date: %s
157 Server: %s
158 Vary: %s
159 Content-Length: 19
160 Content-Type: text/html
161 X-Original-Content-Length: 19
162
163 string(6) "foobar"
164
165 HTTP
166 , str_replace("\r", "", $this->r->getHistory()->toString(true))
167 );
168
169
170 $this->r->send($request);
171
172 $this->assertStringMatchesFormat(<<<HTTP
173 POST /ext-http/.print_request.php HTTP/1.1
174 User-Agent: %s
175 Host: dev.iworks.at
176 Accept: */*
177 Content-Length: 6
178 Content-Type: application/x-www-form-urlencoded
179 X-Original-Content-Length: 6
180
181 foobar
182 HTTP/1.1 200 OK
183 Date: %s
184 Server: %s
185 Vary: %s
186 Content-Length: 19
187 Content-Type: text/html
188 X-Original-Content-Length: 19
189
190 string(6) "foobar"
191
192 POST /ext-http/.print_request.php HTTP/1.1
193 User-Agent: %s
194 Host: dev.iworks.at
195 Accept: */*
196 Content-Length: 6
197 Content-Type: application/x-www-form-urlencoded
198 X-Original-Content-Length: 6
199
200 foobar
201 HTTP/1.1 200 OK
202 Date: %s
203 Server: %s
204 Vary: %s
205 Content-Length: 19
206 Content-Type: text/html
207 X-Original-Content-Length: 19
208
209 string(6) "foobar"
210
211 HTTP
212 , str_replace("\r", "", $this->r->getHistory()->toString(true))
213 );
214 }
215 }
216