Tests: Mock the HTTP request response in download_url() tests.

This aims to speed up the tests and minimize unrelated failures by avoiding an unnecessary external HTTP request, while still performing the intended functionality checks.

Update similar helpers in some other tests to use more consistent terminology.

Follow-up to [37907], [46175], [51626].

See #54420, #53363.

git-svn-id: https://develop.svn.wordpress.org/trunk@52382 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2021-12-15 19:59:32 +00:00
parent 2cd907dd63
commit 5cb75a136d
3 changed files with 32 additions and 8 deletions

View File

@@ -291,6 +291,9 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase {
* @covers ::download_url
*/
public function test_download_url_no_warning_for_url_without_path() {
// Hook a mocked HTTP request response.
add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
$result = download_url( 'https://example.com' );
$this->assertIsString( $result );
@@ -306,6 +309,9 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase {
* @covers ::download_url
*/
public function test_download_url_no_warning_for_url_without_path_with_signature_verification() {
// Hook a mocked HTTP request response.
add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
add_filter(
'wp_signature_hosts',
static function( $urls ) {
@@ -326,4 +332,24 @@ class Tests_Admin_IncludesFile extends WP_UnitTestCase {
$this->assertWPError( $error );
$this->assertSame( 'signature_verification_no_signature', $error->get_error_code() );
}
/**
* Mock the HTTP request response.
*
* @param bool $false False.
* @param array $arguments Request arguments.
* @param string $url Request URL.
* @return array|bool
*/
public function mock_http_request( $false, $arguments, $url ) {
if ( 'https://example.com' === $url ) {
return array(
'response' => array(
'code' => 200,
),
);
}
return $false;
}
}

View File

@@ -25,7 +25,7 @@ class Tests_Functions_DoEnclose extends WP_UnitTestCase {
*/
public function set_up() {
parent::set_up();
add_filter( 'pre_http_request', array( $this, 'fake_http_request' ), 10, 3 );
add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
}
/**
@@ -250,17 +250,16 @@ class Tests_Functions_DoEnclose extends WP_UnitTestCase {
}
/**
* Fake the HTTP request response.
* Mock the HTTP request response.
*
* @since 5.3.0
*
* @param bool $false False.
* @param array $arguments Request arguments.
* @param string $url Request URL.
*
* @return array Header.
*/
public function fake_http_request( $false, $arguments, $url ) {
public function mock_http_request( $false, $arguments, $url ) {
// Video and audio headers.
$fake_headers = array(

View File

@@ -12,8 +12,8 @@ class Tests_HTTP_wpGetHttpHeaders extends WP_UnitTestCase {
public function set_up() {
parent::set_up();
// Hook a fake HTTP request response.
add_filter( 'pre_http_request', array( $this, 'fake_http_request' ), 10, 3 );
// Hook a mocked HTTP request response.
add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
}
/**
@@ -47,10 +47,9 @@ class Tests_HTTP_wpGetHttpHeaders extends WP_UnitTestCase {
* @param bool $false False.
* @param array $arguments Request arguments.
* @param string $url Request URL.
*
* @return array|bool
*/
public function fake_http_request( $false, $arguments, $url ) {
public function mock_http_request( $false, $arguments, $url ) {
if ( 'http://example.com' === $url ) {
return array( 'headers' => true );
}