[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_test/tests/inc/ -> httpclient_http.test.php (source)

   1  <?php
   2  
   3  require_once  (__DIR__ . '/httpclient_mock.php');
   4  
   5  class httpclient_http_test extends DokuWikiTest {
   6      protected $server = 'http://eu.httpbin.org';
   7  
   8  
   9      /**
  10       * @group internet
  11       */
  12      function test_simpleget(){
  13          $http = new HTTPMockClient();
  14          $data = $http->get($this->server.'/get?foo=bar');
  15          if($http->noconnection()) {
  16              $this->markTestSkipped('connection timed out');
  17              return;
  18          }
  19          $this->assertFalse($data === false, $http->errorInfo());
  20          $resp = json_decode($data, true);
  21          $this->assertTrue(is_array($resp), 'JSON response');
  22          $this->assertArrayHasKey('args',$resp);
  23          $this->assertEquals(array('foo'=>'bar'), $resp['args']);
  24      }
  25  
  26      /**
  27       * @group internet
  28       */
  29      function test_dget(){
  30          $http = new HTTPMockClient();
  31          $data = $http->dget($this->server.'/get',array('foo'=>'bar'));
  32          if($http->noconnection()) {
  33              $this->markTestSkipped('connection timed out');
  34              return;
  35          }
  36          $this->assertFalse($data === false, $http->errorInfo());
  37          $resp = json_decode($data, true);
  38          $this->assertTrue(is_array($resp), 'JSON response');
  39          $this->assertArrayHasKey('args',$resp);
  40          $this->assertEquals(array('foo'=>'bar'), $resp['args']);
  41      }
  42  
  43      /**
  44       * @group internet
  45       */
  46      function test_gzip(){
  47          $http = new HTTPMockClient();
  48          $data = $http->get($this->server.'/gzip');
  49          if($http->noconnection()) {
  50              $this->markTestSkipped('connection timed out');
  51              return;
  52          }
  53          $this->assertFalse($data === false, $http->errorInfo());
  54          $resp = json_decode($data, true);
  55          $this->assertTrue(is_array($resp), 'JSON response');
  56          $this->assertArrayHasKey('gzipped',$resp);
  57          $this->assertTrue($resp['gzipped']);
  58      }
  59  
  60      /**
  61       * @group internet
  62       */
  63      function test_simplepost(){
  64          $http = new HTTPMockClient();
  65          $data = $http->post($this->server.'/post',array('foo'=>'bar'));
  66          if($http->noconnection()) {
  67              $this->markTestSkipped('connection timed out');
  68              return;
  69          }
  70          $this->assertFalse($data === false, $http->errorInfo());
  71          $resp = json_decode($data, true);
  72          $this->assertTrue(is_array($resp), 'JSON response');
  73          $this->assertArrayHasKey('form',$resp);
  74          $this->assertEquals(array('foo'=>'bar'), $resp['form']);
  75      }
  76  
  77      /**
  78       * @group internet
  79       */
  80      function test_redirect(){
  81          $http = new HTTPMockClient();
  82          $data = $http->get($this->server.'/redirect/3');
  83          if($http->noconnection()) {
  84              $this->markTestSkipped('connection timed out');
  85              return;
  86          }
  87          $this->assertFalse($data === false, $http->errorInfo());
  88          $resp = json_decode($data, true);
  89          $this->assertTrue(is_array($resp), 'JSON response');
  90          $this->assertArrayHasKey('url',$resp);
  91          $this->assertRegExp('/\/get$/', $resp['url']);
  92      }
  93  
  94      /**
  95       * @group internet
  96       */
  97      function test_relredirect(){
  98          $http = new HTTPMockClient();
  99          $data = $http->get($this->server.'/relative-redirect/3');
 100          if($http->noconnection()) {
 101              $this->markTestSkipped('connection timed out');
 102              return;
 103          }
 104          $this->assertFalse($data === false, $http->errorInfo());
 105          $resp = json_decode($data, true);
 106          $this->assertTrue(is_array($resp), 'JSON response');
 107          $this->assertArrayHasKey('url',$resp);
 108          $this->assertRegExp('/\/get$/', $resp['url']);
 109      }
 110  
 111      /**
 112       * @group internet
 113       */
 114      function test_redirectfail(){
 115          $http = new HTTPMockClient();
 116          $data = $http->get($this->server.'/redirect/5');
 117          if($http->noconnection()) {
 118              $this->markTestSkipped('connection timed out');
 119              return;
 120          }
 121          $this->assertTrue($data === false, $http->errorInfo());
 122          $this->assertEquals('Maximum number of redirects exceeded',$http->error);
 123      }
 124  
 125      /**
 126       * @group internet
 127       */
 128      function test_cookies(){
 129          $http = new HTTPMockClient();
 130          $http->get($this->server.'/cookies/set/foo/bar');
 131          if($http->noconnection()) {
 132              $this->markTestSkipped('connection timed out');
 133              return;
 134          }
 135          $this->assertEquals(array('foo' => 'bar'), $http->cookies);
 136          $data = $http->get($this->server.'/cookies');
 137          if($http->noconnection()) {
 138              $this->markTestSkipped('connection timed out');
 139              return;
 140          }
 141          $this->assertFalse($data === false, $http->errorInfo());
 142          $resp = json_decode($data, true);
 143          $this->assertTrue(is_array($resp), 'JSON response');
 144          $this->assertArrayHasKey('cookies',$resp);
 145          $this->assertEquals(array('foo'=>'bar'), $resp['cookies']);
 146      }
 147  
 148      /**
 149       * @group internet
 150       */
 151      function test_teapot(){
 152          $http = new HTTPMockClient();
 153          $data = $http->get($this->server.'/status/418');
 154          if($http->noconnection()) {
 155              $this->markTestSkipped('connection timed out');
 156              return;
 157          }
 158          $this->assertTrue($data === false, $http->errorInfo());
 159          $this->assertEquals(418,$http->status);
 160      }
 161  
 162      /**
 163       * @group internet
 164       */
 165      function test_maxbody(){
 166          $http = new HTTPMockClient();
 167          $http->max_bodysize = 250;
 168  
 169          // this should abort completely
 170          $data = $http->get($this->server.'/stream/30');
 171          if($http->noconnection()) {
 172              $this->markTestSkipped('connection timed out');
 173              return;
 174          }
 175          $this->assertTrue($data === false, $http->errorInfo());
 176  
 177          // this should read just the needed bytes
 178          $http->max_bodysize_abort = false;
 179          $http->keep_alive = false;
 180          $data = $http->get($this->server.'/stream/30');
 181          if($http->noconnection()) {
 182              $this->markTestSkipped('connection timed out');
 183              return;
 184          }
 185          $this->assertFalse($data === false, $http->errorInfo());
 186          /* should read no more than max_bodysize+1 */
 187          $this->assertLessThanOrEqual(251,strlen($data));
 188      }
 189  
 190      /**
 191       * @group internet
 192       */
 193      function test_maxbodyok(){
 194          $http = new HTTPMockClient();
 195          $http->max_bodysize = 500*1024;
 196          $data = $http->get($this->server.'/stream/5');
 197          if($http->noconnection()) {
 198              $this->markTestSkipped('connection timed out');
 199              return;
 200          }
 201          $this->assertTrue($data !== false, $http->errorInfo());
 202          $http->max_bodysize_abort = false;
 203          $data = $http->get($this->server.'/stream/5');
 204          if($http->noconnection()) {
 205              $this->markTestSkipped('connection timed out');
 206              return;
 207          }
 208          $this->assertTrue($data !== false, $http->errorInfo());
 209      }
 210  
 211      /**
 212       * @group internet
 213       */
 214      function test_basicauth(){
 215          $http = new HTTPMockClient();
 216          $http->user = 'user';
 217          $http->pass = 'pass';
 218          $data = $http->get($this->server.'/basic-auth/user/pass');
 219          if($http->noconnection()) {
 220              $this->markTestSkipped('connection timed out');
 221              return;
 222          }
 223          $this->assertFalse($data === false, $http->errorInfo());
 224          $resp = json_decode($data, true);
 225          $this->assertTrue(is_array($resp), 'JSON response');
 226          $this->assertEquals(array('authenticated'=>true,'user'=>'user'), $resp);
 227      }
 228  
 229      /**
 230       * @group internet
 231       */
 232      function test_basicauthfail(){
 233          $http = new HTTPMockClient();
 234          $http->user = 'user';
 235          $http->pass = 'invalid';
 236          $data = $http->get($this->server.'/basic-auth/user/pass');
 237          if($http->noconnection()) {
 238              $this->markTestSkipped('connection timed out');
 239              return;
 240          }
 241          $this->assertTrue($data === false, $http->errorInfo());
 242          $this->assertEquals(401,$http->status);
 243      }
 244  
 245      /**
 246       * @group internet
 247       */
 248      function test_timeout(){
 249          $http = new HTTPMockClient();
 250          $http->timeout = 5;
 251          $data = $http->get($this->server.'/delay/10');
 252          $this->assertTrue($data === false, $http->errorInfo());
 253          $this->assertEquals(-100,$http->status);
 254      }
 255  
 256      /**
 257       * @group internet
 258       */
 259      function test_headers(){
 260          $http = new HTTPMockClient();
 261          $data = $http->get($this->server.'/response-headers?baz=&foo=bar');
 262          if($http->noconnection()) {
 263              $this->markTestSkipped('connection timed out');
 264              return;
 265          }
 266          $this->assertFalse($data === false, $http->errorInfo());
 267          $resp = json_decode($data, true);
 268          $this->assertTrue(is_array($resp), 'JSON response');
 269          $this->assertArrayHasKey('baz',$http->resp_headers);
 270          $this->assertArrayHasKey('foo',$http->resp_headers);
 271          $this->assertEquals('bar',$http->resp_headers['foo']);
 272      }
 273  
 274      /**
 275       * @group internet
 276       */
 277      function test_chunked(){
 278          $http = new HTTPMockClient();
 279          $data = $http->get($this->server.'/stream-bytes/5000?chunk_size=250');
 280          if($http->noconnection()) {
 281              $this->markTestSkipped('connection timed out');
 282              return;
 283          }
 284          $this->assertFalse($data === false, $http->errorInfo());
 285          $this->assertEquals(5000,strlen($data));
 286      }
 287  
 288      /**
 289       * This address caused trouble with stream_select()
 290       *
 291       * @group internet
 292       * @group flaky
 293       */
 294      function test_wikimatrix(){
 295          $http = new HTTPMockClient();
 296          $data = $http->get('http://www.wikimatrix.org/cfeed/dokuwiki/-/-');
 297          if($http->noconnection()) {
 298              $this->markTestSkipped('connection timed out');
 299              return;
 300          }
 301          $this->assertTrue($data !== false, $http->errorInfo());
 302      }
 303  
 304      /**
 305       * @throws ReflectionException
 306       */
 307      function test_postencode(){
 308          $http = new HTTPMockClient();
 309  
 310  
 311          // check simple data
 312          $data = array(
 313              'öä?' => 'öä?',
 314              'foo' => 'bang'
 315          );
 316          $this->assertEquals(
 317              '%C3%B6%C3%A4%3F=%C3%B6%C3%A4%3F&foo=bang',
 318              $this->callInaccessibleMethod($http, 'postEncode', [$data]),
 319              'simple'
 320          );
 321  
 322          // check first level numeric array
 323          $data = array(
 324              'foo' => 'bang',
 325              'ärr' => array('ö', 'b', 'c')
 326          );
 327          $this->assertEquals(
 328              'foo=bang&%C3%A4rr%5B0%5D=%C3%B6&%C3%A4rr%5B1%5D=b&%C3%A4rr%5B2%5D=c',
 329              $this->callInaccessibleMethod($http, 'postEncode', [$data]),
 330              'onelevelnum'
 331          );
 332  
 333          // check first level associative array
 334          $data = array(
 335              'foo' => 'bang',
 336              'ärr' => array('ö'=>'ä', 'b' => 'c')
 337          );
 338          $this->assertEquals(
 339              'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5Bb%5D=c',
 340              $this->callInaccessibleMethod($http, 'postEncode', [$data]),
 341              'onelevelassoc'
 342          );
 343  
 344  
 345          // check first level associative array
 346          $data = array(
 347              'foo' => 'bang',
 348              'ärr' => array('ö'=>'ä', 'ä' => array('ö'=>'ä'))
 349          );
 350          $this->assertEquals(
 351              'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5B%C3%A4%5D%5B%C3%B6%5D=%C3%A4',
 352              $this->callInaccessibleMethod($http, 'postEncode', [$data]),
 353              'twolevelassoc'
 354          );
 355      }
 356  }
 357  //Setup VIM: ex: et ts=4 :