xref: /dokuwiki/_test/tests/inc/httpclient_http.test.php (revision e260f93b6cea05bc39bbd77b9db5bdc0c2c424bf)
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    }
128
129    /**
130     * @group internet
131     */
132    function test_basicauth(){
133        $http = new HTTPClient();
134        $http->user = 'user';
135        $http->pass = 'pass';
136        $data = $http->get($this->server.'/basic-auth/user/pass');
137        $this->assertFalse($data === false, 'HTTP response');
138        $resp = json_decode($data, true);
139        $this->assertTrue(is_array($resp), 'JSON response');
140        $this->assertEquals(array('authenticated'=>true,'user'=>'user'), $resp);
141    }
142
143    /**
144     * @group internet
145     */
146    function test_basicauthfail(){
147        $http = new HTTPClient();
148        $http->user = 'user';
149        $http->pass = 'invalid';
150        $data = $http->get($this->server.'/basic-auth/user/pass');
151        $this->assertTrue($data === false, 'HTTP response');
152        $this->assertEquals(401,$http->status);
153    }
154
155    /**
156     * @group internet
157     */
158    function test_timeout(){
159        $http = new HTTPClient();
160        $http->timeout = 5;
161        $data = $http->get($this->server.'/delay/10');
162        $this->assertTrue($data === false, 'HTTP response');
163        $this->assertEquals(-100,$http->status);
164    }
165
166    /**
167     * @group internet
168     */
169    function test_headers(){
170        $http = new HTTPClient();
171        $data = $http->get($this->server.'/response-headers?baz=&foo=bar');
172        $this->assertFalse($data === false, 'HTTP response');
173        $resp = json_decode($data, true);
174        $this->assertTrue(is_array($resp), 'JSON response');
175        $this->assertArrayHasKey('baz',$http->resp_headers);
176        $this->assertArrayHasKey('foo',$http->resp_headers);
177        $this->assertEquals('bar',$http->resp_headers['foo']);
178    }
179}
180//Setup VIM: ex: et ts=4 :
181