1<?php 2 3require_once DOKU_INC.'inc/init.php'; 4require_once DOKU_INC.'inc/HTTPClient.php'; 5 6class httpclient_http_test extends DokuWikiTest { 7 protected $server = 'http://httpbin.org'; 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 function test_dget(){ 20 $http = new HTTPClient(); 21 $data = $http->dget($this->server.'/get',array('foo'=>'bar')); 22 $this->assertFalse($data === false, 'HTTP response'); 23 $resp = json_decode($data, true); 24 $this->assertTrue(is_array($resp), 'JSON response'); 25 $this->assertArrayHasKey('args',$resp); 26 $this->assertEquals(array('foo'=>'bar'), $resp['args']); 27 } 28 29 function test_gzip(){ 30 $http = new HTTPClient(); 31 $data = $http->get($this->server.'/gzip'); 32 $this->assertFalse($data === false, 'HTTP response'); 33 $resp = json_decode($data, true); 34 $this->assertTrue(is_array($resp), 'JSON response'); 35 $this->assertArrayHasKey('gzipped',$resp); 36 $this->assertTrue($resp['gzipped']); 37 } 38 39 function test_simplepost(){ 40 $http = new HTTPClient(); 41 $data = $http->post($this->server.'/post',array('foo'=>'bar')); 42 $this->assertFalse($data === false, 'HTTP response'); 43 $resp = json_decode($data, true); 44 $this->assertTrue(is_array($resp), 'JSON response'); 45 $this->assertArrayHasKey('form',$resp); 46 $this->assertEquals(array('foo'=>'bar'), $resp['form']); 47 } 48 49 function test_redirect(){ 50 $http = new HTTPClient(); 51 $data = $http->get($this->server.'/redirect/3'); 52 $this->assertFalse($data === false, 'HTTP response'); 53 $resp = json_decode($data, true); 54 $this->assertTrue(is_array($resp), 'JSON response'); 55 $this->assertArrayHasKey('url',$resp); 56 $this->assertRegExp('/\/get$/', $resp['url']); 57 } 58 59 function test_relredirect(){ 60 $http = new HTTPClient(); 61 $data = $http->get($this->server.'/relative-redirect/3'); 62 $this->assertFalse($data === false, 'HTTP response'); 63 $resp = json_decode($data, true); 64 $this->assertTrue(is_array($resp), 'JSON response'); 65 $this->assertArrayHasKey('url',$resp); 66 $this->assertRegExp('/\/get$/', $resp['url']); 67 } 68 69 function test_redirectfail(){ 70 $http = new HTTPClient(); 71 $data = $http->get($this->server.'/redirect/5'); 72 $this->assertTrue($data === false, 'HTTP response'); 73 $this->assertEquals('Maximum number of redirects exceeded',$http->error); 74 } 75 76 function test_cookies(){ 77 $http = new HTTPClient(); 78 $http->get($this->server.'/cookies/set/foo/bar'); 79 $this->assertEquals(array('foo' => 'bar'), $http->cookies); 80 $data = $http->get($this->server.'/cookies'); 81 $this->assertFalse($data === false, 'HTTP response'); 82 $resp = json_decode($data, true); 83 $this->assertTrue(is_array($resp), 'JSON response'); 84 $this->assertArrayHasKey('cookies',$resp); 85 $this->assertEquals(array('foo'=>'bar'), $resp['cookies']); 86 } 87 88 function test_teapot(){ 89 $http = new HTTPClient(); 90 $data = $http->get($this->server.'/status/418'); 91 $this->assertTrue($data === false, 'HTTP response'); 92 $this->assertEquals(418,$http->status); 93 } 94 95 function test_maxbody(){ 96 $http = new HTTPClient(); 97 $http->max_bodysize = 250; 98 $data = $http->get($this->server.'/stream/30'); 99 $this->assertTrue($data === false, 'HTTP response'); 100 } 101 102 function test_basicauth(){ 103 $http = new HTTPClient(); 104 $http->user = 'user'; 105 $http->pass = 'pass'; 106 $data = $http->get($this->server.'/basic-auth/user/pass'); 107 $this->assertFalse($data === false, 'HTTP response'); 108 $resp = json_decode($data, true); 109 $this->assertTrue(is_array($resp), 'JSON response'); 110 $this->assertEquals(array('authenticated'=>true,'user'=>'user'), $resp); 111 } 112 113 function test_basicauthfail(){ 114 $http = new HTTPClient(); 115 $http->user = 'user'; 116 $http->pass = 'invalid'; 117 $data = $http->get($this->server.'/basic-auth/user/pass'); 118 $this->assertTrue($data === false, 'HTTP response'); 119 $this->assertEquals(401,$http->status); 120 } 121 122 function test_timeout(){ 123 $http = new HTTPClient(); 124 $http->timeout = 5; 125 $data = $http->get($this->server.'/delay/10'); 126 $this->assertTrue($data === false, 'HTTP response'); 127 $this->assertEquals(-100,$http->status); 128 } 129 130 function test_headers(){ 131 $http = new HTTPClient(); 132 $data = $http->get($this->server.'/response-headers?baz=&foo=bar'); 133 $this->assertFalse($data === false, 'HTTP response'); 134 $resp = json_decode($data, true); 135 $this->assertTrue(is_array($resp), 'JSON response'); 136 $this->assertArrayHasKey('baz',$http->resp_headers); 137 $this->assertArrayHasKey('foo',$http->resp_headers); 138 $this->assertEquals('bar',$http->resp_headers['foo']); 139 } 140} 141//Setup VIM: ex: et ts=4 : 142