1f8369d7dSTobias Sarnowski<?php 2f8369d7dSTobias Sarnowski 3f8369d7dSTobias Sarnowskiclass httpclient_http_test extends DokuWikiTest { 4f8369d7dSTobias Sarnowski protected $server = 'http://httpbin.org'; 5f8369d7dSTobias Sarnowski 6*f7161c34SAndreas Gohr /** 7*f7161c34SAndreas Gohr * @group internet 8*f7161c34SAndreas Gohr */ 9f8369d7dSTobias Sarnowski function test_simpleget(){ 10f8369d7dSTobias Sarnowski $http = new HTTPClient(); 11f8369d7dSTobias Sarnowski $data = $http->get($this->server.'/get?foo=bar'); 12f8369d7dSTobias Sarnowski $this->assertFalse($data === false, 'HTTP response'); 13f8369d7dSTobias Sarnowski $resp = json_decode($data, true); 14f8369d7dSTobias Sarnowski $this->assertTrue(is_array($resp), 'JSON response'); 15f8369d7dSTobias Sarnowski $this->assertArrayHasKey('args',$resp); 16f8369d7dSTobias Sarnowski $this->assertEquals(array('foo'=>'bar'), $resp['args']); 17f8369d7dSTobias Sarnowski } 18f8369d7dSTobias Sarnowski 19*f7161c34SAndreas Gohr /** 20*f7161c34SAndreas Gohr * @group internet 21*f7161c34SAndreas Gohr */ 22f8369d7dSTobias Sarnowski function test_dget(){ 23f8369d7dSTobias Sarnowski $http = new HTTPClient(); 24f8369d7dSTobias Sarnowski $data = $http->dget($this->server.'/get',array('foo'=>'bar')); 25f8369d7dSTobias Sarnowski $this->assertFalse($data === false, 'HTTP response'); 26f8369d7dSTobias Sarnowski $resp = json_decode($data, true); 27f8369d7dSTobias Sarnowski $this->assertTrue(is_array($resp), 'JSON response'); 28f8369d7dSTobias Sarnowski $this->assertArrayHasKey('args',$resp); 29f8369d7dSTobias Sarnowski $this->assertEquals(array('foo'=>'bar'), $resp['args']); 30f8369d7dSTobias Sarnowski } 31f8369d7dSTobias Sarnowski 32*f7161c34SAndreas Gohr /** 33*f7161c34SAndreas Gohr * @group internet 34*f7161c34SAndreas Gohr */ 35f8369d7dSTobias Sarnowski function test_gzip(){ 36f8369d7dSTobias Sarnowski $http = new HTTPClient(); 37f8369d7dSTobias Sarnowski $data = $http->get($this->server.'/gzip'); 38f8369d7dSTobias Sarnowski $this->assertFalse($data === false, 'HTTP response'); 39f8369d7dSTobias Sarnowski $resp = json_decode($data, true); 40f8369d7dSTobias Sarnowski $this->assertTrue(is_array($resp), 'JSON response'); 41f8369d7dSTobias Sarnowski $this->assertArrayHasKey('gzipped',$resp); 42f8369d7dSTobias Sarnowski $this->assertTrue($resp['gzipped']); 43f8369d7dSTobias Sarnowski } 44f8369d7dSTobias Sarnowski 45*f7161c34SAndreas Gohr /** 46*f7161c34SAndreas Gohr * @group internet 47*f7161c34SAndreas Gohr */ 48f8369d7dSTobias Sarnowski function test_simplepost(){ 49f8369d7dSTobias Sarnowski $http = new HTTPClient(); 50f8369d7dSTobias Sarnowski $data = $http->post($this->server.'/post',array('foo'=>'bar')); 51f8369d7dSTobias Sarnowski $this->assertFalse($data === false, 'HTTP response'); 52f8369d7dSTobias Sarnowski $resp = json_decode($data, true); 53f8369d7dSTobias Sarnowski $this->assertTrue(is_array($resp), 'JSON response'); 54f8369d7dSTobias Sarnowski $this->assertArrayHasKey('form',$resp); 55f8369d7dSTobias Sarnowski $this->assertEquals(array('foo'=>'bar'), $resp['form']); 56f8369d7dSTobias Sarnowski } 57f8369d7dSTobias Sarnowski 58*f7161c34SAndreas Gohr /** 59*f7161c34SAndreas Gohr * @group internet 60*f7161c34SAndreas Gohr */ 61f8369d7dSTobias Sarnowski function test_redirect(){ 62f8369d7dSTobias Sarnowski $http = new HTTPClient(); 63f8369d7dSTobias Sarnowski $data = $http->get($this->server.'/redirect/3'); 64f8369d7dSTobias Sarnowski $this->assertFalse($data === false, 'HTTP response'); 65f8369d7dSTobias Sarnowski $resp = json_decode($data, true); 66f8369d7dSTobias Sarnowski $this->assertTrue(is_array($resp), 'JSON response'); 67f8369d7dSTobias Sarnowski $this->assertArrayHasKey('url',$resp); 68f8369d7dSTobias Sarnowski $this->assertRegExp('/\/get$/', $resp['url']); 69f8369d7dSTobias Sarnowski } 70f8369d7dSTobias Sarnowski 71*f7161c34SAndreas Gohr /** 72*f7161c34SAndreas Gohr * @group internet 73*f7161c34SAndreas Gohr */ 74f8369d7dSTobias Sarnowski function test_relredirect(){ 75f8369d7dSTobias Sarnowski $http = new HTTPClient(); 76f8369d7dSTobias Sarnowski $data = $http->get($this->server.'/relative-redirect/3'); 77f8369d7dSTobias Sarnowski $this->assertFalse($data === false, 'HTTP response'); 78f8369d7dSTobias Sarnowski $resp = json_decode($data, true); 79f8369d7dSTobias Sarnowski $this->assertTrue(is_array($resp), 'JSON response'); 80f8369d7dSTobias Sarnowski $this->assertArrayHasKey('url',$resp); 81f8369d7dSTobias Sarnowski $this->assertRegExp('/\/get$/', $resp['url']); 82f8369d7dSTobias Sarnowski } 83f8369d7dSTobias Sarnowski 84*f7161c34SAndreas Gohr /** 85*f7161c34SAndreas Gohr * @group internet 86*f7161c34SAndreas Gohr */ 87f8369d7dSTobias Sarnowski function test_redirectfail(){ 88f8369d7dSTobias Sarnowski $http = new HTTPClient(); 89f8369d7dSTobias Sarnowski $data = $http->get($this->server.'/redirect/5'); 90f8369d7dSTobias Sarnowski $this->assertTrue($data === false, 'HTTP response'); 91f8369d7dSTobias Sarnowski $this->assertEquals('Maximum number of redirects exceeded',$http->error); 92f8369d7dSTobias Sarnowski } 93f8369d7dSTobias Sarnowski 94*f7161c34SAndreas Gohr /** 95*f7161c34SAndreas Gohr * @group internet 96*f7161c34SAndreas Gohr */ 97f8369d7dSTobias Sarnowski function test_cookies(){ 98f8369d7dSTobias Sarnowski $http = new HTTPClient(); 99f8369d7dSTobias Sarnowski $http->get($this->server.'/cookies/set/foo/bar'); 100f8369d7dSTobias Sarnowski $this->assertEquals(array('foo' => 'bar'), $http->cookies); 101f8369d7dSTobias Sarnowski $data = $http->get($this->server.'/cookies'); 102f8369d7dSTobias Sarnowski $this->assertFalse($data === false, 'HTTP response'); 103f8369d7dSTobias Sarnowski $resp = json_decode($data, true); 104f8369d7dSTobias Sarnowski $this->assertTrue(is_array($resp), 'JSON response'); 105f8369d7dSTobias Sarnowski $this->assertArrayHasKey('cookies',$resp); 106f8369d7dSTobias Sarnowski $this->assertEquals(array('foo'=>'bar'), $resp['cookies']); 107f8369d7dSTobias Sarnowski } 108f8369d7dSTobias Sarnowski 109*f7161c34SAndreas Gohr /** 110*f7161c34SAndreas Gohr * @group internet 111*f7161c34SAndreas Gohr */ 112f8369d7dSTobias Sarnowski function test_teapot(){ 113f8369d7dSTobias Sarnowski $http = new HTTPClient(); 114f8369d7dSTobias Sarnowski $data = $http->get($this->server.'/status/418'); 115f8369d7dSTobias Sarnowski $this->assertTrue($data === false, 'HTTP response'); 116f8369d7dSTobias Sarnowski $this->assertEquals(418,$http->status); 117f8369d7dSTobias Sarnowski } 118f8369d7dSTobias Sarnowski 119*f7161c34SAndreas Gohr /** 120*f7161c34SAndreas Gohr * @group internet 121*f7161c34SAndreas Gohr */ 122f8369d7dSTobias Sarnowski function test_maxbody(){ 123f8369d7dSTobias Sarnowski $http = new HTTPClient(); 124f8369d7dSTobias Sarnowski $http->max_bodysize = 250; 125f8369d7dSTobias Sarnowski $data = $http->get($this->server.'/stream/30'); 126f8369d7dSTobias Sarnowski $this->assertTrue($data === false, 'HTTP response'); 127f8369d7dSTobias Sarnowski } 128f8369d7dSTobias Sarnowski 129*f7161c34SAndreas Gohr /** 130*f7161c34SAndreas Gohr * @group internet 131*f7161c34SAndreas Gohr */ 132f8369d7dSTobias Sarnowski function test_basicauth(){ 133f8369d7dSTobias Sarnowski $http = new HTTPClient(); 134f8369d7dSTobias Sarnowski $http->user = 'user'; 135f8369d7dSTobias Sarnowski $http->pass = 'pass'; 136f8369d7dSTobias Sarnowski $data = $http->get($this->server.'/basic-auth/user/pass'); 137f8369d7dSTobias Sarnowski $this->assertFalse($data === false, 'HTTP response'); 138f8369d7dSTobias Sarnowski $resp = json_decode($data, true); 139f8369d7dSTobias Sarnowski $this->assertTrue(is_array($resp), 'JSON response'); 140f8369d7dSTobias Sarnowski $this->assertEquals(array('authenticated'=>true,'user'=>'user'), $resp); 141f8369d7dSTobias Sarnowski } 142f8369d7dSTobias Sarnowski 143*f7161c34SAndreas Gohr /** 144*f7161c34SAndreas Gohr * @group internet 145*f7161c34SAndreas Gohr */ 146f8369d7dSTobias Sarnowski function test_basicauthfail(){ 147f8369d7dSTobias Sarnowski $http = new HTTPClient(); 148f8369d7dSTobias Sarnowski $http->user = 'user'; 149f8369d7dSTobias Sarnowski $http->pass = 'invalid'; 150f8369d7dSTobias Sarnowski $data = $http->get($this->server.'/basic-auth/user/pass'); 151f8369d7dSTobias Sarnowski $this->assertTrue($data === false, 'HTTP response'); 152f8369d7dSTobias Sarnowski $this->assertEquals(401,$http->status); 153f8369d7dSTobias Sarnowski } 154f8369d7dSTobias Sarnowski 155*f7161c34SAndreas Gohr /** 156*f7161c34SAndreas Gohr * @group internet 157*f7161c34SAndreas Gohr */ 158f8369d7dSTobias Sarnowski function test_timeout(){ 159f8369d7dSTobias Sarnowski $http = new HTTPClient(); 160f8369d7dSTobias Sarnowski $http->timeout = 5; 161f8369d7dSTobias Sarnowski $data = $http->get($this->server.'/delay/10'); 162f8369d7dSTobias Sarnowski $this->assertTrue($data === false, 'HTTP response'); 163f8369d7dSTobias Sarnowski $this->assertEquals(-100,$http->status); 164f8369d7dSTobias Sarnowski } 165f8369d7dSTobias Sarnowski 166*f7161c34SAndreas Gohr /** 167*f7161c34SAndreas Gohr * @group internet 168*f7161c34SAndreas Gohr */ 169f8369d7dSTobias Sarnowski function test_headers(){ 170f8369d7dSTobias Sarnowski $http = new HTTPClient(); 171f8369d7dSTobias Sarnowski $data = $http->get($this->server.'/response-headers?baz=&foo=bar'); 172f8369d7dSTobias Sarnowski $this->assertFalse($data === false, 'HTTP response'); 173f8369d7dSTobias Sarnowski $resp = json_decode($data, true); 174f8369d7dSTobias Sarnowski $this->assertTrue(is_array($resp), 'JSON response'); 175f8369d7dSTobias Sarnowski $this->assertArrayHasKey('baz',$http->resp_headers); 176f8369d7dSTobias Sarnowski $this->assertArrayHasKey('foo',$http->resp_headers); 177f8369d7dSTobias Sarnowski $this->assertEquals('bar',$http->resp_headers['foo']); 178f8369d7dSTobias Sarnowski } 179f8369d7dSTobias Sarnowski} 180f8369d7dSTobias Sarnowski//Setup VIM: ex: et ts=4 : 181