forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.php
More file actions
484 lines (402 loc) · 13.1 KB
/
base.php
File metadata and controls
484 lines (402 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<?php
/**
* Note, When running these tests, remember that some things are done differently
* based on safe_mode. You can run the test in safe_mode like such:
*
* phpunit -d safe_mode=on --group http
*
* You may also need `-d safe_mode_gid=1` to relax the safe_mode checks to allow
* inclusion of PEAR.
*
* The WP_HTTP tests require a class-http.php file of r17550 or later.
*/
abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
// You can use your own version of data/WPHTTP-testcase-redirection-script.php here.
var $redirection_script = 'http://api.wordpress.org/core/tests/1.0/redirection.php';
var $file_stream_url = 'http://s.w.org/screenshots/3.9/dashboard.png';
protected $http_request_args;
/**
* Mark test as skipped if the HTTP request times out.
*/
function skipTestOnTimeout( $response ) {
if ( ! is_wp_error( $response ) ) {
return;
}
if ( 'connect() timed out!' === $response->get_error_message() ) {
$this->markTestSkipped( 'HTTP timeout' );
}
if ( false !== strpos( $response->get_error_message(), 'timed out after' ) ) {
$this->markTestSkipped( 'HTTP timeout' );
}
if ( 0 === strpos( $response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80' ) ) {
$this->markTestSkipped( 'HTTP timeout' );
}
}
function setUp() {
parent::setUp();
if ( is_callable( array( 'WP_Http', '_getTransport' ) ) ) {
$this->markTestSkipped( 'The WP_Http tests require a class-http.php file of r17550 or later.' );
return;
}
$class = 'WP_Http_' . ucfirst( $this->transport );
if ( ! call_user_func( array( $class, 'test' ) ) ) {
$this->markTestSkipped( sprintf( 'The transport %s is not supported on this system', $this->transport ) );
}
// Disable all transports aside from this one.
foreach ( array( 'curl', 'streams', 'fsockopen' ) as $t ) {
remove_filter( "use_{$t}_transport", '__return_false' ); // Just strip them all
if ( $t != $this->transport ) {
add_filter( "use_{$t}_transport", '__return_false' ); // and add it back if need be..
}
}
}
function tearDown() {
foreach ( array( 'curl', 'streams', 'fsockopen' ) as $t ) {
remove_filter( "use_{$t}_transport", '__return_false' );
}
parent::tearDown();
}
function filter_http_request_args( array $args ) {
$this->http_request_args = $args;
return $args;
}
function test_redirect_on_301() {
// 5 : 5 & 301
$res = wp_remote_request( $this->redirection_script . '?code=301&rt=' . 5, array( 'redirection' => 5 ) );
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$this->assertEquals( 200, (int) $res['response']['code'] );
}
function test_redirect_on_302() {
// 5 : 5 & 302
$res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 5 ) );
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$this->assertEquals( 200, (int) $res['response']['code'] );
}
/**
* @ticket 16855
*/
function test_redirect_on_301_no_redirect() {
// 5 > 0 & 301
$res = wp_remote_request( $this->redirection_script . '?code=301&rt=' . 5, array( 'redirection' => 0 ) );
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$this->assertEquals( 301, (int) $res['response']['code'] );
}
/**
* @ticket 16855
*/
function test_redirect_on_302_no_redirect() {
// 5 > 0 & 302
$res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 0 ) );
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$this->assertEquals( 302, (int) $res['response']['code'] );
}
function test_redirections_equal() {
// 5 - 5
$res = wp_remote_request( $this->redirection_script . '?rt=' . 5, array( 'redirection' => 5 ) );
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$this->assertEquals( 200, (int) $res['response']['code'] );
}
function test_no_head_redirections() {
// No redirections on HEAD request:
$res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 1, array( 'method' => 'HEAD' ) );
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$this->assertEquals( 302, (int) $res['response']['code'] );
}
/**
* @ticket 16855
*/
function test_redirect_on_head() {
// Redirections on HEAD request when Requested
$res = wp_remote_request(
$this->redirection_script . '?rt=' . 5,
array(
'redirection' => 5,
'method' => 'HEAD',
)
);
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$this->assertEquals( 200, (int) $res['response']['code'] );
}
function test_redirections_greater() {
// 10 > 5
$res = wp_remote_request( $this->redirection_script . '?rt=' . 10, array( 'redirection' => 5 ) );
$this->skipTestOnTimeout( $res );
$this->assertWPError( $res );
}
function test_redirections_greater_edgecase() {
// 6 > 5 (close edgecase)
$res = wp_remote_request( $this->redirection_script . '?rt=' . 6, array( 'redirection' => 5 ) );
$this->skipTestOnTimeout( $res );
$this->assertWPError( $res );
}
function test_redirections_less_edgecase() {
// 4 < 5 (close edgecase)
$res = wp_remote_request( $this->redirection_script . '?rt=' . 4, array( 'redirection' => 5 ) );
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
}
/**
* @ticket 16855
*/
function test_redirections_zero_redirections_specified() {
// 0 redirections asked for, Should return the document?
$res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 0 ) );
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$this->assertEquals( 302, (int) $res['response']['code'] );
}
/**
* Do not redirect on non 3xx status codes
*
* @ticket 16889
*/
function test_location_header_on_201() {
// Prints PASS on initial load, FAIL if the client follows the specified redirection
$res = wp_remote_request( $this->redirection_script . '?201-location=true' );
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$this->assertEquals( 'PASS', $res['body'] );
}
/**
* Test handling of PUT requests on redirects
*
* @ticket 16889
*/
function test_no_redirection_on_PUT() {
$url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?201-location=1';
// Test 301 - POST to POST
$res = wp_remote_request(
$url,
array(
'method' => 'PUT',
'timeout' => 30,
)
);
$this->skipTestOnTimeout( $res );
$this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
$this->assertTrue( ! empty( $res['headers']['location'] ) );
}
/**
* @ticket 11888
*/
function test_send_headers() {
// Test that the headers sent are received by the server
$headers = array(
'test1' => 'test',
'test2' => 0,
'test3' => '',
);
$res = wp_remote_request( $this->redirection_script . '?header-check', array( 'headers' => $headers ) );
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$headers = array();
foreach ( explode( "\n", $res['body'] ) as $key => $value ) {
if ( empty( $value ) ) {
continue;
}
$parts = explode( ':', $value, 2 );
unset( $headers[ $key ] );
$headers[ $parts[0] ] = $parts[1];
}
$this->assertTrue( isset( $headers['test1'] ) && 'test' == $headers['test1'] );
$this->assertTrue( isset( $headers['test2'] ) && '0' === $headers['test2'] );
// cURL/HTTP Extension Note: Will never pass, cURL does not pass headers with an empty value.
// Should it be that empty headers with empty values are NOT sent?
//$this->assertTrue( isset($headers['test3']) && '' === $headers['test3'] );
}
function test_file_stream() {
$url = $this->file_stream_url;
$size = 153204;
$res = wp_remote_request(
$url,
array(
'stream' => true,
'timeout' => 30,
)
); //Auto generate the filename.
// Cleanup before we assert, as it'll return early.
if ( ! is_wp_error( $res ) ) {
$filesize = filesize( $res['filename'] );
unlink( $res['filename'] );
}
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$this->assertEquals( '', $res['body'] ); // The body should be empty.
$this->assertEquals( $size, $res['headers']['content-length'] ); // Check the headers are returned (and the size is the same..)
$this->assertEquals( $size, $filesize ); // Check that the file is written to disk correctly without any extra characters
$this->assertStringStartsWith( get_temp_dir(), $res['filename'] ); // Check it's saving within the temp dir
}
/**
* @ticket 26726
*/
function test_file_stream_limited_size() {
$url = $this->file_stream_url;
$size = 10000;
$res = wp_remote_request(
$url,
array(
'stream' => true,
'timeout' => 30,
'limit_response_size' => $size,
)
); //Auto generate the filename.
// Cleanup before we assert, as it'll return early.
if ( ! is_wp_error( $res ) ) {
$filesize = filesize( $res['filename'] );
unlink( $res['filename'] );
}
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$this->assertEquals( $size, $filesize ); // Check that the file is written to disk correctly without any extra characters
}
/**
* Tests Limiting the response size when returning strings
*
* @ticket 31172
*/
function test_request_limited_size() {
$url = $this->file_stream_url;
$size = 10000;
$res = wp_remote_request(
$url,
array(
'timeout' => 30,
'limit_response_size' => $size,
)
);
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
$this->assertEquals( $size, strlen( $res['body'] ) );
}
/**
* Test POST redirection methods
*
* @dataProvider data_post_redirect_to_method_300
*
* @ticket 17588
*/
function test_post_redirect_to_method_300( $response_code, $method ) {
$url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?post-redirect-to-method=1';
$res = wp_remote_post( add_query_arg( 'response_code', $response_code, $url ), array( 'timeout' => 30 ) );
$this->skipTestOnTimeout( $res );
$this->assertEquals( $method, wp_remote_retrieve_body( $res ) );
}
public function data_post_redirect_to_method_300() {
return array(
// Test 300 - POST to POST
array(
300,
'POST',
),
// Test 301 - POST to POST
array(
301,
'POST',
),
// Test 302 - POST to GET
array(
302,
'GET',
),
// Test 303 - POST to GET
array(
303,
'GET',
),
);
}
/**
* Test HTTP Requests using an IP url, with a HOST header specified
*
* @ticket 24182
*/
function test_ip_url_with_host_header() {
$ip = gethostbyname( 'api.wordpress.org' );
$url = 'http://' . $ip . '/core/tests/1.0/redirection.php?print-pass=1';
$args = array(
'headers' => array(
'Host' => 'api.wordpress.org',
),
'timeout' => 30,
'redirection' => 0,
);
$res = wp_remote_get( $url, $args );
$this->skipTestOnTimeout( $res );
$this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
}
/**
* Test HTTP requests where SSL verification is disabled but the CA bundle is still populated
*
* @ticket 33978
*/
function test_https_url_without_ssl_verification() {
$url = 'https://wordpress.org/';
$args = array(
'sslverify' => false,
);
add_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) );
$res = wp_remote_head( $url, $args );
remove_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) );
$this->skipTestOnTimeout( $res );
$this->assertNotEmpty( $this->http_request_args['sslcertificates'] );
$this->assertNotWPError( $res );
}
/**
* Test HTTP Redirects with multiple Location headers specified
*
* @ticket 16890
*/
function test_multiple_location_headers() {
$url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?multiple-location-headers=1';
$res = wp_remote_head( $url, array( 'timeout' => 30 ) );
$this->skipTestOnTimeout( $res );
$this->assertInternalType( 'array', wp_remote_retrieve_header( $res, 'location' ) );
$this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) );
$res = wp_remote_get( $url, array( 'timeout' => 30 ) );
$this->skipTestOnTimeout( $res );
$this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
}
/**
* Test HTTP Cookie handling
*
* @ticket 21182
*/
function test_cookie_handling() {
$url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?cookie-test=1';
$res = wp_remote_get( $url );
$this->skipTestOnTimeout( $res );
$this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
}
/**
* Test if HTTPS support works
*
* @group ssl
* @ticket 25007
*/
function test_ssl() {
if ( ! wp_http_supports( array( 'ssl' ) ) ) {
$this->fail( 'This installation of PHP does not support SSL' );
}
$res = wp_remote_get( 'https://wordpress.org/' );
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
}
/**
* @ticket 37733
*/
function test_url_with_double_slashes_path() {
$url = $this->redirection_script . '?rt=' . 0;
$path = parse_url( $url, PHP_URL_PATH );
$url = str_replace( $path, '/' . $path, $url );
$res = wp_remote_request( $url );
$this->skipTestOnTimeout( $res );
$this->assertNotWPError( $res );
}
}