Make WordPress Core

Changeset 50107


Ignore:
Timestamp:
01/30/2021 09:28:58 PM (4 years 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], [50105] to the 3.8 branch.
See #51669.

Location:
branches/3.8
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/3.8

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

    r47491 r50107  
    5555    }
    5656
    57     /**
    58      * Unregister existing post types and register defaults.
    59      *
    60      * Run before each test in order to clean up the global scope, in case
    61      * a test forgets to unregister a post type on its own, or fails before
    62      * it has a chance to do so.
    63      */
    64     protected function reset_post_types() {
    65         foreach ( get_post_types() as $pt ) {
    66             _unregister_post_type( $pt );
    67         }
    68         create_initial_post_types();
    69     }
    70 
    71     /**
    72      * Unregister existing taxonomies and register defaults.
    73      *
    74      * Run before each test in order to clean up the global scope, in case
    75      * a test forgets to unregister a taxonomy on its own, or fails before
    76      * it has a chance to do so.
    77      */
    78     protected function reset_taxonomies() {
    79         foreach ( get_taxonomies() as $tax ) {
    80             _unregister_taxonomy( $tax );
    81         }
    82         create_initial_taxonomies();
    83     }
    84 
    8557    function tearDown() {
    8658        global $wpdb;
     
    12496            $this->markTestSkipped( 'For automated test runs, this test is only run on trunk/master' );
    12597        }
     98    }
     99
     100    /**
     101     * Allow tests to be skipped if the HTTP request times out.
     102     *
     103     * @param array|WP_Error $response HTTP response.
     104     */
     105    public function skipTestOnTimeout( $response ) {
     106        if ( ! is_wp_error( $response ) ) {
     107            return;
     108        }
     109        if ( 'connect() timed out!' === $response->get_error_message() ) {
     110            $this->markTestSkipped( 'HTTP timeout' );
     111        }
     112
     113        if ( false !== strpos( $response->get_error_message(), 'timed out after' ) ) {
     114            $this->markTestSkipped( 'HTTP timeout' );
     115        }
     116
     117        if ( 0 === strpos( $response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80' ) ) {
     118            $this->markTestSkipped( 'HTTP timeout' );
     119        }
     120
     121    }
     122
     123    /**
     124     * Unregister existing post types and register defaults.
     125     *
     126     * Run before each test in order to clean up the global scope, in case
     127     * a test forgets to unregister a post type on its own, or fails before
     128     * it has a chance to do so.
     129     */
     130    protected function reset_post_types() {
     131        foreach ( get_post_types() as $pt ) {
     132            _unregister_post_type( $pt );
     133        }
     134        create_initial_post_types();
     135    }
     136
     137    /**
     138     * Unregister existing taxonomies and register defaults.
     139     *
     140     * Run before each test in order to clean up the global scope, in case
     141     * a test forgets to unregister a taxonomy on its own, or fails before
     142     * it has a chance to do so.
     143     */
     144    protected function reset_taxonomies() {
     145        foreach ( get_taxonomies() as $tax ) {
     146            _unregister_taxonomy( $tax );
     147        }
     148        create_initial_taxonomies();
    126149    }
    127150
  • branches/3.8/tests/phpunit/tests/http/base.php

    r47338 r50107  
    4545        // 5 : 5 & 301
    4646        $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 5) );
     47
     48        $this->skipTestOnTimeout( $res );
    4749        $this->assertFalse( is_wp_error($res) );
    4850        $this->assertEquals(200, (int)$res['response']['code'] );
     
    5254        // 5 : 5 & 302
    5355        $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 5) );
     56
     57        $this->skipTestOnTimeout( $res );
    5458        $this->assertFalse( is_wp_error($res) );
    5559        $this->assertEquals(200, (int)$res['response']['code'] );
     
    6266        // 5 > 0 & 301
    6367        $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 0) );
     68
     69        $this->skipTestOnTimeout( $res );
    6470        $this->assertFalse( is_wp_error($res) );
    6571        $this->assertEquals(301, (int)$res['response']['code'] );
     
    7278        // 5 > 0 & 302
    7379        $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) );
     80
     81        $this->skipTestOnTimeout( $res );
    7482        $this->assertFalse( is_wp_error($res) );
    7583        $this->assertEquals(302, (int)$res['response']['code'] );
     
    7987        // 5 - 5
    8088        $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5) );
     89
     90        $this->skipTestOnTimeout( $res );
    8191        $this->assertFalse( is_wp_error($res) );
    8292        $this->assertEquals(200, (int)$res['response']['code'] );
     
    8696        // No redirections on HEAD request:
    8797        $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 1, array('method' => 'HEAD') );
     98
     99        $this->skipTestOnTimeout( $res );
    88100        $this->assertFalse( is_wp_error($res) );
    89101        $this->assertEquals( 302, (int)$res['response']['code'] );
     
    96108        // Redirections on HEAD request when Requested
    97109        $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5, 'method' => 'HEAD') );
     110
     111        $this->skipTestOnTimeout( $res );
    98112        $this->assertFalse( is_wp_error($res) );
    99113        $this->assertEquals( 200, (int)$res['response']['code'] );
     
    103117        // 10 > 5
    104118        $res = wp_remote_request($this->redirection_script . '?rt=' . 10, array('redirection' => 5) );
     119
     120        $this->skipTestOnTimeout( $res );
    105121        $this->assertTrue( is_wp_error($res), print_r($res, true) );
    106122    }
     
    109125        // 6 > 5 (close edgecase)
    110126        $res = wp_remote_request($this->redirection_script . '?rt=' . 6, array('redirection' => 5) );
     127
     128        $this->skipTestOnTimeout( $res );
    111129        $this->assertTrue( is_wp_error($res) );
    112130    }
     
    115133        // 4 < 5 (close edgecase)
    116134        $res = wp_remote_request($this->redirection_script . '?rt=' . 4, array('redirection' => 5) );
     135
     136        $this->skipTestOnTimeout( $res );
    117137        $this->assertFalse( is_wp_error($res) );
    118138    }
     
    124144        // 0 redirections asked for, Should return the document?
    125145        $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) );
     146
     147        $this->skipTestOnTimeout( $res );
    126148        $this->assertFalse( is_wp_error($res) );
    127149        $this->assertEquals( 302, (int)$res['response']['code'] );
     
    136158        // Prints PASS on initial load, FAIL if the client follows the specified redirection
    137159        $res = wp_remote_request( $this->redirection_script . '?201-location=true' );
     160
     161        $this->skipTestOnTimeout( $res );
    138162        $this->assertFalse( is_wp_error( $res ) );
    139163        $this->assertEquals( 'PASS', $res['body']);
     
    150174        // Test 301 - POST to POST
    151175        $res = wp_remote_request( $url, array( 'method' => 'PUT', 'timeout' => 30 ) );
     176
     177        $this->skipTestOnTimeout( $res );
    152178        $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
    153179        $this->assertTrue( !empty( $res['headers']['location'] ) );
     
    162188        $res = wp_remote_request( $this->redirection_script . '?header-check', array('headers' => $headers) );
    163189
     190        $this->skipTestOnTimeout( $res );
    164191        $this->assertFalse( is_wp_error($res) );
    165192
     
    185212        $res = wp_remote_request( $url, array( 'stream' => true, 'timeout' => 30 ) ); //Auto generate the filename.
    186213
     214        // Cleanup before we assert, as it'll return early.
     215        if ( ! is_wp_error( $res ) ) {
     216            $filesize = filesize( $res['filename'] );
     217            unlink( $res['filename'] );
     218        }
     219
     220        $this->skipTestOnTimeout( $res );
    187221        $this->assertFalse( is_wp_error( $res ) );
    188222        $this->assertEquals( '', $res['body'] ); // The body should be empty.
    189223        $this->assertEquals( $size, $res['headers']['content-length'] ); // Check the headers are returned (and the size is the same..)
    190         $this->assertEquals( $size, filesize($res['filename']) ); // Check that the file is written to disk correctly without any extra characters
    191 
    192         unlink($res['filename']); // Remove the temporary file
     224        $this->assertEquals( $size, $filesize ); // Check that the file is written to disk correctly without any extra characters
    193225    }
    194226
     
    196228     * Test POST redirection methods
    197229     *
     230     * @dataProvider data_post_redirect_to_method_300
     231     *
    198232     * @ticket 17588
    199233     */
    200     function test_post_redirect_to_method_300() {
     234    function test_post_redirect_to_method_300( $response_code, $method ) {
    201235        $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?post-redirect-to-method=1';
    202236
    203         // Test 300 - POST to POST
    204         $res = wp_remote_post( add_query_arg( 'response_code', 300, $url ), array( 'timeout' => 30 ) );
    205         $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) );
    206 
    207         // Test 301 - POST to POST
    208         $res = wp_remote_post( add_query_arg( 'response_code', 301, $url ), array( 'timeout' => 30 ) );
    209         $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) );
    210 
    211         // Test 302 - POST to GET
    212         $res = wp_remote_post( add_query_arg( 'response_code', 302, $url ), array( 'timeout' => 30 ) );
    213         $this->assertEquals( 'GET', wp_remote_retrieve_body( $res ) );
    214 
    215         // Test 303 - POST to GET
    216         $res = wp_remote_post( add_query_arg( 'response_code', 303, $url ), array( 'timeout' => 30 ) );
    217         $this->assertEquals( 'GET', wp_remote_retrieve_body( $res ) );
    218 
    219         // Test 304 - POST to POST
    220         $res = wp_remote_post( add_query_arg( 'response_code', 304, $url ), array( 'timeout' => 30 ) );
    221         $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) );
     237        $res = wp_remote_post( add_query_arg( 'response_code', $response_code, $url ), array( 'timeout' => 30 ) );
     238
     239        $this->skipTestOnTimeout( $res );
     240        $this->assertEquals( $method, wp_remote_retrieve_body( $res ) );
     241    }
     242
     243    public function data_post_redirect_to_method_300() {
     244        return array(
     245            // Test 300 - POST to POST
     246            array(
     247                300,
     248                'POST',
     249            ),
     250            // Test 301 - POST to POST
     251            array(
     252                301,
     253                'POST',
     254            ),
     255            // Test 302 - POST to GET
     256            array(
     257                302,
     258                'GET',
     259            ),
     260            // Test 303 - POST to GET
     261            array(
     262                303,
     263                'GET',
     264            ),
     265            // Test 304 - POST to POST
     266            array(
     267                304,
     268                'POST',
     269            ),
     270        );
    222271    }
    223272
     
    239288
    240289        $res = wp_remote_get( $url, $args );
     290
     291        $this->skipTestOnTimeout( $res );
    241292        $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
    242293
     
    252303        $res = wp_remote_head( $url, array( 'timeout' => 30 ) );
    253304
     305        $this->skipTestOnTimeout( $res );
    254306        $this->assertInternalType( 'array', wp_remote_retrieve_header( $res, 'location' ) );
    255307        $this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) );
    256308
    257309        $res = wp_remote_get( $url, array( 'timeout' => 30 ) );
     310
     311        $this->skipTestOnTimeout( $res );
    258312        $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
    259313
     
    269323
    270324        $res = wp_remote_get( $url );
     325
     326        $this->skipTestOnTimeout( $res );
    271327        $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
    272328    }
     
    283339
    284340        $res = wp_remote_get( 'https://wordpress.org/' );
     341
     342        $this->skipTestOnTimeout( $res );
    285343        $this->assertTrue( ! is_wp_error( $res ), print_r( $res, true ) );
    286344    }
  • branches/3.8/tests/phpunit/tests/http/functions.php

    r47338 r50107  
    1818        $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
    1919        $response = wp_remote_head( $url );
     20
     21        $this->skipTestOnTimeout( $response );
     22
    2023        $headers = wp_remote_retrieve_headers( $response );
    2124
     
    3033        $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    3134        $response = wp_remote_head( $url );
     35
     36        $this->skipTestOnTimeout( $response );
    3237        $this->assertEquals( '301', wp_remote_retrieve_response_code( $response ) );
    3338    }
     
    3540    function test_head_404() {
    3641        $url = 'https://asdftestblog1.files.wordpress.com/2007/09/awefasdfawef.jpg';
    37         $headers = wp_remote_head( $url );
     42        $response = wp_remote_head( $url );
    3843
    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 ) );
    4147    }
    4248
    4349    function test_get_request() {
    4450        $url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
    45         $file = tempnam('/tmp', 'testfile');
    4651
    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 );
    4857
    4958        // should return the same headers as a head request
     
    5160        $this->assertEquals( 'image/jpeg', $headers['content-type'] );
    5261        $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 ) );
    5863    }
    5964
     
    6166        // this will redirect to asdftestblog1.files.wordpress.com
    6267        $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    63         $file = tempnam('/tmp', 'testfile');
    6468
    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 );
    6674
    6775        // should return the same headers as a head request
     
    6977        $this->assertEquals( 'image/jpeg', $headers['content-type'] );
    7078        $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 ) );
    7680    }
    7781
     
    7983        // this will redirect to asdftestblog1.files.wordpress.com
    8084        $url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
    81         $file = tempnam('/tmp', 'testfile');
     85
    8286        // 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 ) );
    8591    }
    8692}
Note: See TracChangeset for help on using the changeset viewer.