wordpress-develop/tests/phpunit/tests/http/wpGetHttpHeaders.php
Sergey Biryukov 5cb75a136d 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
2021-12-15 19:59:32 +00:00

60 lines
1.3 KiB
PHP

<?php
/**
* @group http
* @covers ::wp_get_http_headers
*/
class Tests_HTTP_wpGetHttpHeaders extends WP_UnitTestCase {
/**
* Set up the environment
*/
public function set_up() {
parent::set_up();
// Hook a mocked HTTP request response.
add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
}
/**
* Test with a valid URL
*/
public function test_wp_get_http_headers_valid_url() {
$result = wp_get_http_headers( 'http://example.com' );
$this->assertTrue( $result );
}
/**
* Test with an invalid URL
*/
public function test_wp_get_http_headers_invalid_url() {
$result = wp_get_http_headers( 'not_an_url' );
$this->assertFalse( $result );
}
/**
* Test to see if the deprecated argument is working
*/
public function test_wp_get_http_headers_deprecated_argument() {
$this->setExpectedDeprecated( 'wp_get_http_headers' );
wp_get_http_headers( 'does_not_matter', $deprecated = true );
}
/**
* 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 ( 'http://example.com' === $url ) {
return array( 'headers' => true );
}
return false;
}
}