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.

git-svn-id: https://develop.svn.wordpress.org/trunk@55029 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2023-01-05 10:21:19 +00:00
parent 53063c6333
commit d8f311800d
13 changed files with 60 additions and 60 deletions

View File

@ -251,7 +251,7 @@ class WP_Http {
*
* @since 2.9.0
*
* @param false|array|WP_Error $preempt A preemptive return value of an HTTP request. Default false.
* @param false|array|WP_Error $response A preemptive return value of an HTTP request. Default false.
* @param array $parsed_args HTTP request arguments.
* @param string $url The request URL.
*/

View File

@ -68,7 +68,7 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase {
remove_filter( 'pre_http_request', array( $this, '_fake_download_url_non_200_response_code' ) );
}
public function _fake_download_url_non_200_response_code( $response, $args, $url ) {
public function _fake_download_url_non_200_response_code( $response, $parsed_args, $url ) {
file_put_contents( $args['filename'], 'This is an unexpected error message from your favorite server.' );
return array(
'response' => array(
@ -150,7 +150,7 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase {
*
* @return array
*/
public function filter_content_disposition_header_with_filename( $response, $args, $url ) {
public function filter_content_disposition_header_with_filename( $response, $parsed_args, $url ) {
return array(
'response' => array(
'code' => 200,
@ -168,7 +168,7 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase {
*
* @return array
*/
public function filter_content_disposition_header_with_filename_with_path_traversal( $response, $args, $url ) {
public function filter_content_disposition_header_with_filename_with_path_traversal( $response, $parsed_args, $url ) {
return array(
'response' => array(
'code' => 200,
@ -186,7 +186,7 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase {
*
* @return array
*/
public function filter_content_disposition_header_with_filename_without_quotes( $response, $args, $url ) {
public function filter_content_disposition_header_with_filename_without_quotes( $response, $parsed_args, $url ) {
return array(
'response' => array(
'code' => 200,
@ -235,7 +235,7 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase {
*
* @return array
*/
public function filter_content_disposition_header_with_filename_without_context( $response, $args, $url ) {
public function filter_content_disposition_header_with_filename_without_context( $response, $parsed_args, $url ) {
return array(
'response' => array(
'code' => 200,
@ -253,7 +253,7 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase {
*
* @return array
*/
public function filter_content_disposition_header_with_filename_with_inline_context( $response, $args, $url ) {
public function filter_content_disposition_header_with_filename_with_inline_context( $response, $parsed_args, $url ) {
return array(
'response' => array(
'code' => 200,
@ -271,7 +271,7 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase {
*
* @return array
*/
public function filter_content_disposition_header_with_filename_with_form_data_context( $response, $args, $url ) {
public function filter_content_disposition_header_with_filename_with_form_data_context( $response, $parsed_args, $url ) {
return array(
'response' => array(
'code' => 200,
@ -365,12 +365,12 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase {
/**
* Mock the HTTP request response.
*
* @param bool $false False.
* @param array $arguments Request arguments.
* @param string $url Request URL.
* @return array|bool
* @param false|array|WP_Error $response A preemptive return value of an HTTP request. Default false.
* @param array $parsed_args HTTP request arguments.
* @param string $url The request URL.
* @return false|array|WP_Error Response data.
*/
public function mock_http_request( $false, $arguments, $url ) {
public function mock_http_request( $response, $parsed_args, $url ) {
if ( 'https://example.com' === $url ) {
return array(
'response' => array(
@ -379,6 +379,6 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase {
);
}
return $false;
return $response;
}
}

View File

@ -202,7 +202,7 @@ class Tests_Admin_wpSiteHealth extends WP_UnitTestCase {
add_filter(
'pre_http_request',
function ( $r, $parsed_args ) use ( &$responses, &$is_unauthorized, $good_basic_auth, $delay_the_response, $threshold ) {
function ( $response, $parsed_args ) use ( &$responses, &$is_unauthorized, $good_basic_auth, $delay_the_response, $threshold ) {
$expected_response = array_shift( $responses );

View File

@ -254,12 +254,12 @@ class Tests_Functions_DoEnclose extends WP_UnitTestCase {
*
* @since 5.3.0
*
* @param bool $false False.
* @param array $arguments Request arguments.
* @param string $url Request URL.
* @return array Header.
* @param false|array|WP_Error $response A preemptive return value of an HTTP request. Default false.
* @param array $parsed_args HTTP request arguments.
* @param string $url The request URL.
* @return array Response data.
*/
public function mock_http_request( $false, $arguments, $url ) {
public function mock_http_request( $response, $parsed_args, $url ) {
// Video and audio headers.
$fake_headers = array(

View File

@ -584,7 +584,7 @@ class Tests_HTTP_HTTP extends WP_UnitTestCase {
// Filter the response made by WP_HTTP::handle_redirects().
add_filter(
'pre_http_request',
function( $response, $args, $url ) use ( &$pre_http_request_filter_has_run ) {
function( $response, $parsed_args, $url ) use ( &$pre_http_request_filter_has_run ) {
$pre_http_request_filter_has_run = true;
// Assert the redirect URL is correct.

View File

@ -44,16 +44,16 @@ class Tests_HTTP_wpGetHttpHeaders extends WP_UnitTestCase {
/**
* Mock the HTTP request response
*
* @param bool $false False.
* @param array $arguments Request arguments.
* @param string $url Request URL.
* @return array|bool
* @param false|array|WP_Error $response A preemptive return value of an HTTP request. Default false.
* @param array $parsed_args HTTP request arguments.
* @param string $url The request URL.
* @return false|array|WP_Error Response data.
*/
public function mock_http_request( $false, $arguments, $url ) {
public function mock_http_request( $response, $parsed_args, $url ) {
if ( 'http://example.com' === $url ) {
return array( 'headers' => true );
}
return false;
return $response;
}
}

View File

@ -264,37 +264,37 @@ class Tests_HTTPS_Detection extends WP_UnitTestCase {
$this->assertNull( wp_is_local_html_output( $html ) );
}
public function record_request_url( $preempt, $parsed_args, $url ) {
public function record_request_url( $response, $parsed_args, $url ) {
$this->last_request_url = $url;
return $preempt;
return $response;
}
public function mock_success_with_sslverify( $preempt, $parsed_args ) {
public function mock_success_with_sslverify( $response, $parsed_args ) {
if ( ! empty( $parsed_args['sslverify'] ) ) {
return $this->mock_success();
}
return $preempt;
return $response;
}
public function mock_error_with_sslverify( $preempt, $parsed_args ) {
public function mock_error_with_sslverify( $response, $parsed_args ) {
if ( ! empty( $parsed_args['sslverify'] ) ) {
return $this->mock_error();
}
return $preempt;
return $response;
}
public function mock_success_without_sslverify( $preempt, $parsed_args ) {
public function mock_success_without_sslverify( $response, $parsed_args ) {
if ( empty( $parsed_args['sslverify'] ) ) {
return $this->mock_success();
}
return $preempt;
return $response;
}
public function mock_error_without_sslverify( $preempt, $parsed_args ) {
public function mock_error_without_sslverify( $response, $parsed_args ) {
if ( empty( $parsed_args['sslverify'] ) ) {
return $this->mock_error();
}
return $preempt;
return $response;
}
public function mock_not_found() {

View File

@ -84,13 +84,13 @@ class Test_oEmbed_Controller extends WP_UnitTestCase {
/**
* Intercept oEmbed requests and mock responses.
*
* @param mixed $preempt Whether to preempt an HTTP request's return value. Default false.
* @param mixed $r HTTP request arguments.
* @param string $url The request URL.
* @param false|array|WP_Error $response A preemptive return value of an HTTP request. Default false.
* @param array $parsed_args HTTP request arguments.
* @param string $url The request URL.
* @return array Response data.
*/
public function mock_embed_request( $preempt, $r, $url ) {
unset( $preempt, $r );
public function mock_embed_request( $response, $parsed_args, $url ) {
unset( $response, $parsed_args );
$parsed_url = wp_parse_url( $url );
$query = isset( $parsed_url['query'] ) ? $parsed_url['query'] : '';

View File

@ -274,13 +274,13 @@ class WP_REST_Block_Directory_Controller_Test extends WP_Test_REST_Controller_Te
private function prevent_requests_to_host( $blocked_host = 'api.wordpress.org' ) {
add_filter(
'pre_http_request',
static function ( $return, $args, $url ) use ( $blocked_host ) {
static function ( $response, $parsed_args, $url ) use ( $blocked_host ) {
if ( @parse_url( $url, PHP_URL_HOST ) === $blocked_host ) {
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" );
}
return $return;
return $response;
},
10,
3

View File

@ -522,10 +522,10 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
private static function mock_successful_response( $action, $expects_results ) {
add_filter(
'pre_http_request',
static function ( $preempt, $args, $url ) use ( $action, $expects_results ) {
static function ( $response, $parsed_args, $url ) use ( $action, $expects_results ) {
if ( 'api.wordpress.org' !== wp_parse_url( $url, PHP_URL_HOST ) ) {
return $preempt;
return $response;
}
$response = array(
@ -556,7 +556,7 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
private static function prevent_requests_to_host( $blocked_host = 'api.wordpress.org' ) {
add_filter(
'pre_http_request',
static function ( $return, $args, $url ) use ( $blocked_host ) {
static function ( $response, $parsed_args, $url ) use ( $blocked_host ) {
if ( wp_parse_url( $url, PHP_URL_HOST ) === $blocked_host ) {
return new WP_Error(
@ -567,7 +567,7 @@ class WP_REST_Pattern_Directory_Controller_Test extends WP_Test_REST_Controller_
}
return $return;
return $response;
},
10,
3

View File

@ -1139,13 +1139,13 @@ PHP;
private function prevent_requests_to_host( $blocked_host = 'api.wordpress.org' ) {
add_filter(
'pre_http_request',
static function ( $return, $args, $url ) use ( $blocked_host ) {
static function ( $response, $parsed_args, $url ) use ( $blocked_host ) {
if ( @parse_url( $url, PHP_URL_HOST ) === $blocked_host ) {
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" );
}
return $return;
return $response;
},
10,
3

View File

@ -34,8 +34,8 @@ class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase {
parent::tear_down();
}
public function mock_embed_request( $preempt, $r, $url ) {
unset( $preempt, $r );
public function mock_embed_request( $response, $parsed_args, $url ) {
unset( $response, $parsed_args );
// Mock request to YouTube Embed.
if ( false !== strpos( $url, self::YOUTUBE_VIDEO_ID ) ) {

View File

@ -1096,20 +1096,20 @@ class Tests_REST_WpRestUrlDetailsController extends WP_Test_REST_Controller_Test
*
* @return array faux/mocked response.
*/
public function mock_success_request_to_remote_url( $response, $args ) {
return $this->mock_request_to_remote_url( 'success', $args );
public function mock_success_request_to_remote_url( $response, $parsed_args ) {
return $this->mock_request_to_remote_url( 'success', $parsed_args );
}
public function mock_failed_request_to_remote_url( $response, $args ) {
return $this->mock_request_to_remote_url( 'failure', $args );
public function mock_failed_request_to_remote_url( $response, $parsed_args ) {
return $this->mock_request_to_remote_url( 'failure', $parsed_args );
}
public function mock_request_to_remote_url_with_empty_body_response( $response, $args ) {
return $this->mock_request_to_remote_url( 'empty_body', $args );
public function mock_request_to_remote_url_with_empty_body_response( $response, $parsed_args ) {
return $this->mock_request_to_remote_url( 'empty_body', $parsed_args );
}
private function mock_request_to_remote_url( $result_type, $args ) {
$this->request_args = $args;
private function mock_request_to_remote_url( $result_type, $parsed_args ) {
$this->request_args = $parsed_args;
$types = array(
'success',