Changeset 50102 for branches/4.2
- Timestamp:
- 01/30/2021 08:46:48 PM (4 years ago)
- Location:
- branches/4.2
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.2
- Property svn:mergeinfo changed
/trunk merged: 38757,43511-43512,46682,46996
- Property svn:mergeinfo changed
-
branches/4.2/tests/phpunit/includes/testcase.php
r40245 r50102 110 110 $this->markTestSkipped( 'For automated test runs, this test is only run on trunk/master' ); 111 111 } 112 } 113 114 /** 115 * Allow tests to be skipped if the HTTP request times out. 116 * 117 * @param array|WP_Error $response HTTP response. 118 */ 119 public function skipTestOnTimeout( $response ) { 120 if ( ! is_wp_error( $response ) ) { 121 return; 122 } 123 if ( 'connect() timed out!' === $response->get_error_message() ) { 124 $this->markTestSkipped( 'HTTP timeout' ); 125 } 126 127 if ( false !== strpos( $response->get_error_message(), 'timed out after' ) ) { 128 $this->markTestSkipped( 'HTTP timeout' ); 129 } 130 131 if ( 0 === strpos( $response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80' ) ) { 132 $this->markTestSkipped( 'HTTP timeout' ); 133 } 134 112 135 } 113 136 -
branches/4.2/tests/phpunit/tests/http/base.php
r31290 r50102 45 45 // 5 : 5 & 301 46 46 $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 5) ); 47 48 $this->skipTestOnTimeout( $res ); 47 49 $this->assertFalse( is_wp_error($res) ); 48 50 $this->assertEquals(200, (int)$res['response']['code'] ); … … 52 54 // 5 : 5 & 302 53 55 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 5) ); 56 57 $this->skipTestOnTimeout( $res ); 54 58 $this->assertFalse( is_wp_error($res) ); 55 59 $this->assertEquals(200, (int)$res['response']['code'] ); … … 62 66 // 5 > 0 & 301 63 67 $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 0) ); 68 69 $this->skipTestOnTimeout( $res ); 64 70 $this->assertFalse( is_wp_error($res) ); 65 71 $this->assertEquals(301, (int)$res['response']['code'] ); … … 72 78 // 5 > 0 & 302 73 79 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) ); 80 81 $this->skipTestOnTimeout( $res ); 74 82 $this->assertFalse( is_wp_error($res) ); 75 83 $this->assertEquals(302, (int)$res['response']['code'] ); … … 79 87 // 5 - 5 80 88 $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5) ); 89 90 $this->skipTestOnTimeout( $res ); 81 91 $this->assertFalse( is_wp_error($res) ); 82 92 $this->assertEquals(200, (int)$res['response']['code'] ); … … 86 96 // No redirections on HEAD request: 87 97 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 1, array('method' => 'HEAD') ); 98 99 $this->skipTestOnTimeout( $res ); 88 100 $this->assertFalse( is_wp_error($res) ); 89 101 $this->assertEquals( 302, (int)$res['response']['code'] ); … … 96 108 // Redirections on HEAD request when Requested 97 109 $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5, 'method' => 'HEAD') ); 110 111 $this->skipTestOnTimeout( $res ); 98 112 $this->assertFalse( is_wp_error($res) ); 99 113 $this->assertEquals( 200, (int)$res['response']['code'] ); … … 103 117 // 10 > 5 104 118 $res = wp_remote_request($this->redirection_script . '?rt=' . 10, array('redirection' => 5) ); 119 120 $this->skipTestOnTimeout( $res ); 105 121 $this->assertTrue( is_wp_error($res), print_r($res, true) ); 106 122 } … … 109 125 // 6 > 5 (close edgecase) 110 126 $res = wp_remote_request($this->redirection_script . '?rt=' . 6, array('redirection' => 5) ); 127 128 $this->skipTestOnTimeout( $res ); 111 129 $this->assertTrue( is_wp_error($res) ); 112 130 } … … 115 133 // 4 < 5 (close edgecase) 116 134 $res = wp_remote_request($this->redirection_script . '?rt=' . 4, array('redirection' => 5) ); 135 136 $this->skipTestOnTimeout( $res ); 117 137 $this->assertFalse( is_wp_error($res) ); 118 138 } … … 124 144 // 0 redirections asked for, Should return the document? 125 145 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) ); 146 147 $this->skipTestOnTimeout( $res ); 126 148 $this->assertFalse( is_wp_error($res) ); 127 149 $this->assertEquals( 302, (int)$res['response']['code'] ); … … 136 158 // Prints PASS on initial load, FAIL if the client follows the specified redirection 137 159 $res = wp_remote_request( $this->redirection_script . '?201-location=true' ); 160 161 $this->skipTestOnTimeout( $res ); 138 162 $this->assertFalse( is_wp_error( $res ) ); 139 163 $this->assertEquals( 'PASS', $res['body']); … … 150 174 // Test 301 - POST to POST 151 175 $res = wp_remote_request( $url, array( 'method' => 'PUT', 'timeout' => 30 ) ); 176 177 $this->skipTestOnTimeout( $res ); 152 178 $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) ); 153 179 $this->assertTrue( !empty( $res['headers']['location'] ) ); … … 162 188 $res = wp_remote_request( $this->redirection_script . '?header-check', array('headers' => $headers) ); 163 189 190 $this->skipTestOnTimeout( $res ); 164 191 $this->assertFalse( is_wp_error($res) ); 165 192 … … 189 216 $filesize = filesize( $res['filename'] ); 190 217 unlink( $res['filename'] ); 191 } 192 218 } 219 220 $this->skipTestOnTimeout( $res ); 193 221 $this->assertFalse( is_wp_error( $res ) ); 194 222 $this->assertEquals( '', $res['body'] ); // The body should be empty. … … 211 239 } 212 240 241 $this->skipTestOnTimeout( $res ); 213 242 $this->assertFalse( is_wp_error( $res ) ); 214 243 $this->assertEquals( $size, $filesize ); // Check that the file is written to disk correctly without any extra characters … … 228 257 $res = wp_remote_request( $url, array( 'timeout' => 30, 'limit_response_size' => $size ) ); 229 258 259 $this->skipTestOnTimeout( $res ); 230 260 $this->assertFalse( is_wp_error( $res ) ); 231 261 $this->assertEquals( $size, strlen( $res['body'] ) ); … … 235 265 * Test POST redirection methods 236 266 * 267 * @dataProvider data_post_redirect_to_method_300 268 * 237 269 * @ticket 17588 238 270 */ 239 function test_post_redirect_to_method_300( ) {271 function test_post_redirect_to_method_300( $response_code, $method ) { 240 272 $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?post-redirect-to-method=1'; 241 273 242 // Test 300 - POST to POST 243 $res = wp_remote_post( add_query_arg( 'response_code', 300, $url ), array( 'timeout' => 30 ) ); 244 $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) ); 245 246 // Test 301 - POST to POST 247 $res = wp_remote_post( add_query_arg( 'response_code', 301, $url ), array( 'timeout' => 30 ) ); 248 $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) ); 249 250 // Test 302 - POST to GET 251 $res = wp_remote_post( add_query_arg( 'response_code', 302, $url ), array( 'timeout' => 30 ) ); 252 $this->assertEquals( 'GET', wp_remote_retrieve_body( $res ) ); 253 254 // Test 303 - POST to GET 255 $res = wp_remote_post( add_query_arg( 'response_code', 303, $url ), array( 'timeout' => 30 ) ); 256 $this->assertEquals( 'GET', wp_remote_retrieve_body( $res ) ); 257 258 // Test 304 - POST to POST 259 $res = wp_remote_post( add_query_arg( 'response_code', 304, $url ), array( 'timeout' => 30 ) ); 260 $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) ); 274 $res = wp_remote_post( add_query_arg( 'response_code', $response_code, $url ), array( 'timeout' => 30 ) ); 275 276 $this->skipTestOnTimeout( $res ); 277 $this->assertEquals( $method, wp_remote_retrieve_body( $res ) ); 278 } 279 280 public function data_post_redirect_to_method_300() { 281 return array( 282 // Test 300 - POST to POST 283 array( 284 300, 285 'POST', 286 ), 287 // Test 301 - POST to POST 288 array( 289 301, 290 'POST', 291 ), 292 // Test 302 - POST to GET 293 array( 294 302, 295 'GET', 296 ), 297 // Test 303 - POST to GET 298 array( 299 303, 300 'GET', 301 ), 302 // Test 304 - POST to POST 303 array( 304 304, 305 'POST', 306 ), 307 ); 261 308 } 262 309 … … 278 325 279 326 $res = wp_remote_get( $url, $args ); 327 328 $this->skipTestOnTimeout( $res ); 280 329 $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) ); 281 330 … … 291 340 $res = wp_remote_head( $url, array( 'timeout' => 30 ) ); 292 341 342 $this->skipTestOnTimeout( $res ); 293 343 $this->assertInternalType( 'array', wp_remote_retrieve_header( $res, 'location' ) ); 294 344 $this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) ); 295 345 296 346 $res = wp_remote_get( $url, array( 'timeout' => 30 ) ); 347 348 $this->skipTestOnTimeout( $res ); 297 349 $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) ); 298 350 … … 308 360 309 361 $res = wp_remote_get( $url ); 362 363 $this->skipTestOnTimeout( $res ); 310 364 $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) ); 311 365 } … … 322 376 323 377 $res = wp_remote_get( 'https://wordpress.org/' ); 378 379 $this->skipTestOnTimeout( $res ); 324 380 $this->assertTrue( ! is_wp_error( $res ), print_r( $res, true ) ); 325 381 } -
branches/4.2/tests/phpunit/tests/http/functions.php
r31258 r50102 18 18 $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg'; 19 19 $response = wp_remote_head( $url ); 20 21 $this->skipTestOnTimeout( $response ); 22 20 23 $headers = wp_remote_retrieve_headers( $response ); 21 24 … … 30 33 $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 31 34 $response = wp_remote_head( $url ); 35 36 $this->skipTestOnTimeout( $response ); 32 37 $this->assertEquals( '301', wp_remote_retrieve_response_code( $response ) ); 33 38 } … … 35 40 function test_head_404() { 36 41 $url = 'https://asdftestblog1.files.wordpress.com/2007/09/awefasdfawef.jpg'; 37 $ headers= wp_remote_head( $url );42 $response = wp_remote_head( $url ); 38 43 39 $this->assertInternalType( 'array', $headers, "Reply wasn't array." ); 40 $this->assertEquals( '404', wp_remote_retrieve_response_code( $headers ) ); 44 $this->skipTestOnTimeout( $response ); 45 $this->assertInternalType( 'array', $response, "Reply wasn't array." ); 46 $this->assertEquals( '404', wp_remote_retrieve_response_code( $response ) ); 41 47 } 42 48 43 49 function test_get_request() { 44 50 $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg'; 45 $file = tempnam('/tmp', 'testfile');46 51 47 $headers = wp_get_http($url, $file); 52 $response = wp_remote_get( $url ); 53 54 $this->skipTestOnTimeout( $response ); 55 56 $headers = wp_remote_retrieve_headers( $response ); 48 57 49 58 // should return the same headers as a head request … … 51 60 $this->assertEquals( 'image/jpeg', $headers['content-type'] ); 52 61 $this->assertEquals( '40148', $headers['content-length'] ); 53 $this->assertEquals( '200', $headers['response'] ); 54 55 // make sure the file is ok 56 $this->assertEquals( 40148, filesize($file) ); 57 $this->assertEquals( 'b0371a0fc575fcf77f62cd298571f53b', md5_file($file) ); 62 $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) ); 58 63 } 59 64 … … 61 66 // this will redirect to asdftestblog1.files.wordpress.com 62 67 $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 63 $file = tempnam('/tmp', 'testfile');64 68 65 $headers = wp_get_http($url, $file); 69 $response = wp_remote_get( $url ); 70 71 $this->skipTestOnTimeout( $response ); 72 73 $headers = wp_remote_retrieve_headers( $response ); 66 74 67 75 // should return the same headers as a head request … … 69 77 $this->assertEquals( 'image/jpeg', $headers['content-type'] ); 70 78 $this->assertEquals( '40148', $headers['content-length'] ); 71 $this->assertEquals( '200', $headers['response'] ); 72 73 // make sure the file is ok 74 $this->assertEquals( 40148, filesize($file) ); 75 $this->assertEquals( 'b0371a0fc575fcf77f62cd298571f53b', md5_file($file) ); 79 $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) ); 76 80 } 77 81 … … 79 83 // this will redirect to asdftestblog1.files.wordpress.com 80 84 $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg'; 81 $file = tempnam('/tmp', 'testfile'); 85 82 86 // pretend we've already redirected 5 times 83 $headers = wp_get_http( $url, $file, 6 ); 84 $this->assertFalse( $headers ); 87 $response = wp_remote_get( $url, array( 'redirection' => -1 ) ); 88 89 $this->skipTestOnTimeout( $response ); 90 $this->assertTrue( is_wp_error( $response ) ); 85 91 } 86 92 }
Note: See TracChangeset
for help on using the changeset viewer.