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_basicauth(){ 138 $http = new HTTPClient(); 139 $http->user = 'user'; 140 $http->pass = 'pass'; 141 $data = $http->get($this->server.'/basic-auth/user/pass'); 142 $this->assertFalse($data === false, 'HTTP response'); 143 $resp = json_decode($data, true); 144 $this->assertTrue(is_array($resp), 'JSON response'); 145 $this->assertEquals(array('authenticated'=>true,'user'=>'user'), $resp); 146 } 147 148 /** 149 * @group internet 150 */ 151 function test_basicauthfail(){ 152 $http = new HTTPClient(); 153 $http->user = 'user'; 154 $http->pass = 'invalid'; 155 $data = $http->get($this->server.'/basic-auth/user/pass'); 156 $this->assertTrue($data === false, 'HTTP response'); 157 $this->assertEquals(401,$http->status); 158 } 159 160 /** 161 * @group internet 162 */ 163 function test_timeout(){ 164 $http = new HTTPClient(); 165 $http->timeout = 5; 166 $data = $http->get($this->server.'/delay/10'); 167 $this->assertTrue($data === false, 'HTTP response'); 168 $this->assertEquals(-100,$http->status); 169 } 170 171 /** 172 * @group internet 173 */ 174 function test_headers(){ 175 $http = new HTTPClient(); 176 $data = $http->get($this->server.'/response-headers?baz=&foo=bar'); 177 $this->assertFalse($data === false, 'HTTP response'); 178 $resp = json_decode($data, true); 179 $this->assertTrue(is_array($resp), 'JSON response'); 180 $this->assertArrayHasKey('baz',$http->resp_headers); 181 $this->assertArrayHasKey('foo',$http->resp_headers); 182 $this->assertEquals('bar',$http->resp_headers['foo']); 183 } 184 185 /** 186 * @group internet 187 */ 188 function test_chunked(){ 189 $http = new HTTPClient(); 190 $data = $http->get('http://whoopdedo.org/cgi-bin/chunked/2550'); 191 $this->assertFalse($data === false, 'HTTP response'); 192 $this->assertEquals(2550,strlen($data)); 193 } 194} 195//Setup VIM: ex: et ts=4 : 196