From e9c4dd6a562c8a645a6b81169a15f6cf7f98e1e7 Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 07:51:34 +0600 Subject: [PATCH] Tests: Add unit tests for the HTTP response and request host helper functions. Covers `wp_remote_retrieve_header()`, `wp_remote_retrieve_response_message()`, `allowed_http_request_hosts()` and `ms_allowed_http_request_hosts()`, which had no tests. See #66501. --- .../tests/http/allowedHttpRequestHosts.php | 67 +++++++++ .../tests/http/msAllowedHttpRequestHosts.php | 98 +++++++++++++ .../tests/http/wpRemoteRetrieveHeader.php | 131 ++++++++++++++++++ .../http/wpRemoteRetrieveResponseMessage.php | 106 ++++++++++++++ 4 files changed, 402 insertions(+) create mode 100644 tests/phpunit/tests/http/allowedHttpRequestHosts.php create mode 100644 tests/phpunit/tests/http/msAllowedHttpRequestHosts.php create mode 100644 tests/phpunit/tests/http/wpRemoteRetrieveHeader.php create mode 100644 tests/phpunit/tests/http/wpRemoteRetrieveResponseMessage.php diff --git a/tests/phpunit/tests/http/allowedHttpRequestHosts.php b/tests/phpunit/tests/http/allowedHttpRequestHosts.php new file mode 100644 index 0000000000000..832f739a26293 --- /dev/null +++ b/tests/phpunit/tests/http/allowedHttpRequestHosts.php @@ -0,0 +1,67 @@ +assertTrue( allowed_http_request_hosts( true, 'unknown.example.com' ) ); + $this->assertTrue( allowed_http_request_hosts( true, WP_TESTS_DOMAIN ) ); + } + + /** + * Tests that the host of the site is allowed. + * + * @ticket 66501 + */ + public function test_should_allow_the_host_of_the_site() { + $this->assertTrue( allowed_http_request_hosts( false, WP_TESTS_DOMAIN ) ); + } + + /** + * Tests that a host that the site does not allow is not made external. + * + * @ticket 66501 + */ + public function test_should_not_allow_other_hosts() { + $this->assertFalse( allowed_http_request_hosts( false, 'unknown.example.com' ) ); + } + + /** + * Tests that the hosts that are added with the `allowed_redirect_hosts` filter are allowed. + * + * @ticket 66501 + */ + public function test_should_allow_the_hosts_of_the_allowed_redirect_hosts_filter() { + add_filter( + 'allowed_redirect_hosts', + static function ( $hosts ) { + $hosts[] = 'allowed-by-filter.example.com'; + + return $hosts; + } + ); + + $this->assertTrue( allowed_http_request_hosts( false, 'allowed-by-filter.example.com' ), 'The host of the filter should be allowed.' ); + $this->assertFalse( allowed_http_request_hosts( false, 'not-in-the-filter.example.com' ), 'A host that is not in the filter should not be allowed.' ); + } + + /** + * Tests that the function is used by default for the requests of the HTTP API. + * + * @ticket 66501 + */ + public function test_should_be_hooked_to_the_filter_of_external_hosts() { + $this->assertSame( 10, has_filter( 'http_request_host_is_external', 'allowed_http_request_hosts' ) ); + } +} diff --git a/tests/phpunit/tests/http/msAllowedHttpRequestHosts.php b/tests/phpunit/tests/http/msAllowedHttpRequestHosts.php new file mode 100644 index 0000000000000..ba9d018aa9dc0 --- /dev/null +++ b/tests/phpunit/tests/http/msAllowedHttpRequestHosts.php @@ -0,0 +1,98 @@ +num_queries; + + $this->assertTrue( ms_allowed_http_request_hosts( true, 'ms-external.example.com' ) ); + $this->assertSame( $queries, $wpdb->num_queries, 'The database should not be queried.' ); + } + + /** + * Tests that the domain of the network is allowed, without a query. + * + * @ticket 66501 + */ + public function test_should_allow_the_domain_of_the_network_without_a_query() { + global $wpdb; + + $queries = $wpdb->num_queries; + + $this->assertTrue( ms_allowed_http_request_hosts( false, get_network()->domain ) ); + $this->assertSame( $queries, $wpdb->num_queries, 'The database should not be queried.' ); + } + + /** + * Tests that the domain of a site of the network is allowed. + * + * @ticket 66501 + */ + public function test_should_allow_the_domain_of_a_site_of_the_network() { + self::factory()->blog->create( + array( + 'domain' => 'ms-allowed-site.example.net', + 'path' => '/', + ) + ); + + $this->assertTrue( ms_allowed_http_request_hosts( false, 'ms-allowed-site.example.net' ) ); + } + + /** + * Tests that a domain that is not one of the network or of its sites is not allowed. + * + * @ticket 66501 + */ + public function test_should_not_allow_other_hosts() { + $this->assertFalse( ms_allowed_http_request_hosts( false, 'ms-unknown-host.example.net' ) ); + } + + /** + * Tests that the database is queried only once for the same host. + * + * @ticket 66501 + */ + public function test_should_query_the_database_once_for_the_same_host() { + $queries = 0; + add_filter( + 'query', + static function ( $query ) use ( &$queries ) { + if ( str_contains( $query, 'ms-cached-host.example.net' ) ) { + ++$queries; + } + + return $query; + } + ); + + $this->assertFalse( ms_allowed_http_request_hosts( false, 'ms-cached-host.example.net' ), 'The first call should not allow the host.' ); + $this->assertFalse( ms_allowed_http_request_hosts( false, 'ms-cached-host.example.net' ), 'The second call should not allow the host.' ); + + $this->assertSame( 1, $queries, 'The host should be queried once.' ); + } + + /** + * Tests that the function is used by default for the requests of the HTTP API in a network. + * + * @ticket 66501 + */ + public function test_should_be_hooked_to_the_filter_of_external_hosts() { + $this->assertSame( 20, has_filter( 'http_request_host_is_external', 'ms_allowed_http_request_hosts' ) ); + } +} diff --git a/tests/phpunit/tests/http/wpRemoteRetrieveHeader.php b/tests/phpunit/tests/http/wpRemoteRetrieveHeader.php new file mode 100644 index 0000000000000..923b450c06d14 --- /dev/null +++ b/tests/phpunit/tests/http/wpRemoteRetrieveHeader.php @@ -0,0 +1,131 @@ + array( + 'content-type' => 'text/html', + 'x-custom' => 'custom value', + ), + ); + + $this->assertSame( 'text/html', wp_remote_retrieve_header( $response, 'content-type' ) ); + $this->assertSame( 'custom value', wp_remote_retrieve_header( $response, 'x-custom' ) ); + } + + /** + * Tests that the values of the headers that are repeated are returned as they are. + * + * @ticket 66501 + */ + public function test_should_return_the_value_of_a_header_that_is_repeated() { + $response = array( + 'headers' => array( + 'set-cookie' => array( 'a=1', 'b=2' ), + ), + ); + + $this->assertSame( array( 'a=1', 'b=2' ), wp_remote_retrieve_header( $response, 'set-cookie' ) ); + } + + /** + * Tests that the headers of the HTTP API are looked up without taking the case into account. + * + * @ticket 66501 + */ + public function test_should_find_the_header_of_a_case_insensitive_dictionary_in_any_case() { + $response = array( + 'headers' => new WpOrg\Requests\Utility\CaseInsensitiveDictionary( array( 'Content-Type' => 'application/json' ) ), + ); + + $this->assertSame( 'application/json', wp_remote_retrieve_header( $response, 'content-type' ), 'The lowercase name should be found.' ); + $this->assertSame( 'application/json', wp_remote_retrieve_header( $response, 'Content-Type' ), 'The original name should be found.' ); + $this->assertSame( 'application/json', wp_remote_retrieve_header( $response, 'CONTENT-TYPE' ), 'The uppercase name should be found.' ); + } + + /** + * Tests that the name of a header of an array is case sensitive. + * + * @ticket 66501 + */ + public function test_should_not_ignore_the_case_of_the_header_of_an_array() { + $response = array( + 'headers' => array( 'content-type' => 'text/html' ), + ); + + $this->assertSame( '', wp_remote_retrieve_header( $response, 'Content-Type' ) ); + } + + /** + * Tests that an empty string is returned when the header is not set. + * + * @ticket 66501 + */ + public function test_should_return_an_empty_string_when_the_header_is_not_set() { + $response = array( + 'headers' => array( 'content-type' => 'text/html' ), + ); + + $this->assertSame( '', wp_remote_retrieve_header( $response, 'x-missing' ) ); + } + + /** + * Tests that the value of a header is returned even when it is empty or falsy. + * + * @ticket 66501 + * + * @dataProvider data_falsy_values + * + * @param mixed $value Value of the header. + */ + public function test_should_return_the_value_of_a_header_that_is_falsy( $value ) { + $response = array( + 'headers' => array( 'x-falsy' => $value ), + ); + + $this->assertSame( $value, wp_remote_retrieve_header( $response, 'x-falsy' ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_falsy_values() { + return array( + 'zero as a string' => array( '0' ), + 'empty string' => array( '' ), + ); + } + + /** + * Tests that an empty string is returned when the response is an error. + * + * @ticket 66501 + */ + public function test_should_return_an_empty_string_for_an_error() { + $this->assertSame( '', wp_remote_retrieve_header( new WP_Error( 'http_request_failed', 'Failed' ), 'content-type' ) ); + } + + /** + * Tests that an empty string is returned when the response has no headers. + * + * @ticket 66501 + */ + public function test_should_return_an_empty_string_when_the_response_has_no_headers() { + $this->assertSame( '', wp_remote_retrieve_header( array( 'body' => 'text' ), 'content-type' ) ); + } +} diff --git a/tests/phpunit/tests/http/wpRemoteRetrieveResponseMessage.php b/tests/phpunit/tests/http/wpRemoteRetrieveResponseMessage.php new file mode 100644 index 0000000000000..e46b5c313aae6 --- /dev/null +++ b/tests/phpunit/tests/http/wpRemoteRetrieveResponseMessage.php @@ -0,0 +1,106 @@ + array( + 'code' => 200, + 'message' => $message, + ), + ); + + $this->assertSame( $message, wp_remote_retrieve_response_message( $response ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_messages() { + return array( + 'OK' => array( 'OK' ), + 'Not Found' => array( 'Not Found' ), + 'Internal Server Error' => array( 'Internal Server Error' ), + 'empty message' => array( '' ), + ); + } + + /** + * Tests that only the message is returned, and not the code. + * + * @ticket 66501 + */ + public function test_should_not_return_the_code() { + $response = array( + 'response' => array( + 'code' => 404, + 'message' => 'Not Found', + ), + ); + + $this->assertNotSame( 404, wp_remote_retrieve_response_message( $response ) ); + $this->assertSame( 'Not Found', wp_remote_retrieve_response_message( $response ) ); + } + + /** + * Tests that an empty string is returned when the response is an error. + * + * @ticket 66501 + */ + public function test_should_return_an_empty_string_for_an_error() { + $this->assertSame( '', wp_remote_retrieve_response_message( new WP_Error( 'http_request_failed', 'Failed' ) ) ); + } + + /** + * Tests that an empty string is returned when the response does not have the response data. + * + * @ticket 66501 + */ + public function test_should_return_an_empty_string_when_the_response_has_no_response_data() { + $this->assertSame( '', wp_remote_retrieve_response_message( array( 'body' => 'text' ) ) ); + } + + /** + * Tests that an empty string is returned when the response data is not an array. + * + * @ticket 66501 + * + * @dataProvider data_response_data_that_is_not_an_array + * + * @param mixed $response_data Value of the response data. + */ + public function test_should_return_an_empty_string_when_the_response_data_is_not_an_array( $response_data ) { + $this->assertSame( '', wp_remote_retrieve_response_message( array( 'response' => $response_data ) ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_response_data_that_is_not_an_array() { + return array( + 'string' => array( 'OK' ), + 'null' => array( null ), + 'int' => array( 200 ), + ); + } +}