[ 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          $this->markTestSkipped('disabled while redirect bug in httpbin is being fixed');
  82          return;
  83  
  84          $http = new HTTPMockClient();
  85          $data = $http->get($this->server.'/redirect/3');
  86          if($http->noconnection()) {
  87              $this->markTestSkipped('connection timed out');
  88              return;
  89          }
  90          $this->assertFalse($data === false, $http->errorInfo());
  91          $resp = json_decode($data, true);
  92          $this->assertTrue(is_array($resp), 'JSON response');
  93          $this->assertArrayHasKey('url',$resp);
  94          $this->assertRegExp('/\/get$/', $resp['url']);
  95      }
  96  
  97      /**
  98       * @group internet
  99       */
 100      function test_relredirect(){
 101          $this->markTestSkipped('disabled while redirect bug in httpbin is being fixed');
 102          return;
 103  
 104          $http = new HTTPMockClient();
 105          $data = $http->get($this->server.'/relative-redirect/3');
 106          if($http->noconnection()) {
 107              $this->markTestSkipped('connection timed out');
 108              return;
 109          }
 110          $this->assertFalse($data === false, $http->errorInfo());
 111          $resp = json_decode($data, true);
 112          $this->assertTrue(is_array($resp), 'JSON response');
 113          $this->assertArrayHasKey('url',$resp);
 114          $this->assertRegExp('/\/get$/', $resp['url']);
 115      }
 116  
 117      /**
 118       * @group internet
 119       */
 120      function test_redirectfail(){
 121          $this->markTestSkipped('disabled while redirect bug in httpbin is being fixed');
 122          return;
 123  
 124          $http = new HTTPMockClient();
 125          $data = $http->get($this->server.'/redirect/5');
 126          if($http->noconnection()) {
 127              $this->markTestSkipped('connection timed out');
 128              return;
 129          }
 130          $this->assertTrue($data === false, $http->errorInfo());
 131          $this->assertEquals('Maximum number of redirects exceeded',$http->error);
 132      }
 133  
 134      /**
 135       * @group internet
 136       */
 137      function test_cookies(){
 138          $http = new HTTPMockClient();
 139          $http->get($this->server.'/cookies/set/foo/bar');
 140          if($http->noconnection()) {
 141              $this->markTestSkipped('connection timed out');
 142              return;
 143          }
 144          $this->assertEquals(array('foo' => 'bar'), $http->cookies);
 145          $data = $http->get($this->server.'/cookies');
 146          if($http->noconnection()) {
 147              $this->markTestSkipped('connection timed out');
 148              return;
 149          }
 150          $this->assertFalse($data === false, $http->errorInfo());
 151          $resp = json_decode($data, true);
 152          $this->assertTrue(is_array($resp), 'JSON response');
 153          $this->assertArrayHasKey('cookies',$resp);
 154          $this->assertEquals(array('foo'=>'bar'), $resp['cookies']);
 155      }
 156  
 157      /**
 158       * @group internet
 159       */
 160      function test_teapot(){
 161          $http = new HTTPMockClient();
 162          $data = $http->get($this->server.'/status/418');
 163          if($http->noconnection()) {
 164              $this->markTestSkipped('connection timed out');
 165              return;
 166          }
 167          $this->assertTrue($data === false, $http->errorInfo());
 168          $this->assertEquals(418,$http->status);
 169      }
 170  
 171      /**
 172       * @group internet
 173       */
 174      function test_maxbody(){
 175          $http = new HTTPMockClient();
 176          $http->max_bodysize = 250;
 177  
 178          // this should abort completely
 179          $data = $http->get($this->server.'/stream/30');
 180          if($http->noconnection()) {
 181              $this->markTestSkipped('connection timed out');
 182              return;
 183          }
 184          $this->assertTrue($data === false, $http->errorInfo());
 185  
 186          // this should read just the needed bytes
 187          $http->max_bodysize_abort = false;
 188          $http->keep_alive = false;
 189          $data = $http->get($this->server.'/stream/30');
 190          if($http->noconnection()) {
 191              $this->markTestSkipped('connection timed out');
 192              return;
 193          }
 194          $this->assertFalse($data === false, $http->errorInfo());
 195          /* should read no more than max_bodysize+1 */
 196          $this->assertLessThanOrEqual(251,strlen($data));
 197      }
 198  
 199      /**
 200       * @group internet
 201       */
 202      function test_maxbodyok(){
 203          $http = new HTTPMockClient();
 204          $http->max_bodysize = 500*1024;
 205          $data = $http->get($this->server.'/stream/5');
 206          if($http->noconnection()) {
 207              $this->markTestSkipped('connection timed out');
 208              return;
 209          }
 210          $this->assertTrue($data !== false, $http->errorInfo());
 211          $http->max_bodysize_abort = false;
 212          $data = $http->get($this->server.'/stream/5');
 213          if($http->noconnection()) {
 214              $this->markTestSkipped('connection timed out');
 215              return;
 216          }
 217          $this->assertTrue($data !== false, $http->errorInfo());
 218      }
 219  
 220      /**
 221       * @group internet
 222       */
 223      function test_basicauth(){
 224          $http = new HTTPMockClient();
 225          $http->user = 'user';
 226          $http->pass = 'pass';
 227          $data = $http->get($this->server.'/basic-auth/user/pass');
 228          if($http->noconnection()) {
 229              $this->markTestSkipped('connection timed out');
 230              return;
 231          }
 232          $this->assertFalse($data === false, $http->errorInfo());
 233          $resp = json_decode($data, true);
 234          $this->assertTrue(is_array($resp), 'JSON response');
 235          $this->assertEquals(array('authenticated'=>true,'user'=>'user'), $resp);
 236      }
 237  
 238      /**
 239       * @group internet
 240       */
 241      function test_basicauthfail(){
 242          $http = new HTTPMockClient();
 243          $http->user = 'user';
 244          $http->pass = 'invalid';
 245          $data = $http->get($this->server.'/basic-auth/user/pass');
 246          if($http->noconnection()) {
 247              $this->markTestSkipped('connection timed out');
 248              return;
 249          }
 250          $this->assertTrue($data === false, $http->errorInfo());
 251          $this->assertEquals(401,$http->status);
 252      }
 253  
 254      /**
 255       * @group internet
 256       */
 257      function test_timeout(){
 258          $http = new HTTPMockClient();
 259          $http->timeout = 5;
 260          $data = $http->get($this->server.'/delay/10');
 261          $this->assertTrue($data === false, $http->errorInfo());
 262          $this->assertEquals(-100,$http->status);
 263      }
 264  
 265      /**
 266       * @group internet
 267       */
 268      function test_headers(){
 269          $http = new HTTPMockClient();
 270          $data = $http->get($this->server.'/response-headers?baz=&foo=bar');
 271          if($http->noconnection()) {
 272              $this->markTestSkipped('connection timed out');
 273              return;
 274          }
 275          $this->assertFalse($data === false, $http->errorInfo());
 276          $resp = json_decode($data, true);
 277          $this->assertTrue(is_array($resp), 'JSON response');
 278          $this->assertArrayHasKey('baz',$http->resp_headers);
 279          $this->assertArrayHasKey('foo',$http->resp_headers);
 280          $this->assertEquals('bar',$http->resp_headers['foo']);
 281      }
 282  
 283      /**
 284       * @group internet
 285       */
 286      function test_chunked(){
 287          $http = new HTTPMockClient();
 288          $data = $http->get($this->server.'/stream-bytes/5000?chunk_size=250');
 289          if($http->noconnection()) {
 290              $this->markTestSkipped('connection timed out');
 291              return;
 292          }
 293          $this->assertFalse($data === false, $http->errorInfo());
 294          $this->assertEquals(5000,strlen($data));
 295      }
 296  
 297      /**
 298       * This address caused trouble with stream_select()
 299       *
 300       * @group internet
 301       * @group flaky
 302       */
 303      function test_wikimatrix(){
 304          $http = new HTTPMockClient();
 305          $data = $http->get('http://www.wikimatrix.org/cfeed/dokuwiki/-/-');
 306          if($http->noconnection()) {
 307              $this->markTestSkipped('connection timed out');
 308              return;
 309          }
 310          $this->assertTrue($data !== false, $http->errorInfo());
 311      }
 312  
 313      /**
 314       * @throws ReflectionException
 315       */
 316      function test_postencode(){
 317          $http = new HTTPMockClient();
 318  
 319  
 320          // check simple data
 321          $data = array(
 322              'öä?' => 'öä?',
 323              'foo' => 'bang'
 324          );
 325          $this->assertEquals(
 326              '%C3%B6%C3%A4%3F=%C3%B6%C3%A4%3F&foo=bang',
 327              $this->callInaccessibleMethod($http, 'postEncode', [$data]),
 328              'simple'
 329          );
 330  
 331          // check first level numeric array
 332          $data = array(
 333              'foo' => 'bang',
 334              'ärr' => array('ö', 'b', 'c')
 335          );
 336          $this->assertEquals(
 337              'foo=bang&%C3%A4rr%5B0%5D=%C3%B6&%C3%A4rr%5B1%5D=b&%C3%A4rr%5B2%5D=c',
 338              $this->callInaccessibleMethod($http, 'postEncode', [$data]),
 339              'onelevelnum'
 340          );
 341  
 342          // check first level associative array
 343          $data = array(
 344              'foo' => 'bang',
 345              'ärr' => array('ö'=>'ä', 'b' => 'c')
 346          );
 347          $this->assertEquals(
 348              'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5Bb%5D=c',
 349              $this->callInaccessibleMethod($http, 'postEncode', [$data]),
 350              'onelevelassoc'
 351          );
 352  
 353  
 354          // check first level associative array
 355          $data = array(
 356              'foo' => 'bang',
 357              'ärr' => array('ö'=>'ä', 'ä' => array('ö'=>'ä'))
 358          );
 359          $this->assertEquals(
 360              'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5B%C3%A4%5D%5B%C3%B6%5D=%C3%A4',
 361              $this->callInaccessibleMethod($http, 'postEncode', [$data]),
 362              'twolevelassoc'
 363          );
 364      }
 365  }
 366  //Setup VIM: ex: et ts=4 :