1<?php 2 3class httpclient_http_test extends DokuWikiTest { 4 protected $server = 'http://httpbin.org'; 5 6 /** 7 * @group internet 8 */ 9 function test_simpleget(){ 10 $http = new HTTPClient(); 11 $data = $http->get($this->server.'/get?foo=bar'); 12 $this->assertFalse($data === false, 'HTTP response'); 13 $resp = json_decode($data, true); 14 $this->assertTrue(is_array($resp), 'JSON response'); 15 $this->assertArrayHasKey('args',$resp); 16 $this->assertEquals(array('foo'=>'bar'), $resp['args']); 17 } 18 19 /** 20 * @group internet 21 */ 22 function test_dget(){ 23 $http = new HTTPClient(); 24 $data = $http->dget($this->server.'/get',array('foo'=>'bar')); 25 $this->assertFalse($data === false, 'HTTP response'); 26 $resp = json_decode($data, true); 27 $this->assertTrue(is_array($resp), 'JSON response'); 28 $this->assertArrayHasKey('args',$resp); 29 $this->assertEquals(array('foo'=>'bar'), $resp['args']); 30 } 31 32 /** 33 * @group internet 34 */ 35 function test_gzip(){ 36 $http = new HTTPClient(); 37 $data = $http->get($this->server.'/gzip'); 38 $this->assertFalse($data === false, 'HTTP response'); 39 $resp = json_decode($data, true); 40 $this->assertTrue(is_array($resp), 'JSON response'); 41 $this->assertArrayHasKey('gzipped',$resp); 42 $this->assertTrue($resp['gzipped']); 43 } 44 45 /** 46 * @group internet 47 */ 48 function test_simplepost(){ 49 $http = new HTTPClient(); 50 $data = $http->post($this->server.'/post',array('foo'=>'bar')); 51 $this->assertFalse($data === false, 'HTTP response'); 52 $resp = json_decode($data, true); 53 $this->assertTrue(is_array($resp), 'JSON response'); 54 $this->assertArrayHasKey('form',$resp); 55 $this->assertEquals(array('foo'=>'bar'), $resp['form']); 56 } 57 58 /** 59 * @group internet 60 */ 61 function test_redirect(){ 62 $http = new HTTPClient(); 63 $data = $http->get($this->server.'/redirect/3'); 64 $this->assertFalse($data === false, 'HTTP response'); 65 $resp = json_decode($data, true); 66 $this->assertTrue(is_array($resp), 'JSON response'); 67 $this->assertArrayHasKey('url',$resp); 68 $this->assertRegExp('/\/get$/', $resp['url']); 69 } 70 71 /** 72 * @group internet 73 */ 74 function test_relredirect(){ 75 $http = new HTTPClient(); 76 $data = $http->get($this->server.'/relative-redirect/3'); 77 $this->assertFalse($data === false, 'HTTP response'); 78 $resp = json_decode($data, true); 79 $this->assertTrue(is_array($resp), 'JSON response'); 80 $this->assertArrayHasKey('url',$resp); 81 $this->assertRegExp('/\/get$/', $resp['url']); 82 } 83 84 /** 85 * @group internet 86 */ 87 function test_redirectfail(){ 88 $http = new HTTPClient(); 89 $data = $http->get($this->server.'/redirect/5'); 90 $this->assertTrue($data === false, 'HTTP response'); 91 $this->assertEquals('Maximum number of redirects exceeded',$http->error); 92 } 93 94 /** 95 * @group internet 96 */ 97 function test_cookies(){ 98 $http = new HTTPClient(); 99 $http->get($this->server.'/cookies/set/foo/bar'); 100 $this->assertEquals(array('foo' => 'bar'), $http->cookies); 101 $data = $http->get($this->server.'/cookies'); 102 $this->assertFalse($data === false, 'HTTP response'); 103 $resp = json_decode($data, true); 104 $this->assertTrue(is_array($resp), 'JSON response'); 105 $this->assertArrayHasKey('cookies',$resp); 106 $this->assertEquals(array('foo'=>'bar'), $resp['cookies']); 107 } 108 109 /** 110 * @group internet 111 */ 112 function test_teapot(){ 113 $http = new HTTPClient(); 114 $data = $http->get($this->server.'/status/418'); 115 $this->assertTrue($data === false, 'HTTP response'); 116 $this->assertEquals(418,$http->status); 117 } 118 119 /** 120 * @group internet 121 */ 122 function test_maxbody(){ 123 $http = new HTTPClient(); 124 $http->max_bodysize = 250; 125 $data = $http->get($this->server.'/stream/30'); 126 $this->assertTrue($data === false, 'HTTP response'); 127 $http->max_bodysize_abort = false; 128 $data = $http->get($this->server.'/stream/30'); 129 $this->assertFalse($data === false, 'HTTP response'); 130 /* should read no more than max_bodysize+1 */ 131 $this->assertLessThanOrEqual(251,strlen($data)); 132 } 133 134 /** 135 * @group internet 136 */ 137 function test_maxbodyok(){ 138 $http = new HTTPClient(); 139 $http->max_bodysize = 500*1024; 140 $data = $http->get($this->server.'/stream/5'); 141 $this->assertTrue($data !== false, 'HTTP response'); 142 $http->max_bodysize_abort = false; 143 $data = $http->get($this->server.'/stream/5'); 144 $this->assertTrue($data !== false, 'HTTP response'); 145 } 146 147 /** 148 * @group internet 149 */ 150 function test_basicauth(){ 151 $http = new HTTPClient(); 152 $http->user = 'user'; 153 $http->pass = 'pass'; 154 $data = $http->get($this->server.'/basic-auth/user/pass'); 155 $this->assertFalse($data === false, 'HTTP response'); 156 $resp = json_decode($data, true); 157 $this->assertTrue(is_array($resp), 'JSON response'); 158 $this->assertEquals(array('authenticated'=>true,'user'=>'user'), $resp); 159 } 160 161 /** 162 * @group internet 163 */ 164 function test_basicauthfail(){ 165 $http = new HTTPClient(); 166 $http->user = 'user'; 167 $http->pass = 'invalid'; 168 $data = $http->get($this->server.'/basic-auth/user/pass'); 169 $this->assertTrue($data === false, 'HTTP response'); 170 $this->assertEquals(401,$http->status); 171 } 172 173 /** 174 * @group internet 175 */ 176 function test_timeout(){ 177 $http = new HTTPClient(); 178 $http->timeout = 5; 179 $data = $http->get($this->server.'/delay/10'); 180 $this->assertTrue($data === false, 'HTTP response'); 181 $this->assertEquals(-100,$http->status); 182 } 183 184 /** 185 * @group internet 186 */ 187 function test_headers(){ 188 $http = new HTTPClient(); 189 $data = $http->get($this->server.'/response-headers?baz=&foo=bar'); 190 $this->assertFalse($data === false, 'HTTP response'); 191 $resp = json_decode($data, true); 192 $this->assertTrue(is_array($resp), 'JSON response'); 193 $this->assertArrayHasKey('baz',$http->resp_headers); 194 $this->assertArrayHasKey('foo',$http->resp_headers); 195 $this->assertEquals('bar',$http->resp_headers['foo']); 196 } 197 198 /** 199 * @group internet 200 */ 201 function test_chunked(){ 202 $http = new HTTPClient(); 203 $data = $http->get('http://whoopdedo.org/cgi-bin/chunked/2550'); 204 $this->assertFalse($data === false, 'HTTP response'); 205 $this->assertEquals(2550,strlen($data)); 206 } 207 208 /** 209 * This address caused trouble with stream_select() 210 * 211 * @group internet 212 */ 213 function test_wikimatrix(){ 214 $http = new HTTPClient(); 215 $data = $http->get('http://www.wikimatrix.org/cfeed/dokuwiki/-/-'); 216 $this->assertTrue($data !== false, $http->error); 217 } 218 219 function test_postencode(){ 220 $http = new HTTPClient(); 221 222 223 // check simple data 224 $data = array( 225 'öä?' => 'öä?', 226 'foo' => 'bang' 227 ); 228 $this->assertEquals( 229 '%C3%B6%C3%A4%3F=%C3%B6%C3%A4%3F&foo=bang', 230 $http->_postEncode($data), 231 'simple' 232 ); 233 234 // check first level numeric array 235 $data = array( 236 'foo' => 'bang', 237 'ärr' => array('ö', 'b', 'c') 238 ); 239 $this->assertEquals( 240 'foo=bang&%C3%A4rr%5B0%5D=%C3%B6&%C3%A4rr%5B1%5D=b&%C3%A4rr%5B2%5D=c', 241 $http->_postEncode($data), 242 'onelevelnum' 243 ); 244 245 // check first level associative array 246 $data = array( 247 'foo' => 'bang', 248 'ärr' => array('ö'=>'ä', 'b' => 'c') 249 ); 250 $this->assertEquals( 251 'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5Bb%5D=c', 252 $http->_postEncode($data), 253 'onelevelassoc' 254 ); 255 256 257 // check first level associative array 258 $data = array( 259 'foo' => 'bang', 260 'ärr' => array('ö'=>'ä', 'ä' => array('ö'=>'ä')) 261 ); 262 $this->assertEquals( 263 'foo=bang&%C3%A4rr%5B%C3%B6%5D=%C3%A4&%C3%A4rr%5B%C3%A4%5D%5B%C3%B6%5D=%C3%A4', 264 $http->_postEncode($data), 265 'twolevelassoc' 266 ); 267 } 268} 269//Setup VIM: ex: et ts=4 : 270