WordPress.org

Make WordPress Core

Changeset 50091


Ignore:
Timestamp:
01/30/2021 12:08:49 PM (8 months ago)
Author:
SergeyBiryukov
Message:

Tests: Use skipTestOnTimeout() in more HTTP tests.

Adjust it to handle more types of timeouts, e.g. "Resolving timed out", "Connection timed out".

Merges [38757], [43511], [43512], [46682], [46996] to the 4.4 branch.
See #51669.

Location:
branches/4.4
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/4.4

  • branches/4.4/tests/phpunit/includes/testcase.php

    r40243 r50091  
    150150
    151151    /**
    152      * Allow tests to be skipped on some automated runs
     152     * Allow tests to be skipped on some automated runs.
    153153     *
    154154     * For test runs on Travis for something other than trunk/master
     
    205205            _unregister_post_status( $post_status );
    206206        }
     207    }
     208
     209    /**
     210     * Allow tests to be skipped if the HTTP request times out.
     211     *
     212     * @param array|WP_Error $response HTTP response.
     213     */
     214    public function skipTestOnTimeout( $response ) {
     215        if ( ! is_wp_error( $response ) ) {
     216            return;
     217        }
     218        if ( 'connect() timed out!' === $response->get_error_message() ) {
     219            $this->markTestSkipped( 'HTTP timeout' );
     220        }
     221
     222        if ( false !== strpos( $response->get_error_message(), 'timed out after' ) ) {
     223            $this->markTestSkipped( 'HTTP timeout' );
     224        }
     225
     226        if ( 0 === strpos( $response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80' ) ) {
     227            $this->markTestSkipped( 'HTTP timeout' );
     228        }
     229
    207230    }
    208231
  • branches/4.4/tests/phpunit/tests/external-http/basic.php

    r42103 r50091  
    1010
    1111        $readme = file_get_contents( ABSPATH . 'readme.html' );
     12
    1213        preg_match( '#Recommendations.*PHP</a> version <strong>([0-9.]*)#s', $readme, $matches );
    1314
    1415        $response = wp_remote_get( 'https://secure.php.net/supported-versions.php' );
    15         if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
    16             $this->markTestSkipped( 'Could not contact PHP.net to check versions.' );
     16
     17        $this->skipTestOnTimeout( $response );
     18
     19        $response_code = wp_remote_retrieve_response_code( $response );
     20        if ( 200 !== $response_code ) {
     21            $this->fail( sprintf( 'Could not contact PHP.net to check versions. Response code: %s', $response_code ) );
    1722        }
     23
    1824        $php = wp_remote_retrieve_body( $response );
    1925
     
    2531
    2632        $response = wp_remote_get( "https://dev.mysql.com/doc/relnotes/mysql/{$matches[1]}/en/" );
    27         if ( 200 != wp_remote_retrieve_response_code( $response ) ) {
    28             $this->markTestSkipped( 'Could not contact dev.MySQL.com to check versions.' );
     33
     34        $this->skipTestOnTimeout( $response );
     35
     36        $response_code = wp_remote_retrieve_response_code( $response );
     37        if ( 200 !== $response_code ) {
     38            $this->fail( sprintf( 'Could not contact dev.MySQL.com to check versions. Response code: %s', $response_code ) );
    2939        }
     40
    3041        $mysql = wp_remote_retrieve_body( $response );
    3142
  • branches/4.4/tests/phpunit/tests/http/base.php

    r34874 r50091  
    1818    protected $http_request_args;
    1919
    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         }
    30 
    31         if ( 0 === strpos( $response->get_error_message(), 'Operation timed out after' ) ){
    32             $this->markTestSkipped( 'HTTP timeout' );
    33         }
    34 
    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         }
    38 
    39     }
    40 
    4120    function setUp() {
    4221
     
    7453        // 5 : 5 & 301
    7554        $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 5) );
     55
     56        $this->skipTestOnTimeout( $res );
    7657        $this->assertNotWPError( $res );
    7758        $this->assertEquals(200, (int)$res['response']['code'] );
     
    8162        // 5 : 5 & 302
    8263        $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 5) );
     64
     65        $this->skipTestOnTimeout( $res );
    8366        $this->assertNotWPError( $res );
    8467        $this->assertEquals(200, (int)$res['response']['code'] );
     
    9174        // 5 > 0 & 301
    9275        $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 0) );
     76
     77        $this->skipTestOnTimeout( $res );
    9378        $this->assertNotWPError( $res );
    9479        $this->assertEquals(301, (int)$res['response']['code'] );
     
    10186        // 5 > 0 & 302
    10287        $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) );
     88
     89        $this->skipTestOnTimeout( $res );
    10390        $this->assertNotWPError( $res );
    10491        $this->assertEquals(302, (int)$res['response']['code'] );
     
    10895        // 5 - 5
    10996        $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5) );
     97
     98        $this->skipTestOnTimeout( $res );
    11099        $this->assertNotWPError( $res );
    111100        $this->assertEquals(200, (int)$res['response']['code'] );
     
    115104        // No redirections on HEAD request:
    116105        $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 1, array('method' => 'HEAD') );
     106
     107        $this->skipTestOnTimeout( $res );
    117108        $this->assertNotWPError( $res );
    118109        $this->assertEquals( 302, (int)$res['response']['code'] );
     
    125116        // Redirections on HEAD request when Requested
    126117        $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5, 'method' => 'HEAD') );
     118
     119        $this->skipTestOnTimeout( $res );
    127120        $this->assertNotWPError( $res );
    128121        $this->assertEquals( 200, (int)$res['response']['code'] );
     
    132125        // 10 > 5
    133126        $res = wp_remote_request($this->redirection_script . '?rt=' . 10, array('redirection' => 5) );
    134         $this->assertTrue( is_wp_error($res), print_r($res, true) );
     127
     128        $this->skipTestOnTimeout( $res );
     129        $this->assertWPError( $res );
    135130    }
    136131
     
    138133        // 6 > 5 (close edgecase)
    139134        $res = wp_remote_request($this->redirection_script . '?rt=' . 6, array('redirection' => 5) );
    140         $this->assertTrue( is_wp_error($res) );
     135
     136        $this->skipTestOnTimeout( $res );
     137        $this->assertWPError( $res );
    141138    }
    142139
     
    144141        // 4 < 5 (close edgecase)
    145142        $res = wp_remote_request($this->redirection_script . '?rt=' . 4, array('redirection' => 5) );
     143
     144        $this->skipTestOnTimeout( $res );
    146145        $this->assertNotWPError( $res );
    147146    }
     
    153152        // 0 redirections asked for, Should return the document?
    154153        $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) );
     154
     155        $this->skipTestOnTimeout( $res );
    155156        $this->assertNotWPError( $res );
    156157        $this->assertEquals( 302, (int)$res['response']['code'] );
     
    165166        // Prints PASS on initial load, FAIL if the client follows the specified redirection
    166167        $res = wp_remote_request( $this->redirection_script . '?201-location=true' );
     168
     169        $this->skipTestOnTimeout( $res );
    167170        $this->assertNotWPError( $res );
    168171        $this->assertEquals( 'PASS', $res['body']);
     
    179182        // Test 301 - POST to POST
    180183        $res = wp_remote_request( $url, array( 'method' => 'PUT', 'timeout' => 30 ) );
     184
     185        $this->skipTestOnTimeout( $res );
    181186        $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
    182187        $this->assertTrue( !empty( $res['headers']['location'] ) );
     
    191196        $res = wp_remote_request( $this->redirection_script . '?header-check', array('headers' => $headers) );
    192197
     198        $this->skipTestOnTimeout( $res );
    193199        $this->assertNotWPError( $res );
    194200
     
    221227
    222228        $this->skipTestOnTimeout( $res );
    223 
    224229        $this->assertNotWPError( $res );
    225230        $this->assertEquals( '', $res['body'] ); // The body should be empty.
     
    244249
    245250        $this->skipTestOnTimeout( $res );
    246 
    247251        $this->assertNotWPError( $res );
    248252        $this->assertEquals( $size, $filesize ); // Check that the file is written to disk correctly without any extra characters
     
    262266
    263267        $this->skipTestOnTimeout( $res );
    264 
    265268        $this->assertNotWPError( $res );
    266269        $this->assertEquals( $size, strlen( $res['body'] ) );
     
    270273     * Test POST redirection methods
    271274     *
     275     * @dataProvider data_post_redirect_to_method_300
     276     *
    272277     * @ticket 17588
    273278     */
    274     function test_post_redirect_to_method_300() {
     279    function test_post_redirect_to_method_300( $response_code, $method ) {
    275280        $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?post-redirect-to-method=1';
    276281
    277         // Test 300 - POST to POST
    278         $res = wp_remote_post( add_query_arg( 'response_code', 300, $url ), array( 'timeout' => 30 ) );
    279         $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) );
    280 
    281         // Test 301 - POST to POST
    282         $res = wp_remote_post( add_query_arg( 'response_code', 301, $url ), array( 'timeout' => 30 ) );
    283         $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) );
    284 
    285         // Test 302 - POST to GET
    286         $res = wp_remote_post( add_query_arg( 'response_code', 302, $url ), array( 'timeout' => 30 ) );
    287         $this->assertEquals( 'GET', wp_remote_retrieve_body( $res ) );
    288 
    289         // Test 303 - POST to GET
    290         $res = wp_remote_post( add_query_arg( 'response_code', 303, $url ), array( 'timeout' => 30 ) );
    291         $this->assertEquals( 'GET', wp_remote_retrieve_body( $res ) );
    292 
    293         // Test 304 - POST to POST
    294         $res = wp_remote_post( add_query_arg( 'response_code', 304, $url ), array( 'timeout' => 30 ) );
    295         $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) );
     282        $res = wp_remote_post( add_query_arg( 'response_code', $response_code, $url ), array( 'timeout' => 30 ) );
     283
     284        $this->skipTestOnTimeout( $res );
     285        $this->assertEquals( $method, wp_remote_retrieve_body( $res ) );
     286    }
     287
     288    public function data_post_redirect_to_method_300() {
     289        return array(
     290            // Test 300 - POST to POST
     291            array(
     292                300,
     293                'POST',
     294            ),
     295            // Test 301 - POST to POST
     296            array(
     297                301,
     298                'POST',
     299            ),
     300            // Test 302 - POST to GET
     301            array(
     302                302,
     303                'GET',
     304            ),
     305            // Test 303 - POST to GET
     306            array(
     307                303,
     308                'GET',
     309            ),
     310            // Test 304 - POST to POST
     311            array(
     312                304,
     313                'POST',
     314            ),
     315        );
    296316    }
    297317
     
    313333
    314334        $res = wp_remote_get( $url, $args );
     335
     336        $this->skipTestOnTimeout( $res );
    315337        $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
    316338
     
    334356        remove_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) );
    335357
     358        $this->skipTestOnTimeout( $res );
    336359        $this->assertNotEmpty( $this->http_request_args['sslcertificates'] );
    337360        $this->assertNotWPError( $res );
     
    347370        $res = wp_remote_head( $url, array( 'timeout' => 30 ) );
    348371
     372        $this->skipTestOnTimeout( $res );
    349373        $this->assertInternalType( 'array', wp_remote_retrieve_header( $res, 'location' ) );
    350374        $this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) );
    351375
    352376        $res = wp_remote_get( $url, array( 'timeout' => 30 ) );
     377
     378        $this->skipTestOnTimeout( $res );
    353379        $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
    354380
     
    364390
    365391        $res = wp_remote_get( $url );
     392
     393        $this->skipTestOnTimeout( $res );
    366394        $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
    367395    }
     
    378406
    379407        $res = wp_remote_get( 'https://wordpress.org/' );
    380         $this->assertTrue( ! is_wp_error( $res ), print_r( $res, true ) );
     408
     409        $this->skipTestOnTimeout( $res );
     410        $this->assertNotWPError( $res );
    381411    }
    382412
  • branches/4.4/tests/phpunit/tests/http/functions.php

    r35734 r50091  
    66 */
    77class Tests_HTTP_Functions extends WP_UnitTestCase {
     8
    89    public function setUp() {
    910        if ( ! extension_loaded( 'openssl' ) ) {
     
    1819        $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
    1920        $response = wp_remote_head( $url );
     21
     22        $this->skipTestOnTimeout( $response );
     23
    2024        $headers = wp_remote_retrieve_headers( $response );
    2125
     
    3034        $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    3135        $response = wp_remote_head( $url );
     36
     37        $this->skipTestOnTimeout( $response );
    3238        $this->assertEquals( '301', wp_remote_retrieve_response_code( $response ) );
    3339    }
     
    3541    function test_head_404() {
    3642        $url = 'https://asdftestblog1.files.wordpress.com/2007/09/awefasdfawef.jpg';
    37         $headers = wp_remote_head( $url );
     43        $response = wp_remote_head( $url );
    3844
    39         $this->assertInternalType( 'array', $headers, "Reply wasn't array." );
    40         $this->assertEquals( '404', wp_remote_retrieve_response_code( $headers ) );
     45        $this->skipTestOnTimeout( $response );
     46        $this->assertInternalType( 'array', $response, "Reply wasn't array." );
     47        $this->assertEquals( '404', wp_remote_retrieve_response_code( $response ) );
    4148    }
    4249
     
    4552
    4653        $response = wp_remote_get( $url );
     54
     55        $this->skipTestOnTimeout( $response );
     56
    4757        $headers = wp_remote_retrieve_headers( $response );
    4858
     
    5969
    6070        $response = wp_remote_get( $url );
     71
     72        $this->skipTestOnTimeout( $response );
     73
    6174        $headers = wp_remote_retrieve_headers( $response );
    6275
     
    7487        // pretend we've already redirected 5 times
    7588        $response = wp_remote_get( $url, array( 'redirection' => -1 ) );
     89
     90        $this->skipTestOnTimeout( $response );
    7691        $this->assertWPError( $response );
    7792    }
     
    8499
    85100        $response = wp_remote_head( $url );
    86         $cookies  = wp_remote_retrieve_cookies( $response );
     101
     102        $this->skipTestOnTimeout( $response );
     103
     104        $cookies = wp_remote_retrieve_cookies( $response );
    87105
    88106        $this->assertInternalType( 'array', $cookies );
Note: See TracChangeset for help on using the changeset viewer.