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); 949ad2b913SAndreas 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); 1119ad2b913SAndreas 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'); 146*e7a87ee2SAndreas Gohr $this->assertEquals(['cookies' => ['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'); 225*e7a87ee2SAndreas Gohr $this->assertEquals([ 226*e7a87ee2SAndreas Gohr 'authorized' => true, 227*e7a87ee2SAndreas Gohr 'user' => 'user', 228*e7a87ee2SAndreas Gohr 'authenticated' => true 229*e7a87ee2SAndreas Gohr ], $resp); 230f8369d7dSTobias Sarnowski } 231f8369d7dSTobias Sarnowski 232f7161c34SAndreas Gohr /** 233f7161c34SAndreas Gohr * @group internet 234f7161c34SAndreas Gohr */ 235d9a7912aSAndreas Gohr public function test_basicauthfail() 236d9a7912aSAndreas Gohr { 23795e6ded1SAndreas Gohr $http = new HTTPMockClient(); 238f8369d7dSTobias Sarnowski $http->user = 'user'; 239f8369d7dSTobias Sarnowski $http->pass = 'invalid'; 240f8369d7dSTobias Sarnowski $data = $http->get($this->server . '/basic-auth/user/pass'); 24195e6ded1SAndreas Gohr if ($http->noconnection()) { 24295e6ded1SAndreas Gohr $this->markTestSkipped('connection timed out'); 24395e6ded1SAndreas Gohr } 2446aad717eSAndreas Gohr $this->assertTrue($data === false, $http->errorInfo()); 245f8369d7dSTobias Sarnowski $this->assertEquals(401, $http->status); 246f8369d7dSTobias Sarnowski } 247f8369d7dSTobias Sarnowski 248f7161c34SAndreas Gohr /** 249f7161c34SAndreas Gohr * @group internet 250f7161c34SAndreas Gohr */ 251d9a7912aSAndreas Gohr public function test_timeout() 252d9a7912aSAndreas Gohr { 25395e6ded1SAndreas Gohr $http = new HTTPMockClient(); 254f8369d7dSTobias Sarnowski $http->timeout = 5; 255f8369d7dSTobias Sarnowski $data = $http->get($this->server . '/delay/10'); 2566aad717eSAndreas Gohr $this->assertTrue($data === false, $http->errorInfo()); 257f8369d7dSTobias Sarnowski $this->assertEquals(-100, $http->status); 258f8369d7dSTobias Sarnowski } 259f8369d7dSTobias Sarnowski 260f7161c34SAndreas Gohr /** 261f7161c34SAndreas Gohr * @group internet 262f7161c34SAndreas Gohr */ 263d9a7912aSAndreas Gohr public function test_headers() 264d9a7912aSAndreas Gohr { 26595e6ded1SAndreas Gohr $http = new HTTPMockClient(); 266f8369d7dSTobias Sarnowski $data = $http->get($this->server . '/response-headers?baz=&foo=bar'); 26795e6ded1SAndreas Gohr if ($http->noconnection()) { 26895e6ded1SAndreas Gohr $this->markTestSkipped('connection timed out'); 26995e6ded1SAndreas Gohr } 2706aad717eSAndreas Gohr $this->assertFalse($data === false, $http->errorInfo()); 271f8369d7dSTobias Sarnowski $resp = json_decode($data, true); 272f8369d7dSTobias Sarnowski $this->assertTrue(is_array($resp), 'JSON response'); 273f8369d7dSTobias Sarnowski $this->assertArrayHasKey('baz', $http->resp_headers); 274f8369d7dSTobias Sarnowski $this->assertArrayHasKey('foo', $http->resp_headers); 275f8369d7dSTobias Sarnowski $this->assertEquals('bar', $http->resp_headers['foo']); 276f8369d7dSTobias Sarnowski } 277d37a3955STom N Harris 278d37a3955STom N Harris /** 279d37a3955STom N Harris * @group internet 280d37a3955STom N Harris */ 281d9a7912aSAndreas Gohr public function test_chunked() 282d9a7912aSAndreas Gohr { 28395e6ded1SAndreas Gohr $http = new HTTPMockClient(); 284d05f72d0SAndreas Gohr $data = $http->get($this->server . '/stream-bytes/5000?chunk_size=250'); 28595e6ded1SAndreas Gohr if ($http->noconnection()) { 28695e6ded1SAndreas Gohr $this->markTestSkipped('connection timed out'); 28795e6ded1SAndreas Gohr } 2886aad717eSAndreas Gohr $this->assertFalse($data === false, $http->errorInfo()); 289d05f72d0SAndreas Gohr $this->assertEquals(5000, strlen($data)); 290d37a3955STom N Harris } 29133b8a947SAndreas Gohr 29233b8a947SAndreas Gohr /** 29333b8a947SAndreas Gohr * This address caused trouble with stream_select() 29433b8a947SAndreas Gohr * 29533b8a947SAndreas Gohr * @group internet 296bd8c2ebfSAndreas Gohr * @group flaky 29733b8a947SAndreas Gohr */ 298d9a7912aSAndreas Gohr public function test_wikimatrix() 299d9a7912aSAndreas Gohr { 30095e6ded1SAndreas Gohr $http = new HTTPMockClient(); 30133b8a947SAndreas Gohr $data = $http->get('http://www.wikimatrix.org/cfeed/dokuwiki/-/-'); 30295e6ded1SAndreas Gohr if ($http->noconnection()) { 30395e6ded1SAndreas Gohr $this->markTestSkipped('connection timed out'); 30495e6ded1SAndreas Gohr } 3056aad717eSAndreas Gohr $this->assertTrue($data !== false, $http->errorInfo()); 30633b8a947SAndreas Gohr } 3074d4b1f8cSAndreas Gohr 308aba86f38SAndreas Gohr /** 309aba86f38SAndreas Gohr * @throws ReflectionException 310aba86f38SAndreas Gohr */ 311d9a7912aSAndreas Gohr public function test_postencode() 312d9a7912aSAndreas Gohr { 31395e6ded1SAndreas Gohr $http = new HTTPMockClient(); 3144d4b1f8cSAndreas Gohr 3154d4b1f8cSAndreas Gohr // check simple data 316d9a7912aSAndreas Gohr $data = [ 3174d4b1f8cSAndreas Gohr 'öä?' => 'öä?', 318d9a7912aSAndreas Gohr 'foo' => 'bang', 319d9a7912aSAndreas Gohr ]; 3204d4b1f8cSAndreas Gohr $this->assertEquals( 3214d4b1f8cSAndreas Gohr '%C3%B6%C3%A4%3F=%C3%B6%C3%A4%3F&foo=bang', 3220efa8d12SMichael Große $this->callInaccessibleMethod($http, 'postEncode', [$data]), 3234d4b1f8cSAndreas Gohr 'simple' 3244d4b1f8cSAndreas Gohr ); 3254d4b1f8cSAndreas Gohr 3264d4b1f8cSAndreas Gohr // check first level numeric array 327d9a7912aSAndreas Gohr $data = [ 3284d4b1f8cSAndreas Gohr 'foo' => 'bang', 329d9a7912aSAndreas Gohr 'ärr' => ['ö', 'b', 'c'], 330d9a7912aSAndreas Gohr ]; 3314d4b1f8cSAndreas Gohr $this->assertEquals( 3324d4b1f8cSAndreas Gohr 'foo=bang&%C3%A4rr%5B0%5D=%C3%B6&%C3%A4rr%5B1%5D=b&%C3%A4rr%5B2%5D=c', 3330efa8d12SMichael Große $this->callInaccessibleMethod($http, 'postEncode', [$data]), 3344d4b1f8cSAndreas Gohr 'onelevelnum' 3354d4b1f8cSAndreas Gohr ); 3364d4b1f8cSAndreas Gohr 3374d4b1f8cSAndreas Gohr // check first level associative array 338d9a7912aSAndreas Gohr $data = [ 3394d4b1f8cSAndreas Gohr 'foo' => 'bang', 340d9a7912aSAndreas Gohr 'ärr' => ['ö' => 'ä', 'b' => 'c'], 341d9a7912aSAndreas Gohr ]; 3424d4b1f8cSAndreas Gohr $this->assertEquals( 3434d4b1f8cSAndreas Gohr 'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5Bb%5D=c', 3440efa8d12SMichael Große $this->callInaccessibleMethod($http, 'postEncode', [$data]), 3454d4b1f8cSAndreas Gohr 'onelevelassoc' 3464d4b1f8cSAndreas Gohr ); 3474d4b1f8cSAndreas Gohr 3484d4b1f8cSAndreas Gohr // check first level associative array 349d9a7912aSAndreas Gohr $data = [ 3504d4b1f8cSAndreas Gohr 'foo' => 'bang', 351d9a7912aSAndreas Gohr 'ärr' => ['ö' => 'ä', 'ä' => ['ö' => 'ä']], 352d9a7912aSAndreas Gohr ]; 3534d4b1f8cSAndreas Gohr $this->assertEquals( 3544d4b1f8cSAndreas Gohr 'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5B%C3%A4%5D%5B%C3%B6%5D=%C3%A4', 3550efa8d12SMichael Große $this->callInaccessibleMethod($http, 'postEncode', [$data]), 3564d4b1f8cSAndreas Gohr 'twolevelassoc' 3574d4b1f8cSAndreas Gohr ); 3584d4b1f8cSAndreas Gohr } 359f8369d7dSTobias Sarnowski} 360