Make WordPress Core

Ticket #4779: test_http_api.5.3.php

File test_http_api.5.3.php, 12.8 KB (added by jacobsantos, 15 years ago)

Test cases for HTTP API

Line 
1<?php
2
3if( !class_exists('WP_Http') )
4        require ABSPATH . WPINC .'/http.php';
5
6class WPTestHTTPAPI extends WPTestCase {
7
8        /**
9         * Tests whether the function returns the same object.
10         *
11         * This test will be skewed on PHP5, because objects are passed by reference
12         * anyway.
13         */
14        function test_should_return_WP_HTTP_Base_object_by_ref() {
15                $expected = _wp_http_get_object();
16                $actual = _wp_http_get_object();
17
18                $this->assertSame($expected, $actual);
19        }
20
21        function test_fopen_transport_object_test_if_available() {
22                if( ini_get('allow_url_fopen') != true ) {
23                        $this->markTestSkipped('PHP allow_url_fopen directive is not enabled.');
24                        return;
25                }
26
27                $this->assertTrue(WP_Http_Fopen::test());
28        }
29
30        function test_fsockopen_transport_not_available_and_function_not_exists() {
31                if( false === strpos(ini_get('disable_functions'), 'fsockopen') ) {
32                        $this->markTestSkipped('Fsockopen() is not part of the disabled functions list.');
33                        return;
34                }
35
36                $this->assertFalse( function_exists('fsockopen') );
37        }
38
39        /**
40         * @covers WP_Http_Fsockopen::request
41         * @covers WP_Http_Fsockopen::test
42         */
43        function test_fsockopen_transport_expects_200_wordpress_org_page_without_redirect() {
44                if( false === WP_Http_Fsockopen::test() ) {
45                        $this->markTestSkipped('Fsockopen() is not available.');
46                        return;
47                }
48
49                $transport = new WP_Http_Fsockopen();
50
51                $response = $transport->request('http://wordpress.org', array('method' => 'HEAD'), null, null);
52
53                $this->assertFalse(is_wp_error($response));
54
55                $expected = 200;
56
57                $actual = $response['response']['code'];
58
59                $this->assertEquals($expected, $actual);
60        }
61
62        /**
63         * @covers WP_Http_Fsockopen::request
64         * @covers WP_Http_Fsockopen::test
65         */
66        function test_fsockopen_transport_expects_200_wordpress_org_page_with_redirect() {
67                if( false === WP_Http_Fsockopen::test() ) {
68                        $this->markTestSkipped('Fsockopen() is not available.');
69                        return;
70                }
71
72                $transport = new WP_Http_Fsockopen();
73
74                $response = $transport->request('http://www.wordpress.org', array('method' => 'HEAD'), null, null);
75
76                $this->assertFalse(is_wp_error($response));
77
78                $expected = 200;
79
80                $actual = $response['response']['code'];
81
82                $this->assertEquals($expected, $actual);
83        }
84
85        /**
86         * @covers WP_Http_Fopen::request
87         * @covers WP_Http_Fopen::test
88         */
89        function test_fopen_transport_expects_200_wordpress_org_page_without_redirect() {
90                if( false === WP_Http_Fopen::test() ) {
91                        $this->markTestSkipped('Fopen() is not available for http requests.');
92                        return;
93                }
94
95                $transport = new WP_Http_Fopen();
96
97                $response = $transport->request('http://wordpress.org', array('method' => 'GET'), null, null);
98
99                $this->assertFalse(is_wp_error($response));
100
101                $expected = 200;
102
103                $actual = $response['response']['code'];
104
105                $this->assertEquals($expected, $actual);
106        }
107
108        /**
109         * @covers WP_Http_Fopen::request
110         * @covers WP_Http_Fopen::test
111         */
112        function test_fopen_transport_expects_200_wordpress_org_page_with_redirect() {
113                if( false === WP_Http_Fopen::test() ) {
114                        $this->markTestSkipped('Fopen() is not available for http requests.');
115                        return;
116                }
117
118                $transport = new WP_Http_Fopen();
119
120                $response = $transport->request('http://www.wordpress.org', array('method' => 'GET'), null, null);
121
122                $this->assertFalse(is_wp_error($response));
123
124                $expected = 200;
125
126                $actual = $response['response']['code'];
127
128                $this->assertEquals($expected, $actual);
129        }
130
131        /**
132         * @covers WP_Http_Streams::request
133         * @covers WP_Http_Streams::test
134         */
135        function test_streams_transport_expects_200_wordpress_org_page_without_redirect() {
136                if( false === WP_Http_Streams::test() ) {
137                        $this->markTestSkipped('Streams is not available.');
138                        return;
139                }
140
141                $transport = new WP_Http_Streams();
142
143                $response = $transport->request('http://wordpress.org', array('method' => 'GET'), null, null);
144
145                $this->assertFalse(is_wp_error($response));
146
147                $expected = 200;
148
149                $actual = $response['response']['code'];
150
151                $this->assertEquals($expected, $actual);
152        }
153
154        /**
155         * @covers WP_Http_Streams::request
156         * @covers WP_Http_Streams::test
157         */
158        function test_streams_transport_expects_200_wordpress_org_page_with_redirect() {
159                if( false === WP_Http_Streams::test() ) {
160                        $this->markTestSkipped('Streams is not available.');
161                        return;
162                }
163
164                $transport = new WP_Http_Streams();
165
166                $response = $transport->request('http://www.wordpress.org', array('method' => 'GET'), null, null);
167
168                $this->assertFalse(is_wp_error($response));
169
170                $expected = 200;
171
172                $actual = $response['response']['code'];
173
174                $this->assertEquals($expected, $actual);
175        }
176
177        /**
178         * @covers WP_Http_ExtHTTP::request
179         * @covers WP_Http_ExtHTTP::test
180         */
181        function test_exthttp_transport_expects_200_wordpress_org_page_without_redirect() {
182                if( false === WP_Http_ExtHTTP::test() ) {
183                        $this->markTestSkipped('The HTTP extension is not available.');
184                        return;
185                }
186
187                $transport = new WP_Http_ExtHTTP();
188
189                $response = $transport->request('http://wordpress.org', array('method' => 'GET'), null, null);
190
191                $this->assertFalse(is_wp_error($response));
192
193                $expected = 200;
194
195                $actual = $response['response']['code'];
196
197                $this->assertEquals($expected, $actual);
198        }
199
200        /**
201         * @covers WP_Http_ExtHTTP::request
202         * @covers WP_Http_ExtHTTP::test
203         */
204        function test_exthttp_transport_expects_200_wordpress_org_page_with_redirect() {
205                if( false === WP_Http_ExtHTTP::test() ) {
206                        $this->markTestSkipped('The HTTP extension is not available.');
207                        return;
208                }
209
210                $transport = new WP_Http_ExtHTTP();
211
212                $response = $transport->request('http://www.wordpress.org', array('method' => 'GET'), null, null);
213
214                $this->assertFalse(is_wp_error($response));
215
216                $expected = 200;
217
218                $actual = $response['response']['code'];
219
220                $this->assertEquals($expected, $actual);
221        }
222
223        function test_wp_remote_request_function_expects_200_response_from_wordpress_org_without_redirect() {
224                $response = wp_remote_request('http://wordpress.org', array('method' => 'GET'), null, null);
225
226                $this->assertFalse(is_wp_error($response));
227
228                $expected = 200;
229
230                $actual = $response['response']['code'];
231
232                $this->assertEquals($expected, $actual);
233        }
234
235        function test_wp_remote_request_function_expects_200_response_from_wordpress_org_with_redirect() {
236                $response = wp_remote_request('http://www.wordpress.org', array('method' => 'GET'), null, null);
237
238                $this->assertFalse(is_wp_error($response));
239
240                $expected = 200;
241
242                $actual = $response['response']['code'];
243
244                $this->assertEquals($expected, $actual);
245        }
246
247        /**
248         * @covers WP_Http_Fopen::request
249         */
250        function test_fopen_transport_and_test_response_with_ssl() {
251                if( !function_exists('openssl_open') ) {
252                        $this->markTestSkipped('The OpenSSL extension is not available.');
253                        return;
254                }
255
256                $transport = new WP_Http_Fopen();
257                $response = $transport->request('https://www.google.com', array('method' => 'GET'), null, null);
258
259                $expected = 200;
260                $actual = $response['response']['code'];
261
262                $this->assertEquals($expected, $actual);
263        }
264
265        /**
266         * @covers WP_Http_Streams::request
267         */
268        function test_streams_transport_and_test_response_with_ssl() {
269                if( !function_exists('openssl_open') ) {
270                        $this->markTestSkipped('The OpenSSL extension is not available.');
271                        return;
272                }
273
274                $transport = new WP_Http_Streams();
275                $response = $transport->request('https://www.google.com', array('method' => 'GET'), null, null);
276
277                $expected = 200;
278                $actual = $response['response']['code'];
279
280                $this->assertEquals($expected, $actual);
281        }
282
283        /**
284         * @covers WP_Http_Streams::request
285         */
286        function test_exthttp_transport_and_test_response_with_ssl() {
287                if( !function_exists('openssl_open') ) {
288                        $this->markTestSkipped('The OpenSSL extension is not available.');
289                        return;
290                }
291
292                $transport = new WP_Http_Streams();
293                $response = $transport->request('https://www.google.com', array('method' => 'GET'), null, null);
294
295                $expected = 200;
296                $actual = $response['response']['code'];
297
298                $this->assertEquals($expected, $actual);
299        }
300
301        /**
302         * @covers WP_Http_Streams::request
303         */
304        function test_fsockopen_transport_and_test_response_with_ssl() {
305                if( !function_exists('openssl_open') ) {
306                        $this->markTestSkipped('The OpenSSL extension is not available.');
307                        return;
308                }
309
310                $transport = new WP_Http_Streams();
311                $response = $transport->request('https://www.google.com', array('method' => 'GET'), null, null);
312
313                $expected = 200;
314                $actual = $response['response']['code'];
315
316                $this->assertEquals($expected, $actual);
317        }
318
319        /**
320         * @covers WP_Http::processHeaders
321         */
322        function test_wp_http_process_headers() {
323                $headers = WP_Http::processHeaders("Content-type: text/html\r\nConnection: close\r\n");
324
325                $this->assertEquals(2, count($headers['headers']));
326
327                $this->assertTrue( isset($headers['headers']['content-type']) );
328                $this->assertEquals('text/html', $headers['headers']['content-type']);
329
330                $this->assertTrue( isset($headers['headers']['connection']) );
331                $this->assertEquals('close', $headers['headers']['connection']);
332        }
333
334        /**
335         * @covers WP_Http::processHeaders
336         */
337        function test_wp_http_base_process_headers_and_get_response() {
338                $headers = WP_Http::processHeaders("HTTP/1.1 200 OK\r\nContent-type: text/html\r\nConnection: close\r\n");
339
340                $this->assertEquals(2, count($headers['headers']));
341                $this->assertEquals(2, count($headers['response']));
342
343                $this->assertTrue( isset($headers['headers']['content-type']) );
344                $this->assertEquals('text/html', $headers['headers']['content-type']);
345
346                $this->assertTrue( isset($headers['headers']['connection']) );
347                $this->assertEquals('close', $headers['headers']['connection']);
348
349                $this->assertEquals(200, $headers['response']['code']);
350                $this->assertEquals('OK', $headers['response']['message']);
351        }
352
353        /**
354         * @covers WP_Http::processResponse
355         */
356        function test_process_response_get_values_raw_response_for_body_headers_and_response() {
357                $response = WP_Http::processResponse("HTTP/1.1 200 OK\r\nLocation: http://www.example.org\r\nContent-type: text/html\r\nConnection: close\r\n\r\nTest Body");
358
359                $this->assertTrue( is_array($response) );
360
361                $this->assertEquals('Test Body', $response['body']);
362
363                $this->assertEquals("HTTP/1.1 200 OK\r\nLocation: http://www.example.org\r\nContent-type: text/html\r\nConnection: close", $response['headers']);
364        }
365
366        function test_process_response_get_values_using_helper_functions_and_all_headers() {
367                $arrResponse = WP_Http::processResponse("HTTP/1.1 200 OK\r\nLocation: http://www.example.org\r\nContent-type: text/html\r\nConnection: close\r\n\r\nTest Body");
368                $headers = WP_Http::processHeaders($arrResponse['headers']);
369                $response = array('body' => $arrResponse['body'], 'headers' => $headers['headers'], 'response' => $headers['response']);
370
371                $this->assertTrue( is_array($response) );
372
373                $this->assertEquals('Test Body', wp_remote_retrieve_body($response));
374
375                $headers = wp_remote_retrieve_headers($response);
376
377                $this->assertEquals('http://www.example.org', $headers['location']);
378                $this->assertEquals('text/html', $headers['content-type']);
379                $this->assertEquals('close', $headers['connection']);
380
381                $this->assertEquals(200, wp_remote_retrieve_response_code($response));
382                $this->assertEquals('OK', wp_remote_retrieve_response_message($response));
383        }
384
385        function test_process_response_get_values_using_helper_functions_and_by_single_headers() {
386                $arrResponse = WP_Http::processResponse("HTTP/1.1 200 OK\r\nLocation: http://www.example.org\r\nContent-type: text/html\r\nConnection: close\r\n\r\nTest Body");
387                $headers = WP_Http::processHeaders($arrResponse['headers']);
388                $response = array('body' => $arrResponse['body'], 'headers' => $headers['headers'], 'response' => $headers['response']);
389
390                $this->assertTrue( is_array($response) );
391
392                $this->assertEquals('Test Body', wp_remote_retrieve_body($response));
393
394                $this->assertEquals('http://www.example.org', wp_remote_retrieve_header($response, 'location'));
395                $this->assertEquals('text/html', wp_remote_retrieve_header($response, 'content-type'));
396                $this->assertEquals('close', wp_remote_retrieve_header($response, 'connection'));
397
398                $this->assertEquals(200, wp_remote_retrieve_response_code($response));
399                $this->assertEquals('OK', wp_remote_retrieve_response_message($response));
400        }
401
402        function test_isRedirect_should_be_a_redirect() {
403                $headers = array(
404                        'content-type' => 'text/html; charset=utf-8',
405                        'location' => 'http://wordpress.org/',
406                        'date' => 'Thu, 31 Jul 2008 23:09:15 GMT',
407                        'server' => 'LiteSpeed',
408                        'connection' => 'close',
409                );
410
411                $this->assertTrue(WP_Http::isRedirect($headers));
412        }
413
414        function test_isRedirect_should_not_be_a_redirect() {
415                $headers = array(
416                        'content-type' => 'text/html; charset=utf-8',
417                        'date' => 'Thu, 31 Jul 2008 23:09:15 GMT',
418                        'server' => 'LiteSpeed',
419                        'connection' => 'close',
420                );
421
422                $this->assertFalse(WP_Http::isRedirect($headers));
423        }
424}
425
426?>