wordpress-develop/tests/phpunit/tests/http/wpGetHttpHeaders.php
Sergey Biryukov d8f311800d 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
2023-01-05 10:21:19 +00:00

60 lines
1.4 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 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( $response, $parsed_args, $url ) {
if ( 'http://example.com' === $url ) {
return array( 'headers' => true );
}
return $response;
}
}