Make WordPress Core

Changeset 55029


Ignore:
Timestamp:
01/05/2023 10:21:19 AM (17 months ago)
Author:
SergeyBiryukov
Message:

Tests: Bring some consistency to mocking HTTP requests in unit tests.

Includes:

  • Renaming the $preempt parameter to $response in the pre_http_request filter to better match the context used in callbacks (returning the original value if the conditions are not met rather than preempting the request).
  • Synchronizing parameter names and types in various pre_http_request callbacks in unit tests.

Follow-up to [34509], [37907], [40628], [40629], [45667], [46175], [48242], [48462], [49904], [51021], [51973], [52146], [52382], [54043], [54968].

See #56793, #56792.

Location:
trunk
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-http.php

    r54997 r55029  
    252252         * @since 2.9.0
    253253         *
    254          * @param false|array|WP_Error $preempt     A preemptive return value of an HTTP request. Default false.
     254         * @param false|array|WP_Error $response    A preemptive return value of an HTTP request. Default false.
    255255         * @param array                $parsed_args HTTP request arguments.
    256256         * @param string               $url         The request URL.
  • trunk/tests/phpunit/tests/admin/includesFile.php

    r52760 r55029  
    6969    }
    7070
    71     public function _fake_download_url_non_200_response_code( $response, $args, $url ) {
     71    public function _fake_download_url_non_200_response_code( $response, $parsed_args, $url ) {
    7272        file_put_contents( $args['filename'], 'This is an unexpected error message from your favorite server.' );
    7373        return array(
     
    151151     * @return array
    152152     */
    153     public function filter_content_disposition_header_with_filename( $response, $args, $url ) {
     153    public function filter_content_disposition_header_with_filename( $response, $parsed_args, $url ) {
    154154        return array(
    155155            'response' => array(
     
    169169     * @return array
    170170     */
    171     public function filter_content_disposition_header_with_filename_with_path_traversal( $response, $args, $url ) {
     171    public function filter_content_disposition_header_with_filename_with_path_traversal( $response, $parsed_args, $url ) {
    172172        return array(
    173173            'response' => array(
     
    187187     * @return array
    188188     */
    189     public function filter_content_disposition_header_with_filename_without_quotes( $response, $args, $url ) {
     189    public function filter_content_disposition_header_with_filename_without_quotes( $response, $parsed_args, $url ) {
    190190        return array(
    191191            'response' => array(
     
    236236     * @return array
    237237     */
    238     public function filter_content_disposition_header_with_filename_without_context( $response, $args, $url ) {
     238    public function filter_content_disposition_header_with_filename_without_context( $response, $parsed_args, $url ) {
    239239        return array(
    240240            'response' => array(
     
    254254     * @return array
    255255     */
    256     public function filter_content_disposition_header_with_filename_with_inline_context( $response, $args, $url ) {
     256    public function filter_content_disposition_header_with_filename_with_inline_context( $response, $parsed_args, $url ) {
    257257        return array(
    258258            'response' => array(
     
    272272     * @return array
    273273     */
    274     public function filter_content_disposition_header_with_filename_with_form_data_context( $response, $args, $url ) {
     274    public function filter_content_disposition_header_with_filename_with_form_data_context( $response, $parsed_args, $url ) {
    275275        return array(
    276276            'response' => array(
     
    366366     * Mock the HTTP request response.
    367367     *
    368      * @param bool   $false     False.
    369      * @param array  $arguments Request arguments.
    370      * @param string $url       Request URL.
    371      * @return array|bool
    372      */
    373     public function mock_http_request( $false, $arguments, $url ) {
     368     * @param false|array|WP_Error $response    A preemptive return value of an HTTP request. Default false.
     369     * @param array                $parsed_args HTTP request arguments.
     370     * @param string               $url         The request URL.
     371     * @return false|array|WP_Error Response data.
     372     */
     373    public function mock_http_request( $response, $parsed_args, $url ) {
    374374        if ( 'https://example.com' === $url ) {
    375375            return array(
     
    380380        }
    381381
    382         return $false;
     382        return $response;
    383383    }
    384384}
  • trunk/tests/phpunit/tests/admin/wpSiteHealth.php

    r54076 r55029  
    203203        add_filter(
    204204            'pre_http_request',
    205             function ( $r, $parsed_args ) use ( &$responses, &$is_unauthorized, $good_basic_auth, $delay_the_response, $threshold ) {
     205            function ( $response, $parsed_args ) use ( &$responses, &$is_unauthorized, $good_basic_auth, $delay_the_response, $threshold ) {
    206206
    207207                $expected_response = array_shift( $responses );
  • trunk/tests/phpunit/tests/functions/doEnclose.php

    r52780 r55029  
    255255     * @since 5.3.0
    256256     *
    257      * @param bool   $false     False.
    258      * @param array  $arguments Request arguments.
    259      * @param string $url       Request URL.
    260      * @return array            Header.
    261      */
    262     public function mock_http_request( $false, $arguments, $url ) {
     257     * @param false|array|WP_Error $response    A preemptive return value of an HTTP request. Default false.
     258     * @param array                $parsed_args HTTP request arguments.
     259     * @param string               $url         The request URL.
     260     * @return array Response data.
     261     */
     262    public function mock_http_request( $response, $parsed_args, $url ) {
    263263
    264264        // Video and audio headers.
  • trunk/tests/phpunit/tests/http/http.php

    r54997 r55029  
    585585        add_filter(
    586586            'pre_http_request',
    587             function( $response, $args, $url ) use ( &$pre_http_request_filter_has_run ) {
     587            function( $response, $parsed_args, $url ) use ( &$pre_http_request_filter_has_run ) {
    588588                $pre_http_request_filter_has_run = true;
    589589
  • trunk/tests/phpunit/tests/http/wpGetHttpHeaders.php

    r52382 r55029  
    4545     * Mock the HTTP request response
    4646     *
    47      * @param bool   $false     False.
    48      * @param array  $arguments Request arguments.
    49      * @param string $url       Request URL.
    50      * @return array|bool
     47     * @param false|array|WP_Error $response    A preemptive return value of an HTTP request. Default false.
     48     * @param array                $parsed_args HTTP request arguments.
     49     * @param string               $url         The request URL.
     50     * @return false|array|WP_Error Response data.
    5151     */
    52     public function mock_http_request( $false, $arguments, $url ) {
     52    public function mock_http_request( $response, $parsed_args, $url ) {
    5353        if ( 'http://example.com' === $url ) {
    5454            return array( 'headers' => true );
    5555        }
    5656
    57         return false;
     57        return $response;
    5858    }
    5959}
  • trunk/tests/phpunit/tests/https-detection.php

    r51657 r55029  
    265265    }
    266266
    267     public function record_request_url( $preempt, $parsed_args, $url ) {
     267    public function record_request_url( $response, $parsed_args, $url ) {
    268268        $this->last_request_url = $url;
    269         return $preempt;
    270     }
    271 
    272     public function mock_success_with_sslverify( $preempt, $parsed_args ) {
     269        return $response;
     270    }
     271
     272    public function mock_success_with_sslverify( $response, $parsed_args ) {
    273273        if ( ! empty( $parsed_args['sslverify'] ) ) {
    274274            return $this->mock_success();
    275275        }
    276         return $preempt;
    277     }
    278 
    279     public function mock_error_with_sslverify( $preempt, $parsed_args ) {
     276        return $response;
     277    }
     278
     279    public function mock_error_with_sslverify( $response, $parsed_args ) {
    280280        if ( ! empty( $parsed_args['sslverify'] ) ) {
    281281            return $this->mock_error();
    282282        }
    283         return $preempt;
    284     }
    285 
    286     public function mock_success_without_sslverify( $preempt, $parsed_args ) {
     283        return $response;
     284    }
     285
     286    public function mock_success_without_sslverify( $response, $parsed_args ) {
    287287        if ( empty( $parsed_args['sslverify'] ) ) {
    288288            return $this->mock_success();
    289289        }
    290         return $preempt;
    291     }
    292 
    293     public function mock_error_without_sslverify( $preempt, $parsed_args ) {
     290        return $response;
     291    }
     292
     293    public function mock_error_without_sslverify( $response, $parsed_args ) {
    294294        if ( empty( $parsed_args['sslverify'] ) ) {
    295295            return $this->mock_error();
    296296        }
    297         return $preempt;
     297        return $response;
    298298    }
    299299
  • trunk/tests/phpunit/tests/oembed/controller.php

    r54891 r55029  
    8585     * Intercept oEmbed requests and mock responses.
    8686     *
    87      * @param mixed  $preempt Whether to preempt an HTTP request's return value. Default false.
    88      * @param mixed  $r      HTTP request arguments.
    89      * @param string $url     The request URL.
     87     * @param false|array|WP_Error $response    A preemptive return value of an HTTP request. Default false.
     88     * @param array                $parsed_args HTTP request arguments.
     89     * @param string               $url         The request URL.
    9090     * @return array Response data.
    9191     */
    92     public function mock_embed_request( $preempt, $r, $url ) {
    93         unset( $preempt, $r );
     92    public function mock_embed_request( $response, $parsed_args, $url ) {
     93        unset( $response, $parsed_args );
    9494
    9595        $parsed_url = wp_parse_url( $url );
  • trunk/tests/phpunit/tests/rest-api/rest-block-directory-controller.php

    r54058 r55029  
    275275        add_filter(
    276276            'pre_http_request',
    277             static function ( $return, $args, $url ) use ( $blocked_host ) {
     277            static function ( $response, $parsed_args, $url ) use ( $blocked_host ) {
    278278                if ( @parse_url( $url, PHP_URL_HOST ) === $blocked_host ) {
    279279                    return new WP_Error( 'plugins_api_failed', "An expected error occurred connecting to $blocked_host because of a unit test", "cURL error 7: Failed to connect to $blocked_host port 80: Connection refused" );
     
    281281                }
    282282
    283                 return $return;
     283                return $response;
    284284            },
    285285            10,
  • trunk/tests/phpunit/tests/rest-api/rest-pattern-directory-controller.php

    r54058 r55029  
    523523        add_filter(
    524524            'pre_http_request',
    525             static function ( $preempt, $args, $url ) use ( $action, $expects_results ) {
     525            static function ( $response, $parsed_args, $url ) use ( $action, $expects_results ) {
    526526
    527527                if ( 'api.wordpress.org' !== wp_parse_url( $url, PHP_URL_HOST ) ) {
    528                     return $preempt;
     528                    return $response;
    529529                }
    530530
     
    557557        add_filter(
    558558            'pre_http_request',
    559             static function ( $return, $args, $url ) use ( $blocked_host ) {
     559            static function ( $response, $parsed_args, $url ) use ( $blocked_host ) {
    560560
    561561                if ( wp_parse_url( $url, PHP_URL_HOST ) === $blocked_host ) {
     
    568568                }
    569569
    570                 return $return;
     570                return $response;
    571571            },
    572572            10,
  • trunk/tests/phpunit/tests/rest-api/rest-plugins-controller.php

    r54304 r55029  
    11401140        add_filter(
    11411141            'pre_http_request',
    1142             static function ( $return, $args, $url ) use ( $blocked_host ) {
     1142            static function ( $response, $parsed_args, $url ) use ( $blocked_host ) {
    11431143                if ( @parse_url( $url, PHP_URL_HOST ) === $blocked_host ) {
    11441144                    return new WP_Error( 'plugins_api_failed', "An expected error occurred connecting to $blocked_host because of a unit test", "cURL error 7: Failed to connect to $blocked_host port 80: Connection refused" );
     
    11461146                }
    11471147
    1148                 return $return;
     1148                return $response;
    11491149            },
    11501150            10,
  • trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php

    r54891 r55029  
    3535    }
    3636
    37     public function mock_embed_request( $preempt, $r, $url ) {
    38         unset( $preempt, $r );
     37    public function mock_embed_request( $response, $parsed_args, $url ) {
     38        unset( $response, $parsed_args );
    3939
    4040        // Mock request to YouTube Embed.
  • trunk/tests/phpunit/tests/rest-api/wpRestUrlDetailsController.php

    r54059 r55029  
    10971097     * @return array faux/mocked response.
    10981098     */
    1099     public function mock_success_request_to_remote_url( $response, $args ) {
    1100         return $this->mock_request_to_remote_url( 'success', $args );
    1101     }
    1102 
    1103     public function mock_failed_request_to_remote_url( $response, $args ) {
    1104         return $this->mock_request_to_remote_url( 'failure', $args );
    1105     }
    1106 
    1107     public function mock_request_to_remote_url_with_empty_body_response( $response, $args ) {
    1108         return $this->mock_request_to_remote_url( 'empty_body', $args );
    1109     }
    1110 
    1111     private function mock_request_to_remote_url( $result_type, $args ) {
    1112         $this->request_args = $args;
     1099    public function mock_success_request_to_remote_url( $response, $parsed_args ) {
     1100        return $this->mock_request_to_remote_url( 'success', $parsed_args );
     1101    }
     1102
     1103    public function mock_failed_request_to_remote_url( $response, $parsed_args ) {
     1104        return $this->mock_request_to_remote_url( 'failure', $parsed_args );
     1105    }
     1106
     1107    public function mock_request_to_remote_url_with_empty_body_response( $response, $parsed_args ) {
     1108        return $this->mock_request_to_remote_url( 'empty_body', $parsed_args );
     1109    }
     1110
     1111    private function mock_request_to_remote_url( $result_type, $parsed_args ) {
     1112        $this->request_args = $parsed_args;
    11131113
    11141114        $types = array(
Note: See TracChangeset for help on using the changeset viewer.