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