Ticket #40281: 40281.patch
File 40281.patch, 75.6 KB (added by , 8 years ago) |
---|
-
base.php
10 10 * 11 11 * The WP_HTTP tests require a class-http.php file of r17550 or later. 12 12 */ 13 abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase { 14 // You can use your own version of data/WPHTTP-testcase-redirection-script.php here. 15 var $redirection_script = 'http://api.wordpress.org/core/tests/1.0/redirection.php'; 16 var $fileStreamUrl = 'http://s.w.org/screenshots/3.9/dashboard.png'; 13 abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase 14 { 15 16 /** 17 * Redirection Script. 18 * You can use your own version of data/WPHTTP-testcase-redirection-script.php here. 19 * 20 * (default value: 'http://api.wordpress.org/core/tests/1.0/redirection.php') 21 * 22 * @var string 23 * @access public 24 */ 25 var $redirection_script = 'http://api.wordpress.org/core/tests/1.0/redirection.php'; 26 27 /** 28 * File Stream URL 29 * 30 * (default value: 'http://s.w.org/screenshots/3.9/dashboard.png') 31 * 32 * @var string 33 * @access public 34 */ 35 var $fileStreamUrl = 'http://s.w.org/screenshots/3.9/dashboard.png'; 36 37 /** 38 * HTTP Request Arguments. 39 * 40 * @var mixed 41 * @access protected 42 */ 43 protected $http_request_args; 17 44 18 protected $http_request_args; 45 /** 46 * Mark test as skipped if the HTTP request times out. 47 * 48 * @access public 49 * @param mixed $response Response. 50 * @return void 51 */ 52 function skipTestOnTimeout( $response ) 53 { 54 if(! is_wp_error($response) ) { 55 return; 56 } 57 if ('connect() timed out!' === $response->get_error_message() ) { 58 $this->markTestSkipped('HTTP timeout'); 59 } 19 60 20 /** 21 * Mark test as skipped if the HTTP request times out 22 */ 23 function skipTestOnTimeout( $response ) { 24 if( ! is_wp_error( $response ) ){ 25 return; 26 } 27 if ( 'connect() timed out!' === $response->get_error_message() ){ 28 $this->markTestSkipped( 'HTTP timeout' ); 29 } 61 if (0 === strpos($response->get_error_message(), 'Operation timed out after') ) { 62 $this->markTestSkipped('HTTP timeout'); 63 } 30 64 31 if ( 0 === strpos( $response->get_error_message(), 'Operation timed out after' ) ){32 $this->markTestSkipped( 'HTTP timeout');33 65 if (0 === strpos($response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80') ) { 66 $this->markTestSkipped('HTTP timeout'); 67 } 34 68 35 if ( 0 === strpos( $response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80' ) ) { 36 $this->markTestSkipped( 'HTTP timeout' ); 37 } 69 } 70 71 /** 72 * Setup. 73 * 74 * @access public 75 * @return void 76 */ 77 function setUp() 78 { 38 79 39 } 80 if (is_callable(array('WP_Http', '_getTransport')) ) { 81 $this->markTestSkipped('The WP_Http tests require a class-http.php file of r17550 or later.'); 82 return; 83 } 40 84 41 function setUp() { 85 $class = "WP_Http_" . ucfirst($this->transport); 86 if (!call_user_func(array($class, 'test')) ) { 87 $this->markTestSkipped(sprintf('The transport %s is not supported on this system', $this->transport)); 88 } 42 89 43 if ( is_callable( array('WP_Http', '_getTransport') ) ) { 44 $this->markTestSkipped('The WP_Http tests require a class-http.php file of r17550 or later.'); 45 return; 46 } 90 // Disable all transports aside from this one. 91 foreach ( array( 'curl', 'streams', 'fsockopen' ) as $t ) { 92 remove_filter("use_{$t}_transport", '__return_false'); // Just strip them all 93 if ($t != $this->transport ) { 94 add_filter("use_{$t}_transport", '__return_false'); // and add it back if need be.. 95 } 96 } 97 } 98 99 /** 100 * TearDown. 101 * 102 * @access public 103 * @return void 104 */ 105 function tearDown() 106 { 107 foreach ( array( 'curl', 'streams', 'fsockopen' ) as $t ) { 108 remove_filter("use_{$t}_transport", '__return_false'); 109 } 110 parent::tearDown(); 111 } 112 113 /** 114 * Filter HTTP Request Arguments. 115 * 116 * @access public 117 * @param array $args Arguments. 118 * @return void 119 */ 120 function filter_http_request_args( array $args ) 121 { 122 $this->http_request_args = $args; 123 return $args; 124 } 125 126 /** 127 * Test Redirect on 301. 128 * 129 * @access public 130 * @return void 131 */ 132 function test_redirect_on_301() 133 { 134 // 5 : 5 & 301 135 $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 5)); 136 $this->assertNotWPError($res); 137 $this->assertEquals(200, (int)$res['response']['code']); 138 } 139 140 /** 141 * Test Redirect on 302 142 * 143 * @access public 144 * @return void 145 */ 146 function test_redirect_on_302() 147 { 148 // 5 : 5 & 302 149 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 5)); 150 $this->assertNotWPError($res); 151 $this->assertEquals(200, (int)$res['response']['code']); 152 } 47 153 48 $class = "WP_Http_" . ucfirst( $this->transport ); 49 if ( !call_user_func( array($class, 'test') ) ) { 50 $this->markTestSkipped( sprintf('The transport %s is not supported on this system', $this->transport) ); 51 } 154 /** 155 * Test Redirect 301 no Redirect. 156 * 157 * @ticket 16855 158 * @access public 159 * @return void 160 */ 161 function test_redirect_on_301_no_redirect() 162 { 163 // 5 > 0 & 301 164 $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 0)); 165 $this->assertNotWPError($res); 166 $this->assertEquals(301, (int)$res['response']['code']); 167 } 52 168 53 // Disable all transports aside from this one. 54 foreach ( array( 'curl', 'streams', 'fsockopen' ) as $t ) { 55 remove_filter( "use_{$t}_transport", '__return_false' ); // Just strip them all 56 if ( $t != $this->transport ) 57 add_filter( "use_{$t}_transport", '__return_false' ); // and add it back if need be.. 58 } 59 } 169 /** 170 * @ticket 16855 171 */ 172 function test_redirect_on_302_no_redirect() 173 { 174 // 5 > 0 & 302 175 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0)); 176 $this->assertNotWPError($res); 177 $this->assertEquals(302, (int)$res['response']['code']); 178 } 60 179 61 function tearDown() { 62 foreach ( array( 'curl', 'streams', 'fsockopen' ) as $t ) { 63 remove_filter( "use_{$t}_transport", '__return_false' ); 64 } 65 parent::tearDown(); 66 } 180 function test_redirections_equal() 181 { 182 // 5 - 5 183 $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5)); 184 $this->assertNotWPError($res); 185 $this->assertEquals(200, (int)$res['response']['code']); 186 } 67 187 68 function filter_http_request_args( array $args ) { 69 $this->http_request_args = $args; 70 return $args; 71 } 188 function test_no_head_redirections() 189 { 190 // No redirections on HEAD request: 191 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 1, array('method' => 'HEAD')); 192 $this->assertNotWPError($res); 193 $this->assertEquals(302, (int)$res['response']['code']); 194 } 72 195 73 function test_redirect_on_301() { 74 // 5 : 5 & 301 75 $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 5) ); 76 $this->assertNotWPError( $res ); 77 $this->assertEquals(200, (int)$res['response']['code'] ); 78 } 196 /** 197 * @ticket 16855 198 */ 199 function test_redirect_on_head() 200 { 201 // Redirections on HEAD request when Requested 202 $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5, 'method' => 'HEAD')); 203 $this->assertNotWPError($res); 204 $this->assertEquals(200, (int)$res['response']['code']); 205 } 79 206 80 function test_redirect_on_302() { 81 // 5 : 5 & 302 82 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 5) ); 83 $this->assertNotWPError( $res);84 $this->assertEquals(200, (int)$res['response']['code']);85 207 function test_redirections_greater() 208 { 209 // 10 > 5 210 $res = wp_remote_request($this->redirection_script . '?rt=' . 10, array('redirection' => 5)); 211 $this->assertWPError($res); 212 } 86 213 87 /** 88 * @ticket 16855 89 */ 90 function test_redirect_on_301_no_redirect() { 91 // 5 > 0 & 301 92 $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 0) ); 93 $this->assertNotWPError( $res ); 94 $this->assertEquals(301, (int)$res['response']['code'] ); 95 } 214 function test_redirections_greater_edgecase() 215 { 216 // 6 > 5 (close edgecase) 217 $res = wp_remote_request($this->redirection_script . '?rt=' . 6, array('redirection' => 5)); 218 $this->assertWPError($res); 219 } 96 220 97 /** 98 * @ticket 16855 99 */ 100 function test_redirect_on_302_no_redirect() { 101 // 5 > 0 & 302 102 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) ); 103 $this->assertNotWPError( $res ); 104 $this->assertEquals(302, (int)$res['response']['code'] ); 105 } 221 function test_redirections_less_edgecase() 222 { 223 // 4 < 5 (close edgecase) 224 $res = wp_remote_request($this->redirection_script . '?rt=' . 4, array('redirection' => 5)); 225 $this->assertNotWPError($res); 226 } 106 227 107 function test_redirections_equal() { 108 // 5 - 5 109 $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5) ); 110 $this->assertNotWPError( $res ); 111 $this->assertEquals(200, (int)$res['response']['code'] ); 112 } 228 /** 229 * @ticket 16855 230 */ 231 function test_redirections_zero_redirections_specified() 232 { 233 // 0 redirections asked for, Should return the document? 234 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0)); 235 $this->assertNotWPError($res); 236 $this->assertEquals(302, (int)$res['response']['code']); 237 } 113 238 114 function test_no_head_redirections() { 115 // No redirections on HEAD request: 116 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 1, array('method' => 'HEAD') ); 117 $this->assertNotWPError( $res ); 118 $this->assertEquals( 302, (int)$res['response']['code'] ); 119 } 239 /** 240 * Do not redirect on non 3xx status codes 241 * 242 * @ticket 16889 243 */ 244 function test_location_header_on_201() 245 { 246 // Prints PASS on initial load, FAIL if the client follows the specified redirection 247 $res = wp_remote_request($this->redirection_script . '?201-location=true'); 248 $this->assertNotWPError($res); 249 $this->assertEquals('PASS', $res['body']); 250 } 120 251 121 /** 122 * @ticket 16855 123 */ 124 function test_redirect_on_head() { 125 // Redirections on HEAD request when Requested 126 $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5, 'method' => 'HEAD') ); 127 $this->assertNotWPError( $res ); 128 $this->assertEquals( 200, (int)$res['response']['code'] ); 129 } 252 /** 253 * Test handling of PUT requests on redirects 254 * 255 * @ticket 16889 256 */ 257 function test_no_redirection_on_PUT() 258 { 259 $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?201-location=1'; 130 260 131 function test_redirections_greater() { 132 // 10 > 5 133 $res = wp_remote_request($this->redirection_script . '?rt=' . 10, array('redirection' => 5));134 $this->assertWPError( $res);135 261 // Test 301 - POST to POST 262 $res = wp_remote_request($url, array( 'method' => 'PUT', 'timeout' => 30 )); 263 $this->assertEquals('PASS', wp_remote_retrieve_body($res)); 264 $this->assertTrue(!empty($res['headers']['location'])); 265 } 136 266 137 function test_redirections_greater_edgecase() { 138 // 6 > 5 (close edgecase) 139 $res = wp_remote_request($this->redirection_script . '?rt=' . 6, array('redirection' => 5) ); 140 $this->assertWPError( $res ); 141 } 267 /** 268 * @ticket 11888 269 */ 270 function test_send_headers() 271 { 272 // Test that the headers sent are recieved by the server 273 $headers = array('test1' => 'test', 'test2' => 0, 'test3' => ''); 274 $res = wp_remote_request($this->redirection_script . '?header-check', array('headers' => $headers)); 142 275 143 function test_redirections_less_edgecase() { 144 // 4 < 5 (close edgecase) 145 $res = wp_remote_request($this->redirection_script . '?rt=' . 4, array('redirection' => 5) ); 146 $this->assertNotWPError( $res ); 147 } 276 $this->assertNotWPError($res); 148 277 149 /** 150 * @ticket 16855 151 */ 152 function test_redirections_zero_redirections_specified() { 153 // 0 redirections asked for, Should return the document? 154 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0));155 $this->assertNotWPError( $res);156 $this->assertEquals( 302, (int)$res['response']['code'] );157 278 $headers = array(); 279 foreach ( explode("\n", $res['body']) as $key => $value ) { 280 if (empty($value) ) { 281 continue; 282 } 283 $parts = explode(':', $value, 2); 284 unset($headers[$key]); 285 $headers[ $parts[0] ] = $parts[1]; 286 } 158 287 159 /** 160 * Do not redirect on non 3xx status codes 161 * 162 * @ticket 16889 163 */ 164 function test_location_header_on_201() { 165 // Prints PASS on initial load, FAIL if the client follows the specified redirection 166 $res = wp_remote_request( $this->redirection_script . '?201-location=true' ); 167 $this->assertNotWPError( $res ); 168 $this->assertEquals( 'PASS', $res['body']); 169 } 288 $this->assertTrue(isset($headers['test1']) && 'test' == $headers['test1']); 289 $this->assertTrue(isset($headers['test2']) && '0' === $headers['test2']); 290 // cURL/HTTP Extension Note: Will never pass, cURL does not pass headers with an empty value. 291 // Should it be that empty headers with empty values are NOT sent? 292 //$this->assertTrue( isset($headers['test3']) && '' === $headers['test3'] ); 293 } 170 294 171 /** 172 * Test handling of PUT requests on redirects 173 * 174 * @ticket 16889 175 */ 176 function test_no_redirection_on_PUT() { 177 $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?201-location=1'; 295 function test_file_stream() 296 { 297 $url = $this->fileStreamUrl; 298 $size = 153204; 299 $res = wp_remote_request($url, array( 'stream' => true, 'timeout' => 30 )); //Auto generate the filename. 178 300 179 // Test 301 - POST to POST 180 $res = wp_remote_request( $url, array( 'method' => 'PUT', 'timeout' => 30 ) ); 181 $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ));182 $this->assertTrue( !empty( $res['headers']['location'] ));183 301 // Cleanup before we assert, as it'll return early. 302 if (! is_wp_error($res) ) { 303 $filesize = filesize($res['filename']); 304 unlink($res['filename']); 305 } 184 306 185 /** 186 * @ticket 11888 187 */ 188 function test_send_headers() { 189 // Test that the headers sent are recieved by the server 190 $headers = array('test1' => 'test', 'test2' => 0, 'test3' => ''); 191 $res = wp_remote_request( $this->redirection_script . '?header-check', array('headers' => $headers) ); 307 $this->skipTestOnTimeout($res); 192 308 193 $this->assertNotWPError( $res ); 309 $this->assertNotWPError($res); 310 $this->assertEquals('', $res['body']); // The body should be empty. 311 $this->assertEquals($size, $res['headers']['content-length']); // Check the headers are returned (and the size is the same..) 312 $this->assertEquals($size, $filesize); // Check that the file is written to disk correctly without any extra characters 313 $this->assertStringStartsWith(get_temp_dir(), $res['filename']); // Check it's saving within the temp dir 314 } 194 315 195 $headers = array(); 196 foreach ( explode("\n", $res['body']) as $key => $value ) { 197 if ( empty($value) ) 198 continue; 199 $parts = explode(':', $value,2); 200 unset($headers[$key]);201 $headers[ $parts[0] ] = $parts[1];202 } 316 /** 317 * @ticket 26726 318 */ 319 function test_file_stream_limited_size() 320 { 321 $url = $this->fileStreamUrl; 322 $size = 10000; 323 $res = wp_remote_request($url, array( 'stream' => true, 'timeout' => 30, 'limit_response_size' => $size )); //Auto generate the filename. 203 324 204 $this->assertTrue( isset($headers['test1']) && 'test' == $headers['test1'] ); 205 $this->assertTrue( isset($headers['test2']) && '0' === $headers['test2'] ); 206 // cURL/HTTP Extension Note: Will never pass, cURL does not pass headers with an empty value. 207 // Should it be that empty headers with empty values are NOT sent? 208 //$this->assertTrue( isset($headers['test3']) && '' === $headers['test3'] ); 209 } 325 // Cleanup before we assert, as it'll return early. 326 if (! is_wp_error($res) ) { 327 $filesize = filesize($res['filename']); 328 unlink($res['filename']); 329 } 210 330 211 function test_file_stream() { 212 $url = $this->fileStreamUrl; 213 $size = 153204; 214 $res = wp_remote_request( $url, array( 'stream' => true, 'timeout' => 30 ) ); //Auto generate the filename. 331 $this->skipTestOnTimeout($res); 215 332 216 // Cleanup before we assert, as it'll return early. 217 if ( ! is_wp_error( $res ) ) { 218 $filesize = filesize( $res['filename'] ); 219 unlink( $res['filename'] ); 220 } 333 $this->assertNotWPError($res); 334 $this->assertEquals($size, $filesize); // Check that the file is written to disk correctly without any extra characters 221 335 222 $this->skipTestOnTimeout( $res ); 336 } 223 337 224 $this->assertNotWPError( $res ); 225 $this->assertEquals( '', $res['body'] ); // The body should be empty. 226 $this->assertEquals( $size, $res['headers']['content-length'] ); // Check the headers are returned (and the size is the same..) 227 $this->assertEquals( $size, $filesize ); // Check that the file is written to disk correctly without any extra characters 228 $this->assertStringStartsWith( get_temp_dir(), $res['filename'] ); // Check it's saving within the temp dir 229 } 338 /** 339 * Tests Limiting the response size when returning strings 340 * 341 * @ticket 31172 342 */ 343 function test_request_limited_size() 344 { 345 $url = $this->fileStreamUrl; 346 $size = 10000; 230 347 231 /** 232 * @ticket 26726 233 */ 234 function test_file_stream_limited_size() { 235 $url = $this->fileStreamUrl; 236 $size = 10000; 237 $res = wp_remote_request( $url, array( 'stream' => true, 'timeout' => 30, 'limit_response_size' => $size ) ); //Auto generate the filename. 348 $res = wp_remote_request($url, array( 'timeout' => 30, 'limit_response_size' => $size )); 238 349 239 // Cleanup before we assert, as it'll return early. 240 if ( ! is_wp_error( $res ) ) { 241 $filesize = filesize( $res['filename'] ); 242 unlink( $res['filename'] ); 243 } 350 $this->skipTestOnTimeout($res); 244 351 245 $this->skipTestOnTimeout( $res ); 352 $this->assertNotWPError($res); 353 $this->assertEquals($size, strlen($res['body'])); 354 } 246 355 247 $this->assertNotWPError( $res ); 248 $this->assertEquals( $size, $filesize ); // Check that the file is written to disk correctly without any extra characters 356 /** 357 * Test POST redirection methods 358 * 359 * @dataProvider data_post_redirect_to_method_300 360 * 361 * @ticket 17588 362 */ 363 function test_post_redirect_to_method_300( $response_code, $method ) 364 { 365 $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?post-redirect-to-method=1'; 249 366 250 } 367 $res = wp_remote_post(add_query_arg('response_code', $response_code, $url), array( 'timeout' => 30 )); 368 $this->assertEquals($method, wp_remote_retrieve_body($res)); 369 } 251 370 252 /** 253 * Tests Limiting the response size when returning strings 254 * 255 * @ticket 31172 256 */ 257 function test_request_limited_size() { 258 $url = $this->fileStreamUrl; 259 $size = 10000; 371 public function data_post_redirect_to_method_300() 372 { 373 return array( 374 // Test 300 - POST to POST 375 array( 376 300, 377 'POST', 378 ), 379 // Test 301 - POST to POST 380 array( 381 301, 382 'POST', 383 ), 384 // Test 302 - POST to GET 385 array( 386 302, 387 'GET', 388 ), 389 // Test 303 - POST to GET 390 array( 391 303, 392 'GET', 393 ), 394 ); 395 } 260 396 261 $res = wp_remote_request( $url, array( 'timeout' => 30, 'limit_response_size' => $size ) ); 397 /** 398 * Test HTTP Requests using an IP url, with a HOST header specified 399 * 400 * @ticket 24182 401 */ 402 function test_ip_url_with_host_header() 403 { 404 $ip = gethostbyname('api.wordpress.org'); 405 $url = 'http://' . $ip . '/core/tests/1.0/redirection.php?print-pass=1'; 406 $args = array( 407 'headers' => array( 408 'Host' => 'api.wordpress.org', 409 ), 410 'timeout' => 30, 411 'redirection' => 0, 412 ); 262 413 263 $this->skipTestOnTimeout( $res ); 414 $res = wp_remote_get($url, $args); 415 $this->assertEquals('PASS', wp_remote_retrieve_body($res)); 264 416 265 $this->assertNotWPError( $res ); 266 $this->assertEquals( $size, strlen( $res['body'] ) ); 267 } 417 } 268 418 269 /** 270 * Test POST redirection methods 271 * 272 * @dataProvider data_post_redirect_to_method_300 273 * 274 * @ticket 17588 275 */ 276 function test_post_redirect_to_method_300( $response_code, $method ) { 277 $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?post-redirect-to-method=1'; 419 /** 420 * Test HTTP requests where SSL verification is disabled but the CA bundle is still populated 421 * 422 * @ticket 33978 423 */ 424 function test_https_url_without_ssl_verification() 425 { 426 $url = 'https://wordpress.org/'; 427 $args = array( 428 'sslverify' => false, 429 ); 278 430 279 $res = wp_remote_post( add_query_arg( 'response_code', $response_code, $url ), array( 'timeout' => 30 ) ); 280 $this->assertEquals( $method, wp_remote_retrieve_body( $res ) ); 281 } 431 add_filter('http_request_args', array( $this, 'filter_http_request_args' )); 282 432 283 public function data_post_redirect_to_method_300() { 284 return array( 285 // Test 300 - POST to POST 286 array( 287 300, 288 'POST', 289 ), 290 // Test 301 - POST to POST 291 array( 292 301, 293 'POST', 294 ), 295 // Test 302 - POST to GET 296 array( 297 302, 298 'GET', 299 ), 300 // Test 303 - POST to GET 301 array( 302 303, 303 'GET', 304 ), 305 ); 306 } 433 $res = wp_remote_head($url, $args); 307 434 308 /** 309 * Test HTTP Requests using an IP url, with a HOST header specified 310 * 311 * @ticket 24182 312 */ 313 function test_ip_url_with_host_header() { 314 $ip = gethostbyname( 'api.wordpress.org' ); 315 $url = 'http://' . $ip . '/core/tests/1.0/redirection.php?print-pass=1'; 316 $args = array( 317 'headers' => array( 318 'Host' => 'api.wordpress.org', 319 ), 320 'timeout' => 30, 321 'redirection' => 0, 322 ); 435 remove_filter('http_request_args', array( $this, 'filter_http_request_args' )); 323 436 324 $res = wp_remote_get( $url, $args ); 325 $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) ); 437 $this->assertNotEmpty($this->http_request_args['sslcertificates']); 438 $this->assertNotWPError($res); 439 } 326 440 327 } 441 /** 442 * Test HTTP Redirects with multiple Location headers specified 443 * 444 * @ticket 16890 445 */ 446 function test_multiple_location_headers() 447 { 448 $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?multiple-location-headers=1'; 449 $res = wp_remote_head($url, array( 'timeout' => 30 )); 328 450 329 /** 330 * Test HTTP requests where SSL verification is disabled but the CA bundle is still populated 331 * 332 * @ticket 33978 333 */ 334 function test_https_url_without_ssl_verification() { 335 $url = 'https://wordpress.org/'; 336 $args = array( 337 'sslverify' => false, 338 ); 451 $this->assertInternalType('array', wp_remote_retrieve_header($res, 'location')); 452 $this->assertCount(2, wp_remote_retrieve_header($res, 'location')); 339 453 340 add_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) ); 454 $res = wp_remote_get($url, array( 'timeout' => 30 )); 455 $this->assertEquals('PASS', wp_remote_retrieve_body($res)); 341 456 342 $res = wp_remote_head( $url, $args ); 457 } 343 458 344 remove_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) ); 459 /** 460 * Test HTTP Cookie handling 461 * 462 * @ticket 21182 463 */ 464 function test_cookie_handling() 465 { 466 $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?cookie-test=1'; 345 467 346 $this->assertNotEmpty( $this->http_request_args['sslcertificates']);347 $this->assertNotWPError( $res);348 468 $res = wp_remote_get($url); 469 $this->assertEquals('PASS', wp_remote_retrieve_body($res)); 470 } 349 471 350 /** 351 * Test HTTP Redirects with multiple Location headers specified 352 * 353 * @ticket 16890 354 */ 355 function test_multiple_location_headers() { 356 $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?multiple-location-headers=1'; 357 $res = wp_remote_head( $url, array( 'timeout' => 30 ) ); 472 /** 473 * Test if HTTPS support works 474 * 475 * @group ssl 476 * @ticket 25007 477 */ 478 function test_ssl() 479 { 480 if (! wp_http_supports(array( 'ssl' )) ) { 481 $this->markTestSkipped('This install of PHP does not support SSL'); 482 } 358 483 359 $this->assertInternalType( 'array', wp_remote_retrieve_header( $res, 'location' ) ); 360 $this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) ); 484 $res = wp_remote_get('https://wordpress.org/'); 485 $this->assertNotWPError($res); 486 } 361 487 362 $res = wp_remote_get( $url, array( 'timeout' => 30 ) ); 363 $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) ); 488 /** 489 * @ticket 37733 490 */ 491 function test_url_with_double_slashes_path() 492 { 493 $url = $this->redirection_script . '?rt=' . 0; 364 494 365 } 495 $path = parse_url($url, PHP_URL_PATH); 496 $url = str_replace($path, '/' . $path, $url); 366 497 367 /** 368 * Test HTTP Cookie handling 369 * 370 * @ticket 21182 371 */ 372 function test_cookie_handling() { 373 $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?cookie-test=1'; 498 $res = wp_remote_request($url); 499 $this->assertNotWPError($res); 500 } 374 501 375 $res = wp_remote_get( $url );376 $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );377 }378 502 379 /**380 * Test if HTTPS support works381 *382 * @group ssl383 * @ticket 25007384 */385 function test_ssl() {386 if ( ! wp_http_supports( array( 'ssl' ) ) )387 $this->markTestSkipped( 'This install of PHP does not support SSL' );388 389 $res = wp_remote_get( 'https://wordpress.org/' );390 $this->assertNotWPError( $res );391 }392 393 /**394 * @ticket 37733395 */396 function test_url_with_double_slashes_path() {397 $url = $this->redirection_script . '?rt=' . 0;398 399 $path = parse_url( $url, PHP_URL_PATH );400 $url = str_replace( $path, '/' . $path, $url );401 402 $res = wp_remote_request( $url );403 $this->assertNotWPError( $res );404 }405 406 407 503 } -
curl.php
1 1 <?php 2 2 3 require_once dirname( __FILE__) . '/base.php';3 require_once dirname(__FILE__) . '/base.php'; 4 4 5 5 /** 6 * @group http 7 * @group external-http 6 * Tests HTTP Curl 7 * 8 * @group http 9 * @group external-http 10 * @extends WP_HTTP_UnitTestCase 8 11 */ 9 class Tests_HTTP_curl extends WP_HTTP_UnitTestCase { 10 var $transport = 'curl'; 12 class Tests_HTTP_curl extends WP_HTTP_UnitTestCase 13 { 14 15 /** 16 * transport 17 * 18 * (default value: 'curl') 19 * 20 * @var string 21 * @access public 22 */ 23 var $transport = 'curl'; 11 24 12 /** 13 * @ticket 39783 14 */ 15 public function test_http_api_curl_stream_parameter_is_a_reference() { 16 add_action( 'http_api_curl', array( $this, '_action_test_http_api_curl_stream_parameter_is_a_reference' ), 10, 3 ); 17 wp_remote_request( $this->fileStreamUrl, array( 'stream' => true, 'timeout' => 30 ) ); 18 remove_action( 'http_api_curl', array( $this, '_action_test_http_api_curl_stream_parameter_is_a_reference' ), 10 ); 19 } 20 21 public function _action_test_http_api_curl_stream_parameter_is_a_reference( &$stream, $r, $url ) { 22 // $stream not being a reference will cause a PHP warning. 23 // For counting tests purposes, let's do a fake assert. 24 $this->assertTrue( true ); 25 } 25 /** 26 * Test HTTP API Curl Stream Paramater is a Reference. 27 * 28 * @ticket 39783 29 * @access public 30 * @return void 31 */ 32 public function test_http_api_curl_stream_parameter_is_a_reference() 33 { 34 add_action('http_api_curl', array( $this, '_action_test_http_api_curl_stream_parameter_is_a_reference' ), 10, 3); 35 wp_remote_request($this->fileStreamUrl, array( 'stream' => true, 'timeout' => 30 )); 36 remove_action('http_api_curl', array( $this, '_action_test_http_api_curl_stream_parameter_is_a_reference' ), 10); 37 } 38 39 /** 40 * Test HTTP API Curl Stream Parameter is a Reference. 41 * 42 * @access public 43 * @param mixed &$stream Stream. 44 * @param mixed $r Reference. 45 * @param mixed $url URL. 46 * @return void 47 */ 48 public function _action_test_http_api_curl_stream_parameter_is_a_reference( &$stream, $r, $url ) 49 { 50 // $stream not being a reference will cause a PHP warning. 51 // For counting tests purposes, let's do a fake assert. 52 $this->assertTrue(true); 53 } 26 54 } -
functions.php
4 4 * @group http 5 5 * @group external-http 6 6 */ 7 class Tests_HTTP_Functions extends WP_UnitTestCase { 8 public function setUp() { 9 if ( ! extension_loaded( 'openssl' ) ) { 10 $this->markTestSkipped( 'Tests_HTTP_Functions requires openssl.' ); 11 } 7 class Tests_HTTP_Functions extends WP_UnitTestCase 8 { 9 10 11 /** 12 * Setup. 13 * 14 * @access public 15 * @return void 16 */ 17 public function setUp() 18 { 19 if (! extension_loaded('openssl') ) { 20 $this->markTestSkipped('Tests_HTTP_Functions requires openssl.'); 21 } 12 22 13 parent::setUp(); 14 } 23 parent::setUp(); 24 } 25 26 /** 27 * Test head Request. 28 * 29 * @access public 30 * @return void 31 */ 32 function test_head_request() 33 { 34 // This url give a direct 200 response. 35 $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg'; 36 $response = wp_remote_head($url); 37 $headers = wp_remote_retrieve_headers($response); 15 38 16 function test_head_request() { 17 // this url give a direct 200 response 18 $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg'; 19 $response = wp_remote_head( $url ); 20 $headers = wp_remote_retrieve_headers( $response ); 39 $this->assertInternalType('array', $response); 40 41 $this->assertEquals('image/jpeg', $headers['content-type']); 42 $this->assertEquals('40148', $headers['content-length']); 43 $this->assertEquals('200', wp_remote_retrieve_response_code($response)); 44 } 45 46 /** 47 * Test Head Redirect. 48 * 49 * @access public 50 * @return void 51 */ 52 function test_head_redirect() 53 { 54 // This url will 301 redirect. 55 $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 56 $response = wp_remote_head($url); 57 $this->assertEquals('301', wp_remote_retrieve_response_code($response)); 58 } 59 60 /** 61 * Test Head 404. 62 * 63 * @access public 64 * @return void 65 */ 66 function test_head_404() 67 { 68 $url = 'https://asdftestblog1.files.wordpress.com/2007/09/awefasdfawef.jpg'; 69 $headers = wp_remote_head($url); 21 70 22 $this->assertInternalType( 'array', $response ); 23 24 $this->assertEquals( 'image/jpeg', $headers['content-type'] ); 25 $this->assertEquals( '40148', $headers['content-length'] ); 26 $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) ); 27 } 71 $this->assertEquals('404', wp_remote_retrieve_response_code($headers)); 72 } 73 74 /** 75 * Test Get Request. 76 * 77 * @access public 78 * @return void 79 */ 80 function test_get_request() 81 { 82 $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg'; 28 83 29 function test_head_redirect() { 30 // this url will 301 redirect 31 $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 32 $response = wp_remote_head( $url ); 33 $this->assertEquals( '301', wp_remote_retrieve_response_code( $response ) ); 34 } 84 $response = wp_remote_get($url); 85 $headers = wp_remote_retrieve_headers($response); 35 86 36 function test_head_404() { 37 $url = 'https://asdftestblog1.files.wordpress.com/2007/09/awefasdfawef.jpg'; 38 $headers = wp_remote_head( $url ); 87 $this->assertInternalType('array', $response); 88 89 // Should return the same headers as a head request. 90 $this->assertEquals('image/jpeg', $headers['content-type']); 91 $this->assertEquals('40148', $headers['content-length']); 92 $this->assertEquals('200', wp_remote_retrieve_response_code($response)); 93 } 94 95 /** 96 * Test Get Redirect. 97 * 98 * @access public 99 * @return void 100 */ 101 function test_get_redirect() 102 { 103 // This will redirect to asdftestblog1.files.wordpress.com 104 $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 39 105 40 $this->assertEquals( '404', wp_remote_retrieve_response_code( $headers ));41 } 106 $response = wp_remote_get($url); 107 $headers = wp_remote_retrieve_headers($response); 42 108 43 function test_get_request() { 44 $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg'; 109 // Should return the same headers as a head request. 110 $this->assertEquals('image/jpeg', $headers['content-type']); 111 $this->assertEquals('40148', $headers['content-length']); 112 $this->assertEquals('200', wp_remote_retrieve_response_code($response)); 113 } 114 115 /** 116 * Test Get Redirect Limit Exceeded 117 * 118 * @access public 119 * @return void 120 */ 121 function test_get_redirect_limit_exceeded() 122 { 123 // This will redirect to asdftestblog1.files.wordpress.com 124 $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 45 125 46 $response = wp_remote_get( $url ); 47 $headers = wp_remote_retrieve_headers( $response ); 126 // Pretend we've already redirected 5 times. 127 $response = wp_remote_get($url, array( 'redirection' => -1 )); 128 $this->assertWPError($response); 129 } 48 130 49 $this->assertInternalType( 'array', $response ); 50 51 // should return the same headers as a head request 52 $this->assertEquals( 'image/jpeg', $headers['content-type'] ); 53 $this->assertEquals( '40148', $headers['content-length'] ); 54 $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) ); 55 } 131 /** 132 * Test Get Response Cookie. 133 * 134 * @ticket 33711 135 * @access public 136 * @return void 137 */ 138 function test_get_response_cookies() 139 { 140 $url = 'https://login.wordpress.org/wp-login.php'; 56 141 57 function test_get_redirect() { 58 // this will redirect to asdftestblog1.files.wordpress.com 59 $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 142 $response = wp_remote_head($url); 143 $cookies = wp_remote_retrieve_cookies($response); 60 144 61 $response = wp_remote_get( $url ); 62 $headers = wp_remote_retrieve_headers( $response ); 145 $this->assertNotEmpty($cookies); 63 146 64 // should return the same headers as a head request 65 $this->assertEquals( 'image/jpeg', $headers['content-type'] ); 66 $this->assertEquals( '40148', $headers['content-length'] ); 67 $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) ); 68 } 147 $cookie = wp_remote_retrieve_cookie($response, 'wordpress_test_cookie'); 148 $this->assertInstanceOf('WP_Http_Cookie', $cookie); 149 $this->assertSame('wordpress_test_cookie', $cookie->name); 150 $this->assertSame('WP Cookie check', $cookie->value); 69 151 70 function test_get_redirect_limit_exceeded() { 71 // this will redirect to asdftestblog1.files.wordpress.com 72 $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 152 $value = wp_remote_retrieve_cookie_value($response, 'wordpress_test_cookie'); 153 $this->assertSame('WP Cookie check', $value); 73 154 74 // pretend we've already redirected 5 times 75 $response = wp_remote_get( $url, array( 'redirection' => -1 ) ); 76 $this->assertWPError( $response ); 77 } 155 $no_value = wp_remote_retrieve_cookie_value($response, 'not_a_cookie'); 156 $this->assertSame('', $no_value); 78 157 79 /** 80 * @ticket 33711 81 */ 82 function test_get_response_cookies() { 83 $url = 'https://login.wordpress.org/wp-login.php'; 158 $no_cookie = wp_remote_retrieve_cookie($response, 'not_a_cookie'); 159 $this->assertSame('', $no_cookie); 160 } 84 161 85 $response = wp_remote_head( $url ); 86 $cookies = wp_remote_retrieve_cookies( $response ); 162 /** 163 * Test Get Response Cookies with WP HTTP Cookie Object. 164 * 165 * @ticket 37437 166 * @access public 167 * @return void 168 */ 169 function test_get_response_cookies_with_wp_http_cookie_object() 170 { 171 $url = 'http://example.org'; 87 172 88 $this->assertNotEmpty( $cookies ); 173 $response = wp_remote_get( 174 $url, array( 175 'cookies' => array( 176 new WP_Http_Cookie(array( 'name' => 'test', 'value' => 'foo' )), 177 ), 178 ) 179 ); 180 $cookies = wp_remote_retrieve_cookies($response); 89 181 90 $cookie = wp_remote_retrieve_cookie( $response, 'wordpress_test_cookie' ); 91 $this->assertInstanceOf( 'WP_Http_Cookie', $cookie ); 92 $this->assertSame( 'wordpress_test_cookie', $cookie->name ); 93 $this->assertSame( 'WP Cookie check', $cookie->value ); 182 $this->assertNotEmpty($cookies); 94 183 95 $value = wp_remote_retrieve_cookie_value( $response, 'wordpress_test_cookie' ); 96 $this->assertSame( 'WP Cookie check', $value ); 184 $cookie = wp_remote_retrieve_cookie($response, 'test'); 185 $this->assertInstanceOf('WP_Http_Cookie', $cookie); 186 $this->assertSame('test', $cookie->name); 187 $this->assertSame('foo', $cookie->value); 188 } 97 189 98 $no_value = wp_remote_retrieve_cookie_value( $response, 'not_a_cookie' ); 99 $this->assertSame( '', $no_value ); 190 /** 191 * Test Get Response Cookies with Name Value Array. 192 * 193 * @ticket 37437 194 * @access public 195 * @return void 196 */ 197 function test_get_response_cookies_with_name_value_array() 198 { 199 $url = 'http://example.org'; 100 200 101 $no_cookie = wp_remote_retrieve_cookie( $response, 'not_a_cookie' ); 102 $this->assertSame( '', $no_cookie ); 103 } 201 $response = wp_remote_get( 202 $url, array( 203 'cookies' => array( 204 'test' => 'foo', 205 ), 206 ) 207 ); 208 $cookies = wp_remote_retrieve_cookies($response); 104 209 105 /** 106 * @ticket 37437 107 */ 108 function test_get_response_cookies_with_wp_http_cookie_object() { 109 $url = 'http://example.org'; 210 $this->assertNotEmpty($cookies); 110 211 111 $response = wp_remote_get( $url, array( 112 'cookies' => array( 113 new WP_Http_Cookie( array( 'name' => 'test', 'value' => 'foo' ) ), 114 ), 115 ) ); 116 $cookies = wp_remote_retrieve_cookies( $response ); 117 118 $this->assertNotEmpty( $cookies ); 119 120 $cookie = wp_remote_retrieve_cookie( $response, 'test' ); 121 $this->assertInstanceOf( 'WP_Http_Cookie', $cookie ); 122 $this->assertSame( 'test', $cookie->name ); 123 $this->assertSame( 'foo', $cookie->value ); 124 } 125 126 /** 127 * @ticket 37437 128 */ 129 function test_get_response_cookies_with_name_value_array() { 130 $url = 'http://example.org'; 131 132 $response = wp_remote_get( $url, array( 133 'cookies' => array( 134 'test' => 'foo', 135 ), 136 ) ); 137 $cookies = wp_remote_retrieve_cookies( $response ); 138 139 $this->assertNotEmpty( $cookies ); 140 141 $cookie = wp_remote_retrieve_cookie( $response, 'test' ); 142 $this->assertInstanceOf( 'WP_Http_Cookie', $cookie ); 143 $this->assertSame( 'test', $cookie->name ); 144 $this->assertSame( 'foo', $cookie->value ); 145 } 212 $cookie = wp_remote_retrieve_cookie($response, 'test'); 213 $this->assertInstanceOf('WP_Http_Cookie', $cookie); 214 $this->assertSame('test', $cookie->name); 215 $this->assertSame('foo', $cookie->value); 216 } 146 217 } -
getHttpHeaders.php
1 1 <?php 2 2 3 3 /** 4 * @group http 4 * Tests_HTTP_GetHttpHeaders class. 5 * 6 * @group http 7 * @extends WP_UnitTestCase 5 8 */ 6 class Tests_HTTP_GetHttpHeaders extends WP_UnitTestCase { 9 class Tests_HTTP_GetHttpHeaders extends WP_UnitTestCase 10 { 7 11 8 /** 9 * Set up the environment 10 */ 11 public function setUp() { 12 parent::setUp(); 12 /** 13 * Set up the environment. 14 * 15 * @access public 16 * @return void 17 */ 18 public function setUp() 19 { 20 parent::setUp(); 13 21 14 15 add_filter( 'pre_http_request', array( $this, 'fake_http_request' ), 10, 3);16 22 // Hook a fake HTTP request response. 23 add_filter('pre_http_request', array( $this, 'fake_http_request' ), 10, 3); 24 } 17 25 18 /** 19 * Clean up environment 20 */ 21 public function tearDown() { 22 parent::tearDown(); 26 /** 27 * Clean up environment. 28 * 29 * @access public 30 * @return void 31 */ 32 public function tearDown() 33 { 34 parent::tearDown(); 23 35 24 25 remove_filter( 'pre_http_request', array( $this, 'fake_http_request' ));26 36 // Clear the hook for the fake HTTP request response. 37 remove_filter('pre_http_request', array( $this, 'fake_http_request' )); 38 } 27 39 28 /** 29 * Test with a valid URL 30 */ 31 public function test_wp_get_http_headers_valid_url() { 32 $result = wp_get_http_headers( 'http://example.com' ); 33 $this->assertTrue( $result ); 34 } 40 /** 41 * Test WP Get HTTP Headers has valid url. 42 * 43 * @access public 44 * @return void 45 */ 46 public function test_wp_get_http_headers_valid_url() 47 { 48 $result = wp_get_http_headers('http://example.com'); 49 $this->assertTrue($result); 50 } 35 51 36 /** 37 * Test with an invalid URL 38 */ 39 public function test_wp_get_http_headers_invalid_url() { 40 $result = wp_get_http_headers( 'not_an_url' ); 41 $this->assertFalse( $result ); 42 } 52 /** 53 * Test WP Get HTTP Headers has invalid URL. 54 * 55 * @access public 56 * @return void 57 */ 58 public function test_wp_get_http_headers_invalid_url() 59 { 60 $result = wp_get_http_headers('not_an_url'); 61 $this->assertFalse($result); 62 } 43 63 44 /** 45 * Test to see if the deprecated argument is working 46 */ 47 public function test_wp_get_http_headers_deprecated_argument() { 48 $this->setExpectedDeprecated( 'wp_get_http_headers' ); 64 /** 65 * Test to see if the deprecated argument is working 66 * 67 * @access public 68 * @return void 69 */ 70 public function test_wp_get_http_headers_deprecated_argument() 71 { 72 $this->setExpectedDeprecated('wp_get_http_headers'); 49 73 50 wp_get_http_headers( 'does_not_matter', $deprecated = true);51 74 wp_get_http_headers('does_not_matter', $deprecated = true); 75 } 52 76 53 /** 54 * Mock the HTTP request response 55 * 56 * @param bool $false False. 57 * @param array $arguments Request arguments. 58 * @param string $url Request URL. 59 * 60 * @return array|bool 61 */ 62 public function fake_http_request( $false, $arguments, $url ) { 63 if ( 'http://example.com' === $url ) { 64 return array( 'headers' => true ); 65 } 77 /** 78 * Mock the HTTP request response. 79 * 80 * @param bool $false False. 81 * @param array $arguments Request arguments. 82 * @param string $url Request URL. 83 * 84 * @return array|bool 85 */ 86 public function fake_http_request( $false, $arguments, $url ) 87 { 88 if ('http://example.com' === $url ) { 89 return array( 'headers' => true ); 90 } 66 91 67 68 92 return false; 93 } 69 94 } -
http.php
4 4 * 5 5 * @group http 6 6 */ 7 class Tests_HTTP_HTTP extends WP_UnitTestCase { 7 class Tests_HTTP_HTTP extends WP_UnitTestCase 8 { 9 10 const FULL_TEST_URL = 'http://username:password@host.name:9090/path?arg1=value1&arg2=value2#anchor'; 8 11 9 const FULL_TEST_URL = 'http://username:password@host.name:9090/path?arg1=value1&arg2=value2#anchor'; 12 /** 13 * Test Make Absolute Url. 14 * 15 * @access public 16 * @dataProvider make_absolute_url_testcases 17 * @param mixed $relative_url Relative URL. 18 * @param mixed $absolute_url Absolute URL. 19 * @param mixed $expected Expected. 20 * @return void 21 */ 22 function test_make_absolute_url( $relative_url, $absolute_url, $expected ) 23 { 24 if (! is_callable(array( 'WP_Http', 'make_absolute_url' )) ) { 25 $this->markTestSkipped("This version of WP_HTTP doesn't support WP_HTTP::make_absolute_url()"); 26 return; 27 } 10 28 11 /** 12 * @dataProvider make_absolute_url_testcases 13 */ 14 function test_make_absolute_url( $relative_url, $absolute_url, $expected ) { 15 if ( ! is_callable( array( 'WP_Http', 'make_absolute_url' ) ) ) { 16 $this->markTestSkipped( "This version of WP_HTTP doesn't support WP_HTTP::make_absolute_url()" ); 17 return; 18 } 29 $actual = WP_Http::make_absolute_url($relative_url, $absolute_url); 30 $this->assertEquals($expected, $actual); 31 } 32 33 /** 34 * Make Absolute URL Test Cases. 35 * 36 * @access public 37 * @return void 38 */ 39 function make_absolute_url_testcases() 40 { 41 // 0: The Location header, 1: The current url, 3: The expected url 42 return array( 43 array( 'http://site.com/', 'http://example.com/', 'http://site.com/' ), // Absolute URL provided 44 array( '/location', '', '/location' ), // No current url provided 19 45 20 $actual = WP_Http::make_absolute_url( $relative_url, $absolute_url ); 21 $this->assertEquals( $expected, $actual ); 22 } 46 array( '', 'http://example.com', 'http://example.com/' ), // No location provided 23 47 24 function make_absolute_url_testcases() { 25 // 0: The Location header, 1: The current url, 3: The expected url 26 return array( 27 array( 'http://site.com/', 'http://example.com/', 'http://site.com/' ), // Absolute URL provided 28 array( '/location', '', '/location' ), // No current url provided 48 // Location provided relative to site root 49 array( '/root-relative-link.ext', 'http://example.com/', 'http://example.com/root-relative-link.ext' ), 50 array( '/root-relative-link.ext?with=query', 'http://example.com/index.ext?query', 'http://example.com/root-relative-link.ext?with=query' ), 29 51 30 array( '', 'http://example.com', 'http://example.com/' ), // No location provided 52 // Location provided relative to current file/directory 53 array( 'relative-file.ext', 'http://example.com/', 'http://example.com/relative-file.ext' ), 54 array( 'relative-file.ext', 'http://example.com/filename', 'http://example.com/relative-file.ext' ), 55 array( 'relative-file.ext', 'http://example.com/directory/', 'http://example.com/directory/relative-file.ext' ), 31 56 32 // Location provided relative to site root 33 array( '/root-relative-link.ext', 'http://example.com/', 'http://example.com/root-relative-link.ext' ), 34 array( '/root-relative-link.ext?with=query', 'http://example.com/index.ext?query', 'http://example.com/root-relative-link.ext?with=query' ), 57 // Location provided relative to current file/directory but in a parent directory 58 array( '../file-in-parent.ext', 'http://example.com', 'http://example.com/file-in-parent.ext' ), 59 array( '../file-in-parent.ext', 'http://example.com/filename', 'http://example.com/file-in-parent.ext' ), 60 array( '../file-in-parent.ext', 'http://example.com/directory/', 'http://example.com/file-in-parent.ext' ), 61 array( '../file-in-parent.ext', 'http://example.com/directory/filename', 'http://example.com/file-in-parent.ext' ), 35 62 36 // Location provided relative to current file/directory 37 array( 'relative-file.ext', 'http://example.com/', 'http://example.com/relative-file.ext' ), 38 array( 'relative-file.ext', 'http://example.com/filename', 'http://example.com/relative-file.ext' ), 39 array( 'relative-file.ext', 'http://example.com/directory/', 'http://example.com/directory/relative-file.ext' ), 63 // Location provided in muliple levels higher, including impossible to reach (../ below DOCROOT) 64 array( '../../file-in-grand-parent.ext', 'http://example.com', 'http://example.com/file-in-grand-parent.ext' ), 65 array( '../../file-in-grand-parent.ext', 'http://example.com/filename', 'http://example.com/file-in-grand-parent.ext' ), 66 array( '../../file-in-grand-parent.ext', 'http://example.com/directory/', 'http://example.com/file-in-grand-parent.ext' ), 67 array( '../../file-in-grand-parent.ext', 'http://example.com/directory/filename/', 'http://example.com/file-in-grand-parent.ext' ), 68 array( '../../file-in-grand-parent.ext', 'http://example.com/directory1/directory2/filename', 'http://example.com/file-in-grand-parent.ext' ), 40 69 41 // Location provided relative to current file/directory but in a parent directory 42 array( '../file-in-parent.ext', 'http://example.com', 'http://example.com/file-in-parent.ext' ),43 array( '../file-in-parent.ext', 'http://example.com/filename', 'http://example.com/file-in-parent.ext' ),44 array( '../file-in-parent.ext', 'http://example.com/directory/', 'http://example.com/file-in-parent.ext' ),45 array( '../file-in-parent.ext', 'http://example.com/directory/filename', 'http://example.com/file-in-parent.ext' ),70 // Query strings should attach, or replace existing query string. 71 array( '?query=string', 'http://example.com', 'http://example.com/?query=string' ), 72 array( '?query=string', 'http://example.com/file.ext', 'http://example.com/file.ext?query=string' ), 73 array( '?query=string', 'http://example.com/file.ext?existing=query-string', 'http://example.com/file.ext?query=string' ), 74 array( 'otherfile.ext?query=string', 'http://example.com/file.ext?existing=query-string', 'http://example.com/otherfile.ext?query=string' ), 46 75 47 // Location provided in muliple levels higher, including impossible to reach (../ below DOCROOT) 48 array( '../../file-in-grand-parent.ext', 'http://example.com', 'http://example.com/file-in-grand-parent.ext' ), 49 array( '../../file-in-grand-parent.ext', 'http://example.com/filename', 'http://example.com/file-in-grand-parent.ext' ), 50 array( '../../file-in-grand-parent.ext', 'http://example.com/directory/', 'http://example.com/file-in-grand-parent.ext' ), 51 array( '../../file-in-grand-parent.ext', 'http://example.com/directory/filename/', 'http://example.com/file-in-grand-parent.ext' ), 52 array( '../../file-in-grand-parent.ext', 'http://example.com/directory1/directory2/filename', 'http://example.com/file-in-grand-parent.ext' ), 76 // A file with a leading dot 77 array( '.ext', 'http://example.com/', 'http://example.com/.ext' ), 53 78 54 // Query strings should attach, or replace existing query string. 55 array( '?query=string', 'http://example.com', 'http://example.com/?query=string' ), 56 array( '?query=string', 'http://example.com/file.ext', 'http://example.com/file.ext?query=string' ), 57 array( '?query=string', 'http://example.com/file.ext?existing=query-string', 'http://example.com/file.ext?query=string' ), 58 array( 'otherfile.ext?query=string', 'http://example.com/file.ext?existing=query-string', 'http://example.com/otherfile.ext?query=string' ), 79 // URls within URLs 80 array( '/expected', 'http://example.com/sub/http://site.com/sub/', 'http://example.com/expected' ), 81 array( '/expected/http://site.com/sub/', 'http://example.com/', 'http://example.com/expected/http://site.com/sub/' ), 59 82 60 // A file with a leading dot 61 array( '.ext', 'http://example.com/', 'http://example.com/.ext' ), 83 // Schemeless URL's (Not valid in HTTP Headers, but may be used elsewhere) 84 array( '//example.com/sub/', 'https://example.net', 'https://example.com/sub/' ), 85 ); 86 } 62 87 63 // URls within URLs 64 array( '/expected', 'http://example.com/sub/http://site.com/sub/', 'http://example.com/expected' ), 65 array( '/expected/http://site.com/sub/', 'http://example.com/', 'http://example.com/expected/http://site.com/sub/' ), 88 /** 89 * Test WP Parse URL. 90 * 91 * @access public 92 * @param mixed $url URL. 93 * @param mixed $expected Expected. 94 * @dataProvider parse_url_testcases 95 * @return void 96 */ 97 function test_wp_parse_url( $url, $expected ) 98 { 99 $actual = wp_parse_url($url); 100 $this->assertEquals($expected, $actual); 101 } 102 103 /** 104 * Parse URL Test Cases. 105 * 106 * @access public 107 * @return void 108 */ 109 function parse_url_testcases() 110 { 111 // 0: The URL, 1: The expected resulting structure. 112 return array( 113 array( self::FULL_TEST_URL, array( 114 'scheme' => 'http', 115 'host' => 'host.name', 116 'port' => 9090, 117 'user' => 'username', 118 'pass' => 'password', 119 'path' => '/path', 120 'query' => 'arg1=value1&arg2=value2', 121 'fragment' => 'anchor', 122 ) ), 123 array( 'http://example.com/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/' ) ), 66 124 67 // Schemeless URL's (Not valid in HTTP Headers, but may be used elsewhere) 68 array( '//example.com/sub/', 'https://example.net', 'https://example.com/sub/'),69 ); 70 } 125 // < PHP 5.4.7: Schemeless URL. 126 array( '//example.com/path/', array( 'host' => 'example.com', 'path' => '/path/' ) ), 127 array( '//example.com/', array( 'host' => 'example.com', 'path' => '/' ) ), 128 array( 'http://example.com//path/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '//path/' ) ), 71 129 72 /** 73 * @dataProvider parse_url_testcases 74 */ 75 function test_wp_parse_url( $url, $expected ) { 76 $actual = wp_parse_url( $url ); 77 $this->assertEquals( $expected, $actual ); 78 } 130 // < PHP 5.4.7: Scheme separator in the URL. 131 array( 'http://example.com/http://example.net/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/http://example.net/' ) ), 132 array( '/path/http://example.net/', array( 'path' => '/path/http://example.net/' ) ), 79 133 80 function parse_url_testcases() { 81 // 0: The URL, 1: The expected resulting structure 82 return array( 83 array( self::FULL_TEST_URL, array( 84 'scheme' => 'http', 85 'host' => 'host.name', 86 'port' => 9090, 87 'user' => 'username', 88 'pass' => 'password', 89 'path' => '/path', 90 'query' => 'arg1=value1&arg2=value2', 91 'fragment' => 'anchor', 92 ) ), 93 array( 'http://example.com/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/' ) ), 134 // < PHP 5.4.7: IPv6 literals in schemeless URLs are handled incorrectly. 135 array( '//[::FFFF::127.0.0.1]/', array( 'host' => '[::FFFF::127.0.0.1]', 'path' => '/' ) ), 94 136 95 // < PHP 5.4.7: Schemeless URL 96 array( '//example.com/path/', array( 'host' => 'example.com', 'path' => '/path/' ) ), 97 array( '//example.com/', array( 'host' => 'example.com', 'path' => '/' ) ), 98 array( 'http://example.com//path/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '//path/' ) ), 137 // PHP's parse_url() calls this an invalid url, we handle it as a path 138 array( '/://example.com/', array( 'path' => '/://example.com/' ) ), 99 139 100 // < PHP 5.4.7: Scheme separator in the URL. 101 array( 'http://example.com/http://example.net/', array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/http://example.net/' ) ), 102 array( '/path/http://example.net/', array( 'path' => '/path/http://example.net/' ) ), 140 // Schemeless URL containing colons cause parse errors in PHP 7+. 141 array( 142 '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', 143 array( 144 'host' => 'fonts.googleapis.com', 145 'path' => '/css', 146 'query' => 'family=Open+Sans:400&subset=latin', 147 ), 148 ), 149 array( 150 '//fonts.googleapis.com/css?family=Open+Sans:400', 151 array( 152 'host' => 'fonts.googleapis.com', 153 'path' => '/css', 154 'query' => 'family=Open+Sans:400', 155 ), 156 ), 103 157 104 // < PHP 5.4.7: IPv6 literals in schemeless URLs are handled incorrectly. 105 array( '//[::FFFF::127.0.0.1]/', array( 'host' => '[::FFFF::127.0.0.1]', 'path' => '/' ) ), 158 array( 'filenamefound', array( 'path' => 'filenamefound' ) ), 106 159 107 // PHP's parse_url() calls this an invalid url, we handle it as a path 108 array( '/://example.com/', array( 'path' => '/://example.com/' ) ), 160 // Empty string or non-string passed in. 161 array( '', array( 'path' => '' ) ), 162 array( 123, array( 'path' => '123' ) ), 163 ); 164 /* 165 Untestable edge cases in various PHP: 166 - ///example.com - Fails in PHP >= 5.4.7, assumed path in <5.4.7 167 - ://example.com - assumed path in PHP >= 5.4.7, fails in <5.4.7 168 */ 169 } 109 170 110 // Schemeless URL containing colons cause parse errors in PHP 7+. 111 array( 112 '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', 113 array( 114 'host' => 'fonts.googleapis.com', 115 'path' => '/css', 116 'query' => 'family=Open+Sans:400&subset=latin', 117 ), 118 ), 119 array( 120 '//fonts.googleapis.com/css?family=Open+Sans:400', 121 array( 122 'host' => 'fonts.googleapis.com', 123 'path' => '/css', 124 'query' => 'family=Open+Sans:400', 125 ), 126 ), 171 /** 172 * Test WP Parse URL with Default Component. 173 * 174 * @ticket 36356 175 * @access public 176 * @return void 177 */ 178 function test_wp_parse_url_with_default_component() 179 { 180 $actual = wp_parse_url(self::FULL_TEST_URL, -1); 181 $this->assertEquals( 182 array( 183 'scheme' => 'http', 184 'host' => 'host.name', 185 'port' => 9090, 186 'user' => 'username', 187 'pass' => 'password', 188 'path' => '/path', 189 'query' => 'arg1=value1&arg2=value2', 190 'fragment' => 'anchor', 191 ), $actual 192 ); 193 } 127 194 128 array( 'filenamefound', array( 'path' => 'filenamefound' ) ), 195 /** 196 * @ticket 36356 197 * 198 * @dataProvider parse_url_component_testcases 199 */ 200 function test_wp_parse_url_with_component( $url, $component, $expected ) 201 { 202 $actual = wp_parse_url($url, $component); 203 $this->assertSame($expected, $actual); 204 } 205 206 /** 207 * Parse URL Component Test Cases. 208 * 209 * @access public 210 * @return void 211 */ 212 function parse_url_component_testcases() 213 { 214 // 0: The URL, 1: The requested component, 2: The expected resulting structure. 215 return array( 216 array( self::FULL_TEST_URL, PHP_URL_SCHEME, 'http' ), 217 array( self::FULL_TEST_URL, PHP_URL_USER, 'username' ), 218 array( self::FULL_TEST_URL, PHP_URL_PASS, 'password' ), 219 array( self::FULL_TEST_URL, PHP_URL_HOST, 'host.name' ), 220 array( self::FULL_TEST_URL, PHP_URL_PORT, 9090 ), 221 array( self::FULL_TEST_URL, PHP_URL_PATH, '/path' ), 222 array( self::FULL_TEST_URL, PHP_URL_QUERY, 'arg1=value1&arg2=value2' ), 223 array( self::FULL_TEST_URL, PHP_URL_FRAGMENT, 'anchor' ), 129 224 130 // Empty string or non-string passed in. 131 array( '', array( 'path' => '' ) ), 132 array( 123, array( 'path' => '123' ) ), 133 ); 134 /* 135 Untestable edge cases in various PHP: 136 - ///example.com - Fails in PHP >= 5.4.7, assumed path in <5.4.7 137 - ://example.com - assumed path in PHP >= 5.4.7, fails in <5.4.7 138 */ 139 } 225 // < PHP 5.4.7: Schemeless URL. 226 array( '//example.com/path/', PHP_URL_HOST, 'example.com' ), 227 array( '//example.com/path/', PHP_URL_PATH, '/path/' ), 228 array( '//example.com/', PHP_URL_HOST, 'example.com' ), 229 array( '//example.com/', PHP_URL_PATH, '/' ), 230 array( 'http://example.com//path/', PHP_URL_HOST, 'example.com' ), 231 array( 'http://example.com//path/', PHP_URL_PATH, '//path/' ), 140 232 141 /** 142 * @ticket 36356 143 */ 144 function test_wp_parse_url_with_default_component() { 145 $actual = wp_parse_url( self::FULL_TEST_URL, -1 ); 146 $this->assertEquals( array( 147 'scheme' => 'http', 148 'host' => 'host.name', 149 'port' => 9090, 150 'user' => 'username', 151 'pass' => 'password', 152 'path' => '/path', 153 'query' => 'arg1=value1&arg2=value2', 154 'fragment' => 'anchor', 155 ), $actual ); 156 } 233 // < PHP 5.4.7: Scheme separator in the URL. 234 array( 'http://example.com/http://example.net/', PHP_URL_HOST, 'example.com' ), 235 array( 'http://example.com/http://example.net/', PHP_URL_PATH, '/http://example.net/' ), 236 array( '/path/http://example.net/', PHP_URL_HOST, null ), 237 array( '/path/http://example.net/', PHP_URL_PATH, '/path/http://example.net/' ), 157 238 158 /** 159 * @ticket 36356 160 * 161 * @dataProvider parse_url_component_testcases 162 */ 163 function test_wp_parse_url_with_component( $url, $component, $expected ) { 164 $actual = wp_parse_url( $url, $component ); 165 $this->assertSame( $expected, $actual ); 166 } 239 // < PHP 5.4.7: IPv6 literals in schemeless URLs are handled incorrectly. 240 array( '//[::FFFF::127.0.0.1]/', PHP_URL_HOST, '[::FFFF::127.0.0.1]' ), 241 array( '//[::FFFF::127.0.0.1]/', PHP_URL_PATH, '/' ), 167 242 168 function parse_url_component_testcases() { 169 // 0: The URL, 1: The requested component, 2: The expected resulting structure. 170 return array( 171 array( self::FULL_TEST_URL, PHP_URL_SCHEME, 'http' ), 172 array( self::FULL_TEST_URL, PHP_URL_USER, 'username' ), 173 array( self::FULL_TEST_URL, PHP_URL_PASS, 'password' ), 174 array( self::FULL_TEST_URL, PHP_URL_HOST, 'host.name' ), 175 array( self::FULL_TEST_URL, PHP_URL_PORT, 9090 ), 176 array( self::FULL_TEST_URL, PHP_URL_PATH, '/path' ), 177 array( self::FULL_TEST_URL, PHP_URL_QUERY, 'arg1=value1&arg2=value2' ), 178 array( self::FULL_TEST_URL, PHP_URL_FRAGMENT, 'anchor' ), 243 // PHP's parse_url() calls this an invalid URL, we handle it as a path. 244 array( '/://example.com/', PHP_URL_PATH, '/://example.com/' ), 179 245 180 // < PHP 5.4.7: Schemeless URL. 181 array( '//example.com/path/', PHP_URL_HOST, 'example.com' ), 182 array( '//example.com/path/', PHP_URL_PATH, '/path/' ), 183 array( '//example.com/', PHP_URL_HOST, 'example.com' ), 184 array( '//example.com/', PHP_URL_PATH, '/' ), 185 array( 'http://example.com//path/', PHP_URL_HOST, 'example.com' ), 186 array( 'http://example.com//path/', PHP_URL_PATH, '//path/' ), 246 // Schemeless URL containing colons cause parse errors in PHP 7+. 247 array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_HOST, 'fonts.googleapis.com' ), 248 array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_PORT, null ), 249 array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_PATH, '/css' ), 250 array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_QUERY, 'family=Open+Sans:400&subset=latin' ), 251 array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_HOST, 'fonts.googleapis.com' ), // 25 252 array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_PORT, null ), 253 array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_PATH, '/css' ), //27 254 array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_QUERY, 'family=Open+Sans:400' ), //28 187 255 188 // < PHP 5.4.7: Scheme separator in the URL. 189 array( 'http://example.com/http://example.net/', PHP_URL_HOST, 'example.com' ), 190 array( 'http://example.com/http://example.net/', PHP_URL_PATH, '/http://example.net/' ), 191 array( '/path/http://example.net/', PHP_URL_HOST, null ), 192 array( '/path/http://example.net/', PHP_URL_PATH, '/path/http://example.net/' ), 256 // Empty string or non-string passed in. 257 array( '', PHP_URL_PATH, '' ), 258 array( '', PHP_URL_QUERY, null ), 259 array( 123, PHP_URL_PORT, null ), 260 array( 123, PHP_URL_PATH, '123' ), 261 ); 262 } 193 263 194 // < PHP 5.4.7: IPv6 literals in schemeless URLs are handled incorrectly. 195 array( '//[::FFFF::127.0.0.1]/', PHP_URL_HOST, '[::FFFF::127.0.0.1]' ), 196 array( '//[::FFFF::127.0.0.1]/', PHP_URL_PATH, '/' ), 264 /** 265 * @ticket 35426 266 */ 267 public function test_http_response_code_constants() 268 { 269 global $wp_header_to_desc; 197 270 198 // PHP's parse_url() calls this an invalid URL, we handle it as a path. 199 array( '/://example.com/', PHP_URL_PATH, '/://example.com/' ), 271 $ref = new ReflectionClass('WP_Http'); 272 $constants = $ref->getConstants(); 200 273 201 // Schemeless URL containing colons cause parse errors in PHP 7+. 202 array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_HOST, 'fonts.googleapis.com' ), 203 array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_PORT, null ), 204 array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_PATH, '/css' ), 205 array( '//fonts.googleapis.com/css?family=Open+Sans:400&subset=latin', PHP_URL_QUERY, 'family=Open+Sans:400&subset=latin' ), 206 array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_HOST, 'fonts.googleapis.com' ), // 25 207 array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_PORT, null ), 208 array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_PATH, '/css' ), //27 209 array( '//fonts.googleapis.com/css?family=Open+Sans:400', PHP_URL_QUERY, 'family=Open+Sans:400' ), //28 274 // This primes the `$wp_header_to_desc` global: 275 get_status_header_desc(200); 210 276 211 // Empty string or non-string passed in. 212 array( '', PHP_URL_PATH, '' ), 213 array( '', PHP_URL_QUERY, null ), 214 array( 123, PHP_URL_PORT, null ), 215 array( 123, PHP_URL_PATH, '123' ), 216 ); 217 } 277 $this->assertEquals(array_keys($wp_header_to_desc), array_values($constants)); 218 278 219 /** 220 * @ticket 35426 221 */ 222 public function test_http_response_code_constants() { 223 global $wp_header_to_desc; 279 } 224 280 225 $ref = new ReflectionClass( 'WP_Http' ); 226 $constants = $ref->getConstants(); 281 /** 282 * @ticket 37768 283 */ 284 public function test_normalize_cookies_scalar_values() 285 { 286 $http = _wp_http_get_object(); 227 287 228 // This primes the `$wp_header_to_desc` global: 229 get_status_header_desc( 200 ); 288 $cookies = array( 289 'x' => 'foo', 290 'y' => 2, 291 'z' => 0.45, 292 'foo' => array( 'bar' ), 293 ); 230 294 231 $this->assertEquals( array_keys( $wp_header_to_desc ), array_values( $constants ) ); 295 $cookie_jar = $http->normalize_cookies( 296 array( 297 'x' => 'foo', 298 'y' => 2, 299 'z' => 0.45, 300 'foo' => array( 'bar' ), 301 ) 302 ); 232 303 233 } 304 $this->assertInstanceOf('Requests_Cookie_Jar', $cookie_jar); 234 305 235 /** 236 * @ticket 37768 237 */ 238 public function test_normalize_cookies_scalar_values() { 239 $http = _wp_http_get_object(); 306 foreach( array_keys($cookies) as $cookie ) { 307 if ('foo' === $cookie ) { 308 $this->assertFalse(isset($cookie_jar[ $cookie ])); 309 } else { 310 $this->assertInstanceOf('Requests_Cookie', $cookie_jar[ $cookie ]); 311 } 312 } 313 } 240 314 241 $cookies = array( 242 'x' => 'foo', 243 'y' => 2, 244 'z' => 0.45, 245 'foo' => array( 'bar' ), 246 ); 315 /** 316 * @ticket 36356 317 * 318 * @dataProvider get_component_from_parsed_url_array_testcases 319 */ 320 function test_get_component_from_parsed_url_array( $url, $component, $expected ) 321 { 322 $parts = wp_parse_url($url); 323 $actual = _get_component_from_parsed_url_array($parts, $component); 324 $this->assertSame($expected, $actual); 325 } 326 327 /** 328 * Get Component from Parsed URL Array TestCases. 329 * 330 * @access public 331 * @return void 332 */ 333 function get_component_from_parsed_url_array_testcases() 334 { 335 // 0: A URL, 1: PHP URL constant, 2: The expected result. 336 return array( 337 array( 'http://example.com/', -1, array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/' ) ), 338 array( 'http://example.com/', -1, array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/' ) ), 339 array( 'http://example.com/', PHP_URL_HOST, 'example.com' ), 340 array( 'http://example.com/', PHP_URL_USER, null ), 341 array( 'http:///example.com', -1, false ), // Malformed. 342 array( 'http:///example.com', PHP_URL_HOST, null ), // Malformed. 343 ); 344 } 247 345 248 $cookie_jar = $http->normalize_cookies( array( 249 'x' => 'foo', 250 'y' => 2, 251 'z' => 0.45, 252 'foo' => array( 'bar' ), 253 ) ); 346 /** 347 * @ticket 36356 348 * 349 * @dataProvider wp_translate_php_url_constant_to_key_testcases 350 */ 351 function test_wp_translate_php_url_constant_to_key( $input, $expected ) 352 { 353 $actual = _wp_translate_php_url_constant_to_key($input); 354 $this->assertSame($expected, $actual); 355 } 356 357 /** 358 * WP Translate PHP URL Constant to Key Testcases. 359 * 360 * @access public 361 * @return void 362 */ 363 function wp_translate_php_url_constant_to_key_testcases() 364 { 365 // 0: PHP URL constant, 1: The expected result. 366 return array( 367 array( PHP_URL_SCHEME, 'scheme' ), 368 array( PHP_URL_HOST, 'host' ), 369 array( PHP_URL_PORT, 'port' ), 370 array( PHP_URL_USER, 'user' ), 371 array( PHP_URL_PASS, 'pass' ), 372 array( PHP_URL_PATH, 'path' ), 373 array( PHP_URL_QUERY, 'query' ), 374 array( PHP_URL_FRAGMENT, 'fragment' ), 254 375 255 $this->assertInstanceOf( 'Requests_Cookie_Jar', $cookie_jar ); 376 // Test with non-PHP_URL_CONSTANT parameter. 377 array( 'something', false ), 378 array( ABSPATH, false ), 379 ); 380 } 256 381 257 foreach( array_keys( $cookies ) as $cookie ) {258 if ( 'foo' === $cookie ) {259 $this->assertFalse( isset( $cookie_jar[ $cookie ] ) );260 } else {261 $this->assertInstanceOf( 'Requests_Cookie', $cookie_jar[ $cookie ] );262 }263 }264 }265 266 /**267 * @ticket 36356268 *269 * @dataProvider get_component_from_parsed_url_array_testcases270 */271 function test_get_component_from_parsed_url_array( $url, $component, $expected ) {272 $parts = wp_parse_url( $url );273 $actual = _get_component_from_parsed_url_array( $parts, $component );274 $this->assertSame( $expected, $actual );275 }276 277 function get_component_from_parsed_url_array_testcases() {278 // 0: A URL, 1: PHP URL constant, 2: The expected result.279 return array(280 array( 'http://example.com/', -1, array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/' ) ),281 array( 'http://example.com/', -1, array( 'scheme' => 'http', 'host' => 'example.com', 'path' => '/' ) ),282 array( 'http://example.com/', PHP_URL_HOST, 'example.com' ),283 array( 'http://example.com/', PHP_URL_USER, null ),284 array( 'http:///example.com', -1, false ), // Malformed.285 array( 'http:///example.com', PHP_URL_HOST, null ), // Malformed.286 );287 }288 289 /**290 * @ticket 36356291 *292 * @dataProvider wp_translate_php_url_constant_to_key_testcases293 */294 function test_wp_translate_php_url_constant_to_key( $input, $expected ) {295 $actual = _wp_translate_php_url_constant_to_key( $input );296 $this->assertSame( $expected, $actual );297 }298 299 function wp_translate_php_url_constant_to_key_testcases() {300 // 0: PHP URL constant, 1: The expected result.301 return array(302 array( PHP_URL_SCHEME, 'scheme' ),303 array( PHP_URL_HOST, 'host' ),304 array( PHP_URL_PORT, 'port' ),305 array( PHP_URL_USER, 'user' ),306 array( PHP_URL_PASS, 'pass' ),307 array( PHP_URL_PATH, 'path' ),308 array( PHP_URL_QUERY, 'query' ),309 array( PHP_URL_FRAGMENT, 'fragment' ),310 311 // Test with non-PHP_URL_CONSTANT parameter.312 array( 'something', false ),313 array( ABSPATH, false ),314 );315 }316 317 382 } -
remoteRetrieveHeaders.php
1 1 <?php 2 2 3 3 /** 4 * @group http 4 * Tests_HTTP_RemoteRetrieveHeaders class. 5 * 6 * @group http 7 * @extends WP_UnitTestCase 5 8 */ 6 class Tests_HTTP_RemoteRetrieveHeaders extends WP_UnitTestCase { 9 class Tests_HTTP_RemoteRetrieveHeaders extends WP_UnitTestCase 10 { 7 11 8 /** 9 * Valid response 10 */ 11 function test_remote_retrieve_headers_valid_response() { 12 $headers = 'headers_data'; 13 $response = array( 'headers' => $headers ); 12 /** 13 * Test Remote Retrieve Headers has valid response. 14 * 15 * @access public 16 * @return void 17 */ 18 function test_remote_retrieve_headers_valid_response() 19 { 20 $headers = 'headers_data'; 21 $response = array( 'headers' => $headers ); 14 22 15 $result = wp_remote_retrieve_headers( $response);16 $this->assertEquals( $headers, $result);17 23 $result = wp_remote_retrieve_headers($response); 24 $this->assertEquals($headers, $result); 25 } 18 26 19 /** 20 * Response is a WP_Error 21 */ 22 function test_remote_retrieve_headers_is_error() { 23 $response = new WP_Error( 'Some error' ); 27 /** 28 * Response is a WP_Error 29 * 30 * @access public 31 * @return void 32 */ 33 function test_remote_retrieve_headers_is_error() 34 { 35 $response = new WP_Error('Some error'); 24 36 25 $result = wp_remote_retrieve_headers( $response);26 $this->assertEquals( array(), $result);27 37 $result = wp_remote_retrieve_headers($response); 38 $this->assertEquals(array(), $result); 39 } 28 40 29 /** 30 * Response does not contain 'headers' 31 */ 32 function test_remote_retrieve_headers_invalid_response() { 33 $response = array( 'no_headers' => 'set'); 41 /** 42 * Response does not contain 'headers'. 43 * 44 * @access public 45 * @return void 46 */ 47 function test_remote_retrieve_headers_invalid_response() 48 { 49 $response = array( 'no_headers' => 'set'); 34 50 35 $result = wp_remote_retrieve_headers( $response);36 $this->assertEquals( array(), $result);37 51 $result = wp_remote_retrieve_headers($response); 52 $this->assertEquals(array(), $result); 53 } 38 54 } -
streams.php
1 1 <?php 2 2 3 require_once dirname( __FILE__) . '/base.php';3 require_once dirname(__FILE__) . '/base.php'; 4 4 5 5 /** 6 * @group http 7 * @group external-http 6 * Tests_HTTP_streams class. 7 * 8 * @group http 9 * @group external-http 10 * @extends WP_HTTP_UnitTestCase 8 11 */ 9 class Tests_HTTP_streams extends WP_HTTP_UnitTestCase { 10 var $transport = 'streams'; 12 class Tests_HTTP_streams extends WP_HTTP_UnitTestCase 13 { 14 15 /** 16 * Transport. 17 * 18 * (default value: 'streams') 19 * 20 * @var string 21 * @access public 22 */ 23 var $transport = 'streams'; 11 24 }