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