xref: /dokuwiki/_test/tests/inc/httpclient_http.test.php (revision 5b230a45afb9bb825ba148e699bdcbfc57957fb4)
1f8369d7dSTobias Sarnowski<?php
2f8369d7dSTobias Sarnowski
3f8369d7dSTobias Sarnowskiclass httpclient_http_test extends DokuWikiTest {
4f8369d7dSTobias Sarnowski    protected $server = 'http://httpbin.org';
5f8369d7dSTobias Sarnowski
6f7161c34SAndreas Gohr    /**
7f7161c34SAndreas Gohr     * @group internet
8f7161c34SAndreas Gohr     */
9f8369d7dSTobias Sarnowski    function test_simpleget(){
10f8369d7dSTobias Sarnowski        $http = new HTTPClient();
11f8369d7dSTobias Sarnowski        $data = $http->get($this->server.'/get?foo=bar');
12f8369d7dSTobias Sarnowski        $this->assertFalse($data === false, 'HTTP response');
13f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
14f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
15f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('args',$resp);
16f8369d7dSTobias Sarnowski        $this->assertEquals(array('foo'=>'bar'), $resp['args']);
17f8369d7dSTobias Sarnowski    }
18f8369d7dSTobias Sarnowski
19f7161c34SAndreas Gohr    /**
20f7161c34SAndreas Gohr     * @group internet
21f7161c34SAndreas Gohr     */
22f8369d7dSTobias Sarnowski    function test_dget(){
23f8369d7dSTobias Sarnowski        $http = new HTTPClient();
24f8369d7dSTobias Sarnowski        $data = $http->dget($this->server.'/get',array('foo'=>'bar'));
25f8369d7dSTobias Sarnowski        $this->assertFalse($data === false, 'HTTP response');
26f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
27f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
28f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('args',$resp);
29f8369d7dSTobias Sarnowski        $this->assertEquals(array('foo'=>'bar'), $resp['args']);
30f8369d7dSTobias Sarnowski    }
31f8369d7dSTobias Sarnowski
32f7161c34SAndreas Gohr    /**
33f7161c34SAndreas Gohr     * @group internet
34f7161c34SAndreas Gohr     */
35f8369d7dSTobias Sarnowski    function test_gzip(){
36f8369d7dSTobias Sarnowski        $http = new HTTPClient();
37f8369d7dSTobias Sarnowski        $data = $http->get($this->server.'/gzip');
38f8369d7dSTobias Sarnowski        $this->assertFalse($data === false, 'HTTP response');
39f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
40f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
41f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('gzipped',$resp);
42f8369d7dSTobias Sarnowski        $this->assertTrue($resp['gzipped']);
43f8369d7dSTobias Sarnowski    }
44f8369d7dSTobias Sarnowski
45f7161c34SAndreas Gohr    /**
46f7161c34SAndreas Gohr     * @group internet
47f7161c34SAndreas Gohr     */
48f8369d7dSTobias Sarnowski    function test_simplepost(){
49f8369d7dSTobias Sarnowski        $http = new HTTPClient();
50f8369d7dSTobias Sarnowski        $data = $http->post($this->server.'/post',array('foo'=>'bar'));
51f8369d7dSTobias Sarnowski        $this->assertFalse($data === false, 'HTTP response');
52f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
53f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
54f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('form',$resp);
55f8369d7dSTobias Sarnowski        $this->assertEquals(array('foo'=>'bar'), $resp['form']);
56f8369d7dSTobias Sarnowski    }
57f8369d7dSTobias Sarnowski
58f7161c34SAndreas Gohr    /**
59f7161c34SAndreas Gohr     * @group internet
60f7161c34SAndreas Gohr     */
61f8369d7dSTobias Sarnowski    function test_redirect(){
62f8369d7dSTobias Sarnowski        $http = new HTTPClient();
63f8369d7dSTobias Sarnowski        $data = $http->get($this->server.'/redirect/3');
64f8369d7dSTobias Sarnowski        $this->assertFalse($data === false, 'HTTP response');
65f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
66f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
67f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('url',$resp);
68f8369d7dSTobias Sarnowski        $this->assertRegExp('/\/get$/', $resp['url']);
69f8369d7dSTobias Sarnowski    }
70f8369d7dSTobias Sarnowski
71f7161c34SAndreas Gohr    /**
72f7161c34SAndreas Gohr     * @group internet
73f7161c34SAndreas Gohr     */
74f8369d7dSTobias Sarnowski    function test_relredirect(){
75f8369d7dSTobias Sarnowski        $http = new HTTPClient();
76f8369d7dSTobias Sarnowski        $data = $http->get($this->server.'/relative-redirect/3');
77f8369d7dSTobias Sarnowski        $this->assertFalse($data === false, 'HTTP response');
78f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
79f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
80f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('url',$resp);
81f8369d7dSTobias Sarnowski        $this->assertRegExp('/\/get$/', $resp['url']);
82f8369d7dSTobias Sarnowski    }
83f8369d7dSTobias Sarnowski
84f7161c34SAndreas Gohr    /**
85f7161c34SAndreas Gohr     * @group internet
86f7161c34SAndreas Gohr     */
87f8369d7dSTobias Sarnowski    function test_redirectfail(){
88f8369d7dSTobias Sarnowski        $http = new HTTPClient();
89f8369d7dSTobias Sarnowski        $data = $http->get($this->server.'/redirect/5');
90f8369d7dSTobias Sarnowski        $this->assertTrue($data === false, 'HTTP response');
91f8369d7dSTobias Sarnowski        $this->assertEquals('Maximum number of redirects exceeded',$http->error);
92f8369d7dSTobias Sarnowski    }
93f8369d7dSTobias Sarnowski
94f7161c34SAndreas Gohr    /**
95f7161c34SAndreas Gohr     * @group internet
96f7161c34SAndreas Gohr     */
97f8369d7dSTobias Sarnowski    function test_cookies(){
98f8369d7dSTobias Sarnowski        $http = new HTTPClient();
99f8369d7dSTobias Sarnowski        $http->get($this->server.'/cookies/set/foo/bar');
100f8369d7dSTobias Sarnowski        $this->assertEquals(array('foo' => 'bar'), $http->cookies);
101f8369d7dSTobias Sarnowski        $data = $http->get($this->server.'/cookies');
102f8369d7dSTobias Sarnowski        $this->assertFalse($data === false, 'HTTP response');
103f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
104f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
105f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('cookies',$resp);
106f8369d7dSTobias Sarnowski        $this->assertEquals(array('foo'=>'bar'), $resp['cookies']);
107f8369d7dSTobias Sarnowski    }
108f8369d7dSTobias Sarnowski
109f7161c34SAndreas Gohr    /**
110f7161c34SAndreas Gohr     * @group internet
111f7161c34SAndreas Gohr     */
112f8369d7dSTobias Sarnowski    function test_teapot(){
113f8369d7dSTobias Sarnowski        $http = new HTTPClient();
114f8369d7dSTobias Sarnowski        $data = $http->get($this->server.'/status/418');
115f8369d7dSTobias Sarnowski        $this->assertTrue($data === false, 'HTTP response');
116f8369d7dSTobias Sarnowski        $this->assertEquals(418,$http->status);
117f8369d7dSTobias Sarnowski    }
118f8369d7dSTobias Sarnowski
119f7161c34SAndreas Gohr    /**
120f7161c34SAndreas Gohr     * @group internet
121f7161c34SAndreas Gohr     */
122f8369d7dSTobias Sarnowski    function test_maxbody(){
123f8369d7dSTobias Sarnowski        $http = new HTTPClient();
124f8369d7dSTobias Sarnowski        $http->max_bodysize = 250;
125*5b230a45SAndreas Gohr
126*5b230a45SAndreas Gohr        // this should abort completely
127f8369d7dSTobias Sarnowski        $data = $http->get($this->server.'/stream/30');
128f8369d7dSTobias Sarnowski        $this->assertTrue($data === false, 'HTTP response');
129*5b230a45SAndreas Gohr
130*5b230a45SAndreas Gohr        // this should read just the needed bytes
131769b429aSTom N Harris        $http->max_bodysize_abort = false;
132*5b230a45SAndreas Gohr        $http->keep_alive = false;
133769b429aSTom N Harris        $data = $http->get($this->server.'/stream/30');
134769b429aSTom N Harris        $this->assertFalse($data === false, 'HTTP response');
13508118f51STom N Harris        /* should read no more than max_bodysize+1 */
13608118f51STom N Harris        $this->assertLessThanOrEqual(251,strlen($data));
137f8369d7dSTobias Sarnowski    }
138f8369d7dSTobias Sarnowski
139f7161c34SAndreas Gohr    /**
140f7161c34SAndreas Gohr     * @group internet
141f7161c34SAndreas Gohr     */
14247a8b919SAndreas Gohr    function test_maxbodyok(){
14347a8b919SAndreas Gohr        $http = new HTTPClient();
14447a8b919SAndreas Gohr        $http->max_bodysize = 500*1024;
14547a8b919SAndreas Gohr        $data = $http->get($this->server.'/stream/5');
14647a8b919SAndreas Gohr        $this->assertTrue($data !== false, 'HTTP response');
14747a8b919SAndreas Gohr        $http->max_bodysize_abort = false;
14847a8b919SAndreas Gohr        $data = $http->get($this->server.'/stream/5');
14947a8b919SAndreas Gohr        $this->assertTrue($data !== false, 'HTTP response');
15047a8b919SAndreas Gohr    }
15147a8b919SAndreas Gohr
15247a8b919SAndreas Gohr    /**
15347a8b919SAndreas Gohr     * @group internet
15447a8b919SAndreas Gohr     */
155f8369d7dSTobias Sarnowski    function test_basicauth(){
156f8369d7dSTobias Sarnowski        $http = new HTTPClient();
157f8369d7dSTobias Sarnowski        $http->user = 'user';
158f8369d7dSTobias Sarnowski        $http->pass = 'pass';
159f8369d7dSTobias Sarnowski        $data = $http->get($this->server.'/basic-auth/user/pass');
160f8369d7dSTobias Sarnowski        $this->assertFalse($data === false, 'HTTP response');
161f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
162f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
163f8369d7dSTobias Sarnowski        $this->assertEquals(array('authenticated'=>true,'user'=>'user'), $resp);
164f8369d7dSTobias Sarnowski    }
165f8369d7dSTobias Sarnowski
166f7161c34SAndreas Gohr    /**
167f7161c34SAndreas Gohr     * @group internet
168f7161c34SAndreas Gohr     */
169f8369d7dSTobias Sarnowski    function test_basicauthfail(){
170f8369d7dSTobias Sarnowski        $http = new HTTPClient();
171f8369d7dSTobias Sarnowski        $http->user = 'user';
172f8369d7dSTobias Sarnowski        $http->pass = 'invalid';
173f8369d7dSTobias Sarnowski        $data = $http->get($this->server.'/basic-auth/user/pass');
174f8369d7dSTobias Sarnowski        $this->assertTrue($data === false, 'HTTP response');
175f8369d7dSTobias Sarnowski        $this->assertEquals(401,$http->status);
176f8369d7dSTobias Sarnowski    }
177f8369d7dSTobias Sarnowski
178f7161c34SAndreas Gohr    /**
179f7161c34SAndreas Gohr     * @group internet
180f7161c34SAndreas Gohr     */
181f8369d7dSTobias Sarnowski    function test_timeout(){
182f8369d7dSTobias Sarnowski        $http = new HTTPClient();
183f8369d7dSTobias Sarnowski        $http->timeout = 5;
184f8369d7dSTobias Sarnowski        $data = $http->get($this->server.'/delay/10');
185f8369d7dSTobias Sarnowski        $this->assertTrue($data === false, 'HTTP response');
186f8369d7dSTobias Sarnowski        $this->assertEquals(-100,$http->status);
187f8369d7dSTobias Sarnowski    }
188f8369d7dSTobias Sarnowski
189f7161c34SAndreas Gohr    /**
190f7161c34SAndreas Gohr     * @group internet
191f7161c34SAndreas Gohr     */
192f8369d7dSTobias Sarnowski    function test_headers(){
193f8369d7dSTobias Sarnowski        $http = new HTTPClient();
194f8369d7dSTobias Sarnowski        $data = $http->get($this->server.'/response-headers?baz=&foo=bar');
195f8369d7dSTobias Sarnowski        $this->assertFalse($data === false, 'HTTP response');
196f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
197f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
198f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('baz',$http->resp_headers);
199f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('foo',$http->resp_headers);
200f8369d7dSTobias Sarnowski        $this->assertEquals('bar',$http->resp_headers['foo']);
201f8369d7dSTobias Sarnowski    }
202d37a3955STom N Harris
203d37a3955STom N Harris    /**
204d37a3955STom N Harris     * @group internet
205d37a3955STom N Harris     */
206d37a3955STom N Harris    function test_chunked(){
207d37a3955STom N Harris        $http = new HTTPClient();
208d37a3955STom N Harris        $data = $http->get('http://whoopdedo.org/cgi-bin/chunked/2550');
209d37a3955STom N Harris        $this->assertFalse($data === false, 'HTTP response');
210d37a3955STom N Harris        $this->assertEquals(2550,strlen($data));
211d37a3955STom N Harris    }
21233b8a947SAndreas Gohr
21333b8a947SAndreas Gohr    /**
21433b8a947SAndreas Gohr     * This address caused trouble with stream_select()
21533b8a947SAndreas Gohr     *
21633b8a947SAndreas Gohr     * @group internet
21733b8a947SAndreas Gohr     */
21833b8a947SAndreas Gohr    function test_wikimatrix(){
21933b8a947SAndreas Gohr        $http = new HTTPClient();
22033b8a947SAndreas Gohr        $data = $http->get('http://www.wikimatrix.org/cfeed/dokuwiki/-/-');
22133b8a947SAndreas Gohr        $this->assertTrue($data !== false, $http->error);
22233b8a947SAndreas Gohr    }
2234d4b1f8cSAndreas Gohr
2244d4b1f8cSAndreas Gohr    function test_postencode(){
2254d4b1f8cSAndreas Gohr        $http = new HTTPClient();
2264d4b1f8cSAndreas Gohr
2274d4b1f8cSAndreas Gohr
2284d4b1f8cSAndreas Gohr        // check simple data
2294d4b1f8cSAndreas Gohr        $data = array(
2304d4b1f8cSAndreas Gohr            'öä?' => 'öä?',
2314d4b1f8cSAndreas Gohr            'foo' => 'bang'
2324d4b1f8cSAndreas Gohr        );
2334d4b1f8cSAndreas Gohr        $this->assertEquals(
2344d4b1f8cSAndreas Gohr            '%C3%B6%C3%A4%3F=%C3%B6%C3%A4%3F&foo=bang',
2354d4b1f8cSAndreas Gohr            $http->_postEncode($data),
2364d4b1f8cSAndreas Gohr            'simple'
2374d4b1f8cSAndreas Gohr        );
2384d4b1f8cSAndreas Gohr
2394d4b1f8cSAndreas Gohr        // check first level numeric array
2404d4b1f8cSAndreas Gohr        $data = array(
2414d4b1f8cSAndreas Gohr            'foo' => 'bang',
2424d4b1f8cSAndreas Gohr            'ärr' => array('ö', 'b', 'c')
2434d4b1f8cSAndreas Gohr        );
2444d4b1f8cSAndreas Gohr        $this->assertEquals(
2454d4b1f8cSAndreas Gohr            'foo=bang&%C3%A4rr%5B0%5D=%C3%B6&%C3%A4rr%5B1%5D=b&%C3%A4rr%5B2%5D=c',
2464d4b1f8cSAndreas Gohr            $http->_postEncode($data),
2474d4b1f8cSAndreas Gohr            'onelevelnum'
2484d4b1f8cSAndreas Gohr        );
2494d4b1f8cSAndreas Gohr
2504d4b1f8cSAndreas Gohr        // check first level associative array
2514d4b1f8cSAndreas Gohr        $data = array(
2524d4b1f8cSAndreas Gohr            'foo' => 'bang',
2534d4b1f8cSAndreas Gohr            'ärr' => array('ö'=>'ä', 'b' => 'c')
2544d4b1f8cSAndreas Gohr        );
2554d4b1f8cSAndreas Gohr        $this->assertEquals(
2564d4b1f8cSAndreas Gohr            'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5Bb%5D=c',
2574d4b1f8cSAndreas Gohr            $http->_postEncode($data),
2584d4b1f8cSAndreas Gohr            'onelevelassoc'
2594d4b1f8cSAndreas Gohr        );
2604d4b1f8cSAndreas Gohr
2614d4b1f8cSAndreas Gohr
2624d4b1f8cSAndreas Gohr        // check first level associative array
2634d4b1f8cSAndreas Gohr        $data = array(
2644d4b1f8cSAndreas Gohr            'foo' => 'bang',
2654d4b1f8cSAndreas Gohr            'ärr' => array('ö'=>'ä', 'ä' => array('ö'=>'ä'))
2664d4b1f8cSAndreas Gohr        );
2674d4b1f8cSAndreas Gohr        $this->assertEquals(
2684d4b1f8cSAndreas Gohr            'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5B%C3%A4%5D%5B%C3%B6%5D=%C3%A4',
2694d4b1f8cSAndreas Gohr            $http->_postEncode($data),
2704d4b1f8cSAndreas Gohr            'twolevelassoc'
2714d4b1f8cSAndreas Gohr        );
2724d4b1f8cSAndreas Gohr    }
273f8369d7dSTobias Sarnowski}
274f8369d7dSTobias Sarnowski//Setup VIM: ex: et ts=4 :
275