xref: /dokuwiki/_test/tests/inc/httpclient_http.test.php (revision 769b429a77368df14e3753f624466f658e971df6)
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        /* the current implementation will read in max_bodysize blocks,
131           and aborts if the response is larger, thus a limit of 2*max_bodysize */
132        $this->assertLessThanOrEqual(500,strlen($data));
133    }
134
135    /**
136     * @group internet
137     */
138    function test_basicauth(){
139        $http = new HTTPClient();
140        $http->user = 'user';
141        $http->pass = 'pass';
142        $data = $http->get($this->server.'/basic-auth/user/pass');
143        $this->assertFalse($data === false, 'HTTP response');
144        $resp = json_decode($data, true);
145        $this->assertTrue(is_array($resp), 'JSON response');
146        $this->assertEquals(array('authenticated'=>true,'user'=>'user'), $resp);
147    }
148
149    /**
150     * @group internet
151     */
152    function test_basicauthfail(){
153        $http = new HTTPClient();
154        $http->user = 'user';
155        $http->pass = 'invalid';
156        $data = $http->get($this->server.'/basic-auth/user/pass');
157        $this->assertTrue($data === false, 'HTTP response');
158        $this->assertEquals(401,$http->status);
159    }
160
161    /**
162     * @group internet
163     */
164    function test_timeout(){
165        $http = new HTTPClient();
166        $http->timeout = 5;
167        $data = $http->get($this->server.'/delay/10');
168        $this->assertTrue($data === false, 'HTTP response');
169        $this->assertEquals(-100,$http->status);
170    }
171
172    /**
173     * @group internet
174     */
175    function test_headers(){
176        $http = new HTTPClient();
177        $data = $http->get($this->server.'/response-headers?baz=&foo=bar');
178        $this->assertFalse($data === false, 'HTTP response');
179        $resp = json_decode($data, true);
180        $this->assertTrue(is_array($resp), 'JSON response');
181        $this->assertArrayHasKey('baz',$http->resp_headers);
182        $this->assertArrayHasKey('foo',$http->resp_headers);
183        $this->assertEquals('bar',$http->resp_headers['foo']);
184    }
185
186    /**
187     * @group internet
188     */
189    function test_chunked(){
190        $http = new HTTPClient();
191        $data = $http->get('http://whoopdedo.org/cgi-bin/chunked/2550');
192        $this->assertFalse($data === false, 'HTTP response');
193        $this->assertEquals(2550,strlen($data));
194    }
195}
196//Setup VIM: ex: et ts=4 :
197