summaryrefslogtreecommitdiff
path: root/_test/tests/inc/httpclient_http.test.php
blob: 94b8e1bc10c86e4762ed09ac678f5306168bbf5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php

require_once (__DIR__ . '/httpclient_mock.php');

class httpclient_http_test extends DokuWikiTest {
    protected $server = 'http://httpbin.org';


    /**
     * @group internet
     */
    function test_simpleget(){
        $http = new HTTPMockClient();
        $data = $http->get($this->server.'/get?foo=bar');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertFalse($data === false, 'HTTP response '.$http->error);
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('args',$resp);
        $this->assertEquals(array('foo'=>'bar'), $resp['args']);
    }

    /**
     * @group internet
     */
    function test_dget(){
        $http = new HTTPMockClient();
        $data = $http->dget($this->server.'/get',array('foo'=>'bar'));
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertFalse($data === false, 'HTTP response '.$http->error);
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('args',$resp);
        $this->assertEquals(array('foo'=>'bar'), $resp['args']);
    }

    /**
     * @group internet
     */
    function test_gzip(){
        $http = new HTTPMockClient();
        $data = $http->get($this->server.'/gzip');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertFalse($data === false, 'HTTP response '.$http->error);
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('gzipped',$resp);
        $this->assertTrue($resp['gzipped']);
    }

    /**
     * @group internet
     */
    function test_simplepost(){
        $http = new HTTPMockClient();
        $data = $http->post($this->server.'/post',array('foo'=>'bar'));
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertFalse($data === false, 'HTTP response '.$http->error);
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('form',$resp);
        $this->assertEquals(array('foo'=>'bar'), $resp['form']);
    }

    /**
     * @group internet
     */
    function test_redirect(){
        $http = new HTTPMockClient();
        $data = $http->get($this->server.'/redirect/3');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertFalse($data === false, 'HTTP response '.$http->error);
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('url',$resp);
        $this->assertRegExp('/\/get$/', $resp['url']);
    }

    /**
     * @group internet
     */
    function test_relredirect(){
        $http = new HTTPMockClient();
        $data = $http->get($this->server.'/relative-redirect/3');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertFalse($data === false, 'HTTP response '.$http->error);
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('url',$resp);
        $this->assertRegExp('/\/get$/', $resp['url']);
    }

    /**
     * @group internet
     */
    function test_redirectfail(){
        $http = new HTTPMockClient();
        $data = $http->get($this->server.'/redirect/5');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertTrue($data === false, 'HTTP response '.$http->error);
        $this->assertEquals('Maximum number of redirects exceeded',$http->error);
    }

    /**
     * @group internet
     */
    function test_cookies(){
        $http = new HTTPMockClient();
        $http->get($this->server.'/cookies/set/foo/bar');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertEquals(array('foo' => 'bar'), $http->cookies);
        $data = $http->get($this->server.'/cookies');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertFalse($data === false, 'HTTP response '.$http->error);
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('cookies',$resp);
        $this->assertEquals(array('foo'=>'bar'), $resp['cookies']);
    }

    /**
     * @group internet
     */
    function test_teapot(){
        $http = new HTTPMockClient();
        $data = $http->get($this->server.'/status/418');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertTrue($data === false, 'HTTP response '.$http->error);
        $this->assertEquals(418,$http->status);
    }

    /**
     * @group internet
     */
    function test_maxbody(){
        $http = new HTTPMockClient();
        $http->max_bodysize = 250;

        // this should abort completely
        $data = $http->get($this->server.'/stream/30');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertTrue($data === false, 'HTTP response '.$http->error);

        // this should read just the needed bytes
        $http->max_bodysize_abort = false;
        $http->keep_alive = false;
        $data = $http->get($this->server.'/stream/30');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertFalse($data === false, 'HTTP response '.$http->error);
        /* should read no more than max_bodysize+1 */
        $this->assertLessThanOrEqual(251,strlen($data));
    }

    /**
     * @group internet
     */
    function test_maxbodyok(){
        $http = new HTTPMockClient();
        $http->max_bodysize = 500*1024;
        $data = $http->get($this->server.'/stream/5');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertTrue($data !== false, 'HTTP response '.$http->error);
        $http->max_bodysize_abort = false;
        $data = $http->get($this->server.'/stream/5');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertTrue($data !== false, 'HTTP response '.$http->error);
    }

    /**
     * @group internet
     */
    function test_basicauth(){
        $http = new HTTPMockClient();
        $http->user = 'user';
        $http->pass = 'pass';
        $data = $http->get($this->server.'/basic-auth/user/pass');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertFalse($data === false, 'HTTP response '.$http->error);
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertEquals(array('authenticated'=>true,'user'=>'user'), $resp);
    }

    /**
     * @group internet
     */
    function test_basicauthfail(){
        $http = new HTTPMockClient();
        $http->user = 'user';
        $http->pass = 'invalid';
        $data = $http->get($this->server.'/basic-auth/user/pass');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertTrue($data === false, 'HTTP response '.$http->error);
        $this->assertEquals(401,$http->status);
    }

    /**
     * @group internet
     */
    function test_timeout(){
        $http = new HTTPMockClient();
        $http->timeout = 5;
        $data = $http->get($this->server.'/delay/10');
        $this->assertTrue($data === false, 'HTTP response '.$http->error);
        $this->assertEquals(-100,$http->status);
    }

    /**
     * @group internet
     */
    function test_headers(){
        $http = new HTTPMockClient();
        $data = $http->get($this->server.'/response-headers?baz=&foo=bar');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertFalse($data === false, 'HTTP response '.$http->error);
        $resp = json_decode($data, true);
        $this->assertTrue(is_array($resp), 'JSON response');
        $this->assertArrayHasKey('baz',$http->resp_headers);
        $this->assertArrayHasKey('foo',$http->resp_headers);
        $this->assertEquals('bar',$http->resp_headers['foo']);
    }

    /**
     * @group internet
     */
    function test_chunked(){
        $http = new HTTPMockClient();
        $data = $http->get('http://whoopdedo.org/cgi-bin/chunked/2550');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertFalse($data === false, 'HTTP response '.$http->error);
        $this->assertEquals(2550,strlen($data));
    }

    /**
     * This address caused trouble with stream_select()
     *
     * @group internet
     * @group flaky
     */
    function test_wikimatrix(){
        $http = new HTTPMockClient();
        $data = $http->get('http://www.wikimatrix.org/cfeed/dokuwiki/-/-');
        if($http->noconnection()) {
            $this->markTestSkipped('connection timed out');
            return;
        }
        $this->assertTrue($data !== false, 'HTTP response '.$http->error);
    }

    function test_postencode(){
        $http = new HTTPMockClient();


        // check simple data
        $data = array(
            'öä?' => 'öä?',
            'foo' => 'bang'
        );
        $this->assertEquals(
            '%C3%B6%C3%A4%3F=%C3%B6%C3%A4%3F&foo=bang',
            $http->_postEncode($data),
            'simple'
        );

        // check first level numeric array
        $data = array(
            'foo' => 'bang',
            'ärr' => array('ö', 'b', 'c')
        );
        $this->assertEquals(
            'foo=bang&%C3%A4rr%5B0%5D=%C3%B6&%C3%A4rr%5B1%5D=b&%C3%A4rr%5B2%5D=c',
            $http->_postEncode($data),
            'onelevelnum'
        );

        // check first level associative array
        $data = array(
            'foo' => 'bang',
            'ärr' => array('ö'=>'ä', 'b' => 'c')
        );
        $this->assertEquals(
            'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5Bb%5D=c',
            $http->_postEncode($data),
            'onelevelassoc'
        );


        // check first level associative array
        $data = array(
            'foo' => 'bang',
            'ärr' => array('ö'=>'ä', 'ä' => array('ö'=>'ä'))
        );
        $this->assertEquals(
            'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5B%C3%A4%5D%5B%C3%B6%5D=%C3%A4',
            $http->_postEncode($data),
            'twolevelassoc'
        );
    }
}
//Setup VIM: ex: et ts=4 :