80%+ test coverage
[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 $this->r = (new http\Client\Factory)->createClient();
32 $this->r->setOptions(
33 array(
34 "connecttimeout" => 30,
35 "timeout" => 300,
36 )
37 );
38 $this->r->setOptions(
39 array(
40 "timeout" => 600
41 )
42 );
43 $this->assertEquals(
44 array(
45 "connecttimeout" => 30,
46 "timeout" => 600,
47 ),
48 $this->r->getOptions()
49 );
50 }
51
52 function testClone() {
53 $c = clone $this->r;
54 $this->assertNotSame($this->r, $c);
55 }
56
57 function testObserver() {
58 $this->r->attach($o1 = new ProgressObserver1);
59 $this->r->attach($o2 = new ProgressObserver2);
60 $this->r->attach(
61 $o3 = new CallbackObserver(
62 function ($r) {
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);
71 }
72 )
73 );
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());
83 }
84
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());
88 $this->r->send(null);
89 $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
90 $this->r->enableCookies()->send(null);
91 $this->assertNotContains("Cookie", (string) $this->r->getRequestMessage());
92 $this->r->send(null);
93 $this->assertContains("Cookie", (string) $this->r->getRequestMessage());
94 $this->assertCount(2, $this->r->getResponseMessage()->getCookies());
95 }
96
97 function testResetCookies() {
98 $this->r->setRequest(new http\Client\Request("GET", "http://dev.iworks.at/ext-http/.cookie.php"));
99
100 $this->r->enableCookies();
101 $this->r->send(null);
102
103 $f = function ($a) { return $a->getCookies(); };
104 $c = array_map($f, $this->r->getResponseMessage()->getCookies());
105
106 $this->r->send(null);
107 $this->assertEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
108
109 $this->r->resetCookies();
110 $this->r->send(null);
111 $this->assertNotEquals($c, array_map($f, $this->r->getResponseMessage()->getCookies()));
112 }
113
114 function testSsl() {
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));
118 $this->assertEquals(
119 array(
120 "verify_peer" => true,
121 "verify_host" => 2,
122 ),
123 $this->r->getSslOptions()
124 );
125 $this->r->send();
126 $ti = $this->r->getTransferInfo();
127 $this->assertArrayHasKey("ssl_engines", $ti);
128 $this->assertGreaterThan(0, count($ti["ssl_engines"]));
129 }
130
131 function testHistory() {
132 $body = new http\Message\Body;
133 $body->append("foobar");
134
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");
139
140 $this->r->recordHistory = true;
141 $this->r->send($request);
142
143 $this->assertStringMatchesFormat(<<<HTTP
144 POST /ext-http/.print_request.php HTTP/1.1
145 User-Agent: %s
146 Host: dev.iworks.at
147 Accept: */*
148 Content-Length: 6
149 Content-Type: application/x-www-form-urlencoded
150 X-Original-Content-Length: 6
151
152 foobar
153 HTTP/1.1 200 OK
154 Date: %s
155 Server: %s
156 Vary: %s
157 Content-Length: 19
158 Content-Type: text/html
159 X-Original-Content-Length: 19
160
161 string(6) "foobar"
162
163 HTTP
164 , str_replace("\r", "", $this->r->getHistory()->toString(true))
165 );
166
167
168 $this->r->send($request);
169
170 $this->assertStringMatchesFormat(<<<HTTP
171 POST /ext-http/.print_request.php HTTP/1.1
172 User-Agent: %s
173 Host: dev.iworks.at
174 Accept: */*
175 Content-Length: 6
176 Content-Type: application/x-www-form-urlencoded
177 X-Original-Content-Length: 6
178
179 foobar
180 HTTP/1.1 200 OK
181 Date: %s
182 Server: %s
183 Vary: %s
184 Content-Length: 19
185 Content-Type: text/html
186 X-Original-Content-Length: 19
187
188 string(6) "foobar"
189
190 POST /ext-http/.print_request.php HTTP/1.1
191 User-Agent: %s
192 Host: dev.iworks.at
193 Accept: */*
194 Content-Length: 6
195 Content-Type: application/x-www-form-urlencoded
196 X-Original-Content-Length: 6
197
198 foobar
199 HTTP/1.1 200 OK
200 Date: %s
201 Server: %s
202 Vary: %s
203 Content-Length: 19
204 Content-Type: text/html
205 X-Original-Content-Length: 19
206
207 string(6) "foobar"
208
209 HTTP
210 , str_replace("\r", "", $this->r->getHistory()->toString(true))
211 );
212 }
213 }
214