xref: /dokuwiki/_test/tests/inc/httpclient_http.test.php (revision 6ed3476b11fe6e9c4625fb28e8953ac26e8160fb)
1<?php
2
3require_once (__DIR__ . '/httpclient_mock.php');
4
5class httpclient_http_test extends DokuWikiTest {
6    protected $server = 'http://httpbin.org';
7
8
9    /**
10     * @group internet
11     */
12    function test_simpleget(){
13        $http = new HTTPMockClient();
14        $data = $http->get($this->server.'/get?foo=bar');
15        if($http->noconnection()) {
16            $this->markTestSkipped('connection timed out');
17            return;
18        }
19        $this->assertFalse($data === false, 'HTTP response '.$http->error);
20        $resp = json_decode($data, true);
21        $this->assertTrue(is_array($resp), 'JSON response');
22        $this->assertArrayHasKey('args',$resp);
23        $this->assertEquals(array('foo'=>'bar'), $resp['args']);
24    }
25
26    /**
27     * @group internet
28     */
29    function test_dget(){
30        $http = new HTTPMockClient();
31        $data = $http->dget($this->server.'/get',array('foo'=>'bar'));
32        if($http->noconnection()) {
33            $this->markTestSkipped('connection timed out');
34            return;
35        }
36        $this->assertFalse($data === false, 'HTTP response '.$http->error);
37        $resp = json_decode($data, true);
38        $this->assertTrue(is_array($resp), 'JSON response');
39        $this->assertArrayHasKey('args',$resp);
40        $this->assertEquals(array('foo'=>'bar'), $resp['args']);
41    }
42
43    /**
44     * @group internet
45     */
46    function test_gzip(){
47        $http = new HTTPMockClient();
48        $data = $http->get($this->server.'/gzip');
49        if($http->noconnection()) {
50            $this->markTestSkipped('connection timed out');
51            return;
52        }
53        $this->assertFalse($data === false, 'HTTP response '.$http->error);
54        $resp = json_decode($data, true);
55        $this->assertTrue(is_array($resp), 'JSON response');
56        $this->assertArrayHasKey('gzipped',$resp);
57        $this->assertTrue($resp['gzipped']);
58    }
59
60    /**
61     * @group internet
62     */
63    function test_simplepost(){
64        $http = new HTTPMockClient();
65        $data = $http->post($this->server.'/post',array('foo'=>'bar'));
66        if($http->noconnection()) {
67            $this->markTestSkipped('connection timed out');
68            return;
69        }
70        $this->assertFalse($data === false, 'HTTP response '.$http->error);
71        $resp = json_decode($data, true);
72        $this->assertTrue(is_array($resp), 'JSON response');
73        $this->assertArrayHasKey('form',$resp);
74        $this->assertEquals(array('foo'=>'bar'), $resp['form']);
75    }
76
77    /**
78     * @group internet
79     */
80    function test_redirect(){
81        $http = new HTTPMockClient();
82        $data = $http->get($this->server.'/redirect/3');
83        if($http->noconnection()) {
84            $this->markTestSkipped('connection timed out');
85            return;
86        }
87        $this->assertFalse($data === false, 'HTTP response '.$http->error);
88        $resp = json_decode($data, true);
89        $this->assertTrue(is_array($resp), 'JSON response');
90        $this->assertArrayHasKey('url',$resp);
91        $this->assertRegExp('/\/get$/', $resp['url']);
92    }
93
94    /**
95     * @group internet
96     */
97    function test_relredirect(){
98        $http = new HTTPMockClient();
99        $data = $http->get($this->server.'/relative-redirect/3');
100        if($http->noconnection()) {
101            $this->markTestSkipped('connection timed out');
102            return;
103        }
104        $this->assertFalse($data === false, 'HTTP response '.$http->error);
105        $resp = json_decode($data, true);
106        $this->assertTrue(is_array($resp), 'JSON response');
107        $this->assertArrayHasKey('url',$resp);
108        $this->assertRegExp('/\/get$/', $resp['url']);
109    }
110
111    /**
112     * @group internet
113     */
114    function test_redirectfail(){
115        $http = new HTTPMockClient();
116        $data = $http->get($this->server.'/redirect/5');
117        if($http->noconnection()) {
118            $this->markTestSkipped('connection timed out');
119            return;
120        }
121        $this->assertTrue($data === false, 'HTTP response '.$http->error);
122        $this->assertEquals('Maximum number of redirects exceeded',$http->error);
123    }
124
125    /**
126     * @group internet
127     */
128    function test_cookies(){
129        $http = new HTTPMockClient();
130        $http->get($this->server.'/cookies/set/foo/bar');
131        if($http->noconnection()) {
132            $this->markTestSkipped('connection timed out');
133            return;
134        }
135        $this->assertEquals(array('foo' => 'bar'), $http->cookies);
136        $data = $http->get($this->server.'/cookies');
137        if($http->noconnection()) {
138            $this->markTestSkipped('connection timed out');
139            return;
140        }
141        $this->assertFalse($data === false, 'HTTP response '.$http->error);
142        $resp = json_decode($data, true);
143        $this->assertTrue(is_array($resp), 'JSON response');
144        $this->assertArrayHasKey('cookies',$resp);
145        $this->assertEquals(array('foo'=>'bar'), $resp['cookies']);
146    }
147
148    /**
149     * @group internet
150     */
151    function test_teapot(){
152        $http = new HTTPMockClient();
153        $data = $http->get($this->server.'/status/418');
154        if($http->noconnection()) {
155            $this->markTestSkipped('connection timed out');
156            return;
157        }
158        $this->assertTrue($data === false, 'HTTP response '.$http->error);
159        $this->assertEquals(418,$http->status);
160    }
161
162    /**
163     * @group internet
164     */
165    function test_maxbody(){
166        $http = new HTTPMockClient();
167        $http->max_bodysize = 250;
168
169        // this should abort completely
170        $data = $http->get($this->server.'/stream/30');
171        if($http->noconnection()) {
172            $this->markTestSkipped('connection timed out');
173            return;
174        }
175        $this->assertTrue($data === false, 'HTTP response '.$http->error);
176
177        // this should read just the needed bytes
178        $http->max_bodysize_abort = false;
179        $http->keep_alive = false;
180        $data = $http->get($this->server.'/stream/30');
181        if($http->noconnection()) {
182            $this->markTestSkipped('connection timed out');
183            return;
184        }
185        $this->assertFalse($data === false, 'HTTP response '.$http->error);
186        /* should read no more than max_bodysize+1 */
187        $this->assertLessThanOrEqual(251,strlen($data));
188    }
189
190    /**
191     * @group internet
192     */
193    function test_maxbodyok(){
194        $http = new HTTPMockClient();
195        $http->max_bodysize = 500*1024;
196        $data = $http->get($this->server.'/stream/5');
197        if($http->noconnection()) {
198            $this->markTestSkipped('connection timed out');
199            return;
200        }
201        $this->assertTrue($data !== false, 'HTTP response '.$http->error);
202        $http->max_bodysize_abort = false;
203        $data = $http->get($this->server.'/stream/5');
204        if($http->noconnection()) {
205            $this->markTestSkipped('connection timed out');
206            return;
207        }
208        $this->assertTrue($data !== false, 'HTTP response '.$http->error);
209    }
210
211    /**
212     * @group internet
213     */
214    function test_basicauth(){
215        $http = new HTTPMockClient();
216        $http->user = 'user';
217        $http->pass = 'pass';
218        $data = $http->get($this->server.'/basic-auth/user/pass');
219        if($http->noconnection()) {
220            $this->markTestSkipped('connection timed out');
221            return;
222        }
223        $this->assertFalse($data === false, 'HTTP response '.$http->error);
224        $resp = json_decode($data, true);
225        $this->assertTrue(is_array($resp), 'JSON response');
226        $this->assertEquals(array('authenticated'=>true,'user'=>'user'), $resp);
227    }
228
229    /**
230     * @group internet
231     */
232    function test_basicauthfail(){
233        $http = new HTTPMockClient();
234        $http->user = 'user';
235        $http->pass = 'invalid';
236        $data = $http->get($this->server.'/basic-auth/user/pass');
237        if($http->noconnection()) {
238            $this->markTestSkipped('connection timed out');
239            return;
240        }
241        $this->assertTrue($data === false, 'HTTP response '.$http->error);
242        $this->assertEquals(401,$http->status);
243    }
244
245    /**
246     * @group internet
247     */
248    function test_timeout(){
249        $http = new HTTPMockClient();
250        $http->timeout = 5;
251        $data = $http->get($this->server.'/delay/10');
252        $this->assertTrue($data === false, 'HTTP response '.$http->error);
253        $this->assertEquals(-100,$http->status);
254    }
255
256    /**
257     * @group internet
258     */
259    function test_headers(){
260        $http = new HTTPMockClient();
261        $data = $http->get($this->server.'/response-headers?baz=&foo=bar');
262        if($http->noconnection()) {
263            $this->markTestSkipped('connection timed out');
264            return;
265        }
266        $this->assertFalse($data === false, 'HTTP response '.$http->error);
267        $resp = json_decode($data, true);
268        $this->assertTrue(is_array($resp), 'JSON response');
269        $this->assertArrayHasKey('baz',$http->resp_headers);
270        $this->assertArrayHasKey('foo',$http->resp_headers);
271        $this->assertEquals('bar',$http->resp_headers['foo']);
272    }
273
274    /**
275     * @group internet
276     */
277    function test_chunked(){
278        $http = new HTTPMockClient();
279        $data = $http->get('http://whoopdedo.org/cgi-bin/chunked/2550');
280        if($http->noconnection()) {
281            $this->markTestSkipped('connection timed out');
282            return;
283        }
284        $this->assertFalse($data === false, 'HTTP response '.$http->error);
285        $this->assertEquals(2550,strlen($data));
286    }
287
288    /**
289     * This address caused trouble with stream_select()
290     *
291     * @group internet
292     */
293    function test_wikimatrix(){
294        $http = new HTTPMockClient();
295        $data = $http->get('http://www.wikimatrix.org/cfeed/dokuwiki/-/-');
296        if($http->noconnection()) {
297            $this->markTestSkipped('connection timed out');
298            return;
299        }
300        $this->assertTrue($data !== false, 'HTTP response '.$http->error);
301    }
302
303    function test_postencode(){
304        $http = new HTTPMockClient();
305
306
307        // check simple data
308        $data = array(
309            'öä?' => 'öä?',
310            'foo' => 'bang'
311        );
312        $this->assertEquals(
313            '%C3%B6%C3%A4%3F=%C3%B6%C3%A4%3F&foo=bang',
314            $http->_postEncode($data),
315            'simple'
316        );
317
318        // check first level numeric array
319        $data = array(
320            'foo' => 'bang',
321            'ärr' => array('ö', 'b', 'c')
322        );
323        $this->assertEquals(
324            'foo=bang&%C3%A4rr%5B0%5D=%C3%B6&%C3%A4rr%5B1%5D=b&%C3%A4rr%5B2%5D=c',
325            $http->_postEncode($data),
326            'onelevelnum'
327        );
328
329        // check first level associative array
330        $data = array(
331            'foo' => 'bang',
332            'ärr' => array('ö'=>'ä', 'b' => 'c')
333        );
334        $this->assertEquals(
335            'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5Bb%5D=c',
336            $http->_postEncode($data),
337            'onelevelassoc'
338        );
339
340
341        // check first level associative array
342        $data = array(
343            'foo' => 'bang',
344            'ärr' => array('ö'=>'ä', 'ä' => array('ö'=>'ä'))
345        );
346        $this->assertEquals(
347            'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5B%C3%A4%5D%5B%C3%B6%5D=%C3%A4',
348            $http->_postEncode($data),
349            'twolevelassoc'
350        );
351    }
352}
353//Setup VIM: ex: et ts=4 :
354