xref: /dokuwiki/_test/tests/inc/httpclient_http.test.php (revision 9ad2b9131dcbe90c5d256acd8f9f347b14d958fb)
1f8369d7dSTobias Sarnowski<?php
2f8369d7dSTobias Sarnowski
395e6ded1SAndreas Gohrrequire_once(__DIR__ . '/httpclient_mock.php');
495e6ded1SAndreas Gohr
5d9a7912aSAndreas Gohr/**
6d9a7912aSAndreas Gohr * Tests are executed against an instance of go-httpbin
7d9a7912aSAndreas Gohr */
8d9a7912aSAndreas Gohrclass httpclient_http_test extends DokuWikiTest
9d9a7912aSAndreas Gohr{
10d9a7912aSAndreas Gohr    protected $server = 'http://httpbingo.org';
1195e6ded1SAndreas Gohr
12f7161c34SAndreas Gohr    /**
13f7161c34SAndreas Gohr     * @group internet
14f7161c34SAndreas Gohr     */
15d9a7912aSAndreas Gohr    public function test_simpleget()
16d9a7912aSAndreas Gohr    {
1795e6ded1SAndreas Gohr        $http = new HTTPMockClient();
18f8369d7dSTobias Sarnowski        $data = $http->get($this->server . '/get?foo=bar');
1995e6ded1SAndreas Gohr        if ($http->noconnection()) {
2095e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
2195e6ded1SAndreas Gohr        }
226aad717eSAndreas Gohr        $this->assertFalse($data === false, $http->errorInfo());
23f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
24f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
25f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('args', $resp);
26d9a7912aSAndreas Gohr        $this->assertEquals(['foo' => ['bar']], $resp['args']);
27f8369d7dSTobias Sarnowski    }
28f8369d7dSTobias Sarnowski
29f7161c34SAndreas Gohr    /**
30f7161c34SAndreas Gohr     * @group internet
31f7161c34SAndreas Gohr     */
32d9a7912aSAndreas Gohr    public function test_dget()
33d9a7912aSAndreas Gohr    {
3495e6ded1SAndreas Gohr        $http = new HTTPMockClient();
35d9a7912aSAndreas Gohr        $data = $http->dget($this->server . '/get', ['foo' => 'bar']);
3695e6ded1SAndreas Gohr        if ($http->noconnection()) {
3795e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
3895e6ded1SAndreas Gohr        }
396aad717eSAndreas Gohr        $this->assertFalse($data === false, $http->errorInfo());
40f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
41f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
42f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('args', $resp);
43d9a7912aSAndreas Gohr        $this->assertEquals(['foo' => ['bar']], $resp['args']);
44f8369d7dSTobias Sarnowski    }
45f8369d7dSTobias Sarnowski
46f7161c34SAndreas Gohr    /**
47f7161c34SAndreas Gohr     * @group internet
48f7161c34SAndreas Gohr     */
49d9a7912aSAndreas Gohr    public function test_gzip()
50d9a7912aSAndreas Gohr    {
5195e6ded1SAndreas Gohr        $http = new HTTPMockClient();
52f8369d7dSTobias Sarnowski        $data = $http->get($this->server . '/gzip');
5395e6ded1SAndreas Gohr        if ($http->noconnection()) {
5495e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
5595e6ded1SAndreas Gohr        }
566aad717eSAndreas Gohr        $this->assertFalse($data === false, $http->errorInfo());
57f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
58f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
59f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('gzipped', $resp);
60f8369d7dSTobias Sarnowski        $this->assertTrue($resp['gzipped']);
61f8369d7dSTobias Sarnowski    }
62f8369d7dSTobias Sarnowski
63f7161c34SAndreas Gohr    /**
64f7161c34SAndreas Gohr     * @group internet
65f7161c34SAndreas Gohr     */
66d9a7912aSAndreas Gohr    public function test_simplepost()
67d9a7912aSAndreas Gohr    {
6895e6ded1SAndreas Gohr        $http = new HTTPMockClient();
69d9a7912aSAndreas Gohr        $data = $http->post($this->server . '/post', ['foo' => 'bar']);
7095e6ded1SAndreas Gohr        if ($http->noconnection()) {
7195e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
7295e6ded1SAndreas Gohr        }
736aad717eSAndreas Gohr        $this->assertFalse($data === false, $http->errorInfo());
74f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
75f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
76f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('form', $resp);
77d9a7912aSAndreas Gohr        $this->assertEquals(['foo' => ['bar']], $resp['form']);
78f8369d7dSTobias Sarnowski    }
79f8369d7dSTobias Sarnowski
80f7161c34SAndreas Gohr    /**
81f7161c34SAndreas Gohr     * @group internet
82f7161c34SAndreas Gohr     */
83d9a7912aSAndreas Gohr    public function test_redirect()
84d9a7912aSAndreas Gohr    {
8595e6ded1SAndreas Gohr        $http = new HTTPMockClient();
86f8369d7dSTobias Sarnowski        $data = $http->get($this->server . '/redirect/3');
8795e6ded1SAndreas Gohr        if ($http->noconnection()) {
8895e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
8995e6ded1SAndreas Gohr        }
906aad717eSAndreas Gohr        $this->assertFalse($data === false, $http->errorInfo());
91f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
92f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
93f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('url', $resp);
94*9ad2b913SAndreas Gohr        $this->assertMatchesRegularExpression('/\/get$/', $resp['url']);
95f8369d7dSTobias Sarnowski    }
96f8369d7dSTobias Sarnowski
97f7161c34SAndreas Gohr    /**
98f7161c34SAndreas Gohr     * @group internet
99f7161c34SAndreas Gohr     */
100d9a7912aSAndreas Gohr    public function test_relredirect()
101d9a7912aSAndreas Gohr    {
10295e6ded1SAndreas Gohr        $http = new HTTPMockClient();
103f8369d7dSTobias Sarnowski        $data = $http->get($this->server . '/relative-redirect/3');
10495e6ded1SAndreas Gohr        if ($http->noconnection()) {
10595e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
10695e6ded1SAndreas Gohr        }
1076aad717eSAndreas Gohr        $this->assertFalse($data === false, $http->errorInfo());
108f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
109f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
110f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('url', $resp);
111*9ad2b913SAndreas Gohr        $this->assertMatchesRegularExpression('/\/get$/', $resp['url']);
112f8369d7dSTobias Sarnowski    }
113f8369d7dSTobias Sarnowski
114f7161c34SAndreas Gohr    /**
115f7161c34SAndreas Gohr     * @group internet
116f7161c34SAndreas Gohr     */
117d9a7912aSAndreas Gohr    public function test_redirectfail()
118d9a7912aSAndreas Gohr    {
11995e6ded1SAndreas Gohr        $http = new HTTPMockClient();
120f8369d7dSTobias Sarnowski        $data = $http->get($this->server . '/redirect/5');
12195e6ded1SAndreas Gohr        if ($http->noconnection()) {
12295e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
12395e6ded1SAndreas Gohr        }
1246aad717eSAndreas Gohr        $this->assertTrue($data === false, $http->errorInfo());
125f8369d7dSTobias Sarnowski        $this->assertEquals('Maximum number of redirects exceeded', $http->error);
126f8369d7dSTobias Sarnowski    }
127f8369d7dSTobias Sarnowski
128f7161c34SAndreas Gohr    /**
129f7161c34SAndreas Gohr     * @group internet
130f7161c34SAndreas Gohr     */
131d9a7912aSAndreas Gohr    public function test_cookies()
132d9a7912aSAndreas Gohr    {
13395e6ded1SAndreas Gohr        $http = new HTTPMockClient();
134d9a7912aSAndreas Gohr        $http->get($this->server . '/cookies/set?foo=bar');
13595e6ded1SAndreas Gohr        if ($http->noconnection()) {
13695e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
13795e6ded1SAndreas Gohr        }
138d9a7912aSAndreas Gohr        $this->assertEquals(['foo' => 'bar'], $http->cookies);
139f8369d7dSTobias Sarnowski        $data = $http->get($this->server . '/cookies');
14095e6ded1SAndreas Gohr        if ($http->noconnection()) {
14195e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
14295e6ded1SAndreas Gohr        }
1436aad717eSAndreas Gohr        $this->assertFalse($data === false, $http->errorInfo());
144f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
145f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
146d9a7912aSAndreas Gohr        $this->assertEquals(['foo' => 'bar'], $resp);
147f8369d7dSTobias Sarnowski    }
148f8369d7dSTobias Sarnowski
149f7161c34SAndreas Gohr    /**
150f7161c34SAndreas Gohr     * @group internet
151f7161c34SAndreas Gohr     */
152d9a7912aSAndreas Gohr    public function test_teapot()
153d9a7912aSAndreas Gohr    {
15495e6ded1SAndreas Gohr        $http = new HTTPMockClient();
155f8369d7dSTobias Sarnowski        $data = $http->get($this->server . '/status/418');
15695e6ded1SAndreas Gohr        if ($http->noconnection()) {
15795e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
15895e6ded1SAndreas Gohr        }
1596aad717eSAndreas Gohr        $this->assertTrue($data === false, $http->errorInfo());
160f8369d7dSTobias Sarnowski        $this->assertEquals(418, $http->status);
161f8369d7dSTobias Sarnowski    }
162f8369d7dSTobias Sarnowski
163f7161c34SAndreas Gohr    /**
164f7161c34SAndreas Gohr     * @group internet
165f7161c34SAndreas Gohr     */
166d9a7912aSAndreas Gohr    public function test_maxbody()
167d9a7912aSAndreas Gohr    {
16895e6ded1SAndreas Gohr        $http = new HTTPMockClient();
169f8369d7dSTobias Sarnowski        $http->max_bodysize = 250;
1705b230a45SAndreas Gohr
1715b230a45SAndreas Gohr        // this should abort completely
172f8369d7dSTobias Sarnowski        $data = $http->get($this->server . '/stream/30');
17395e6ded1SAndreas Gohr        if ($http->noconnection()) {
17495e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
17595e6ded1SAndreas Gohr        }
1766aad717eSAndreas Gohr        $this->assertTrue($data === false, $http->errorInfo());
1775b230a45SAndreas Gohr
1785b230a45SAndreas Gohr        // this should read just the needed bytes
179769b429aSTom N Harris        $http->max_bodysize_abort = false;
1805b230a45SAndreas Gohr        $http->keep_alive = false;
181769b429aSTom N Harris        $data = $http->get($this->server . '/stream/30');
18295e6ded1SAndreas Gohr        if ($http->noconnection()) {
18395e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
18495e6ded1SAndreas Gohr        }
1856aad717eSAndreas Gohr        $this->assertFalse($data === false, $http->errorInfo());
18608118f51STom N Harris        /* should read no more than max_bodysize+1 */
18708118f51STom N Harris        $this->assertLessThanOrEqual(251, strlen($data));
188f8369d7dSTobias Sarnowski    }
189f8369d7dSTobias Sarnowski
190f7161c34SAndreas Gohr    /**
191f7161c34SAndreas Gohr     * @group internet
192f7161c34SAndreas Gohr     */
193d9a7912aSAndreas Gohr    public function test_maxbodyok()
194d9a7912aSAndreas Gohr    {
19595e6ded1SAndreas Gohr        $http = new HTTPMockClient();
19647a8b919SAndreas Gohr        $http->max_bodysize = 500 * 1024;
19747a8b919SAndreas Gohr        $data = $http->get($this->server . '/stream/5');
19895e6ded1SAndreas Gohr        if ($http->noconnection()) {
19995e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
20095e6ded1SAndreas Gohr        }
2016aad717eSAndreas Gohr        $this->assertTrue($data !== false, $http->errorInfo());
20247a8b919SAndreas Gohr        $http->max_bodysize_abort = false;
20347a8b919SAndreas Gohr        $data = $http->get($this->server . '/stream/5');
20495e6ded1SAndreas Gohr        if ($http->noconnection()) {
20595e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
20695e6ded1SAndreas Gohr        }
2076aad717eSAndreas Gohr        $this->assertTrue($data !== false, $http->errorInfo());
20847a8b919SAndreas Gohr    }
20947a8b919SAndreas Gohr
21047a8b919SAndreas Gohr    /**
21147a8b919SAndreas Gohr     * @group internet
21247a8b919SAndreas Gohr     */
213d9a7912aSAndreas Gohr    function test_basicauth()
214d9a7912aSAndreas Gohr    {
21595e6ded1SAndreas Gohr        $http = new HTTPMockClient();
216f8369d7dSTobias Sarnowski        $http->user = 'user';
217f8369d7dSTobias Sarnowski        $http->pass = 'pass';
218f8369d7dSTobias Sarnowski        $data = $http->get($this->server . '/basic-auth/user/pass');
21995e6ded1SAndreas Gohr        if ($http->noconnection()) {
22095e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
22195e6ded1SAndreas Gohr        }
2226aad717eSAndreas Gohr        $this->assertFalse($data === false, $http->errorInfo());
223f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
224f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
225d9a7912aSAndreas Gohr        $this->assertEquals(['authorized' => true, 'user' => 'user'], $resp);
226f8369d7dSTobias Sarnowski    }
227f8369d7dSTobias Sarnowski
228f7161c34SAndreas Gohr    /**
229f7161c34SAndreas Gohr     * @group internet
230f7161c34SAndreas Gohr     */
231d9a7912aSAndreas Gohr    public function test_basicauthfail()
232d9a7912aSAndreas Gohr    {
23395e6ded1SAndreas Gohr        $http = new HTTPMockClient();
234f8369d7dSTobias Sarnowski        $http->user = 'user';
235f8369d7dSTobias Sarnowski        $http->pass = 'invalid';
236f8369d7dSTobias Sarnowski        $data = $http->get($this->server . '/basic-auth/user/pass');
23795e6ded1SAndreas Gohr        if ($http->noconnection()) {
23895e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
23995e6ded1SAndreas Gohr        }
2406aad717eSAndreas Gohr        $this->assertTrue($data === false, $http->errorInfo());
241f8369d7dSTobias Sarnowski        $this->assertEquals(401, $http->status);
242f8369d7dSTobias Sarnowski    }
243f8369d7dSTobias Sarnowski
244f7161c34SAndreas Gohr    /**
245f7161c34SAndreas Gohr     * @group internet
246f7161c34SAndreas Gohr     */
247d9a7912aSAndreas Gohr    public function test_timeout()
248d9a7912aSAndreas Gohr    {
24995e6ded1SAndreas Gohr        $http = new HTTPMockClient();
250f8369d7dSTobias Sarnowski        $http->timeout = 5;
251f8369d7dSTobias Sarnowski        $data = $http->get($this->server . '/delay/10');
2526aad717eSAndreas Gohr        $this->assertTrue($data === false, $http->errorInfo());
253f8369d7dSTobias Sarnowski        $this->assertEquals(-100, $http->status);
254f8369d7dSTobias Sarnowski    }
255f8369d7dSTobias Sarnowski
256f7161c34SAndreas Gohr    /**
257f7161c34SAndreas Gohr     * @group internet
258f7161c34SAndreas Gohr     */
259d9a7912aSAndreas Gohr    public function test_headers()
260d9a7912aSAndreas Gohr    {
26195e6ded1SAndreas Gohr        $http = new HTTPMockClient();
262f8369d7dSTobias Sarnowski        $data = $http->get($this->server . '/response-headers?baz=&foo=bar');
26395e6ded1SAndreas Gohr        if ($http->noconnection()) {
26495e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
26595e6ded1SAndreas Gohr        }
2666aad717eSAndreas Gohr        $this->assertFalse($data === false, $http->errorInfo());
267f8369d7dSTobias Sarnowski        $resp = json_decode($data, true);
268f8369d7dSTobias Sarnowski        $this->assertTrue(is_array($resp), 'JSON response');
269f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('baz', $http->resp_headers);
270f8369d7dSTobias Sarnowski        $this->assertArrayHasKey('foo', $http->resp_headers);
271f8369d7dSTobias Sarnowski        $this->assertEquals('bar', $http->resp_headers['foo']);
272f8369d7dSTobias Sarnowski    }
273d37a3955STom N Harris
274d37a3955STom N Harris    /**
275d37a3955STom N Harris     * @group internet
276d37a3955STom N Harris     */
277d9a7912aSAndreas Gohr    public function test_chunked()
278d9a7912aSAndreas Gohr    {
27995e6ded1SAndreas Gohr        $http = new HTTPMockClient();
280d05f72d0SAndreas Gohr        $data = $http->get($this->server . '/stream-bytes/5000?chunk_size=250');
28195e6ded1SAndreas Gohr        if ($http->noconnection()) {
28295e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
28395e6ded1SAndreas Gohr        }
2846aad717eSAndreas Gohr        $this->assertFalse($data === false, $http->errorInfo());
285d05f72d0SAndreas Gohr        $this->assertEquals(5000, strlen($data));
286d37a3955STom N Harris    }
28733b8a947SAndreas Gohr
28833b8a947SAndreas Gohr    /**
28933b8a947SAndreas Gohr     * This address caused trouble with stream_select()
29033b8a947SAndreas Gohr     *
29133b8a947SAndreas Gohr     * @group internet
292bd8c2ebfSAndreas Gohr     * @group flaky
29333b8a947SAndreas Gohr     */
294d9a7912aSAndreas Gohr    public function test_wikimatrix()
295d9a7912aSAndreas Gohr    {
29695e6ded1SAndreas Gohr        $http = new HTTPMockClient();
29733b8a947SAndreas Gohr        $data = $http->get('http://www.wikimatrix.org/cfeed/dokuwiki/-/-');
29895e6ded1SAndreas Gohr        if ($http->noconnection()) {
29995e6ded1SAndreas Gohr            $this->markTestSkipped('connection timed out');
30095e6ded1SAndreas Gohr        }
3016aad717eSAndreas Gohr        $this->assertTrue($data !== false, $http->errorInfo());
30233b8a947SAndreas Gohr    }
3034d4b1f8cSAndreas Gohr
304aba86f38SAndreas Gohr    /**
305aba86f38SAndreas Gohr     * @throws ReflectionException
306aba86f38SAndreas Gohr     */
307d9a7912aSAndreas Gohr    public function test_postencode()
308d9a7912aSAndreas Gohr    {
30995e6ded1SAndreas Gohr        $http = new HTTPMockClient();
3104d4b1f8cSAndreas Gohr
3114d4b1f8cSAndreas Gohr        // check simple data
312d9a7912aSAndreas Gohr        $data = [
3134d4b1f8cSAndreas Gohr            'öä?' => 'öä?',
314d9a7912aSAndreas Gohr            'foo' => 'bang',
315d9a7912aSAndreas Gohr        ];
3164d4b1f8cSAndreas Gohr        $this->assertEquals(
3174d4b1f8cSAndreas Gohr            '%C3%B6%C3%A4%3F=%C3%B6%C3%A4%3F&foo=bang',
3180efa8d12SMichael Große            $this->callInaccessibleMethod($http, 'postEncode', [$data]),
3194d4b1f8cSAndreas Gohr            'simple'
3204d4b1f8cSAndreas Gohr        );
3214d4b1f8cSAndreas Gohr
3224d4b1f8cSAndreas Gohr        // check first level numeric array
323d9a7912aSAndreas Gohr        $data = [
3244d4b1f8cSAndreas Gohr            'foo' => 'bang',
325d9a7912aSAndreas Gohr            'ärr' => ['ö', 'b', 'c'],
326d9a7912aSAndreas Gohr        ];
3274d4b1f8cSAndreas Gohr        $this->assertEquals(
3284d4b1f8cSAndreas Gohr            'foo=bang&%C3%A4rr%5B0%5D=%C3%B6&%C3%A4rr%5B1%5D=b&%C3%A4rr%5B2%5D=c',
3290efa8d12SMichael Große            $this->callInaccessibleMethod($http, 'postEncode', [$data]),
3304d4b1f8cSAndreas Gohr            'onelevelnum'
3314d4b1f8cSAndreas Gohr        );
3324d4b1f8cSAndreas Gohr
3334d4b1f8cSAndreas Gohr        // check first level associative array
334d9a7912aSAndreas Gohr        $data = [
3354d4b1f8cSAndreas Gohr            'foo' => 'bang',
336d9a7912aSAndreas Gohr            'ärr' => ['ö' => 'ä', 'b' => 'c'],
337d9a7912aSAndreas Gohr        ];
3384d4b1f8cSAndreas Gohr        $this->assertEquals(
3394d4b1f8cSAndreas Gohr            'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5Bb%5D=c',
3400efa8d12SMichael Große            $this->callInaccessibleMethod($http, 'postEncode', [$data]),
3414d4b1f8cSAndreas Gohr            'onelevelassoc'
3424d4b1f8cSAndreas Gohr        );
3434d4b1f8cSAndreas Gohr
3444d4b1f8cSAndreas Gohr        // check first level associative array
345d9a7912aSAndreas Gohr        $data = [
3464d4b1f8cSAndreas Gohr            'foo' => 'bang',
347d9a7912aSAndreas Gohr            'ärr' => ['ö' => 'ä', 'ä' => ['ö' => 'ä']],
348d9a7912aSAndreas Gohr        ];
3494d4b1f8cSAndreas Gohr        $this->assertEquals(
3504d4b1f8cSAndreas Gohr            'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5B%C3%A4%5D%5B%C3%B6%5D=%C3%A4',
3510efa8d12SMichael Große            $this->callInaccessibleMethod($http, 'postEncode', [$data]),
3524d4b1f8cSAndreas Gohr            'twolevelassoc'
3534d4b1f8cSAndreas Gohr        );
3544d4b1f8cSAndreas Gohr    }
355f8369d7dSTobias Sarnowski}
356