From a2529fe578e7bc46ae4bdc67feb82d66e485b6d8 Mon Sep 17 00:00:00 2001 From: Shail Mehta Date: Thu, 5 Nov 2026 10:02:06 +0530 Subject: [PATCH 01/10] Tests: Drop external-http from `Tests_HTTP_curl`. --- tests/phpunit/tests/http/curl.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/http/curl.php b/tests/phpunit/tests/http/curl.php index 2bf1fbc7f0570..8afdaa9455d92 100644 --- a/tests/phpunit/tests/http/curl.php +++ b/tests/phpunit/tests/http/curl.php @@ -4,7 +4,8 @@ /** * @group http - * @group external-http + * + * @ticket 63914 */ class Tests_HTTP_curl extends WP_HTTP_UnitTestCase { public $transport = 'curl'; From b61e5d5929eaca0a06fc2be4b0376dbac84ec013 Mon Sep 17 00:00:00 2001 From: Shail Mehta Date: Thu, 5 Nov 2026 10:03:30 +0530 Subject: [PATCH 02/10] Tests: Drop live HTTP fallbacks from `WP_HTTP_UnitTestCase` --- tests/phpunit/tests/http/base.php | 126 +++++++++++++++++++++++++----- 1 file changed, 105 insertions(+), 21 deletions(-) diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index a4120f8e7437c..1c8ad63133214 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -11,8 +11,16 @@ * The WP_Http tests require a class-http.php file of r17550 or later. */ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase { - // You can use your own version of data/WPHTTP-testcase-redirection-script.php here. - public $redirection_script = 'http://api.wordpress.org/core/tests/1.0/redirection.php'; + /** + * URL of the local redirection fixture (set in set_up() when available). + * + * Prefer the local PHP built-in server fixture. Tests that need redirection + * coverage must call require_redirection_fixture() so missing fixtures skip + * or fail instead of falling back to a live api.wordpress.org request. + * + * @var string + */ + public $redirection_script = ''; /** * URL of the local stream/size fixture (set in set_up_before_class()). @@ -306,7 +314,7 @@ public function filter_http_request_args( array $args ) { */ public function test_redirect_on_301() { // 5 : 5 & 301. - $res = $this->wp_remote_request( $this->redirection_script . '?code=301&rt=' . 5, array( 'redirection' => 5 ) ); + $res = $this->wp_remote_request( $this->get_redirection_script() . '?code=301&rt=' . 5, array( 'redirection' => 5 ) ); $this->assertNotWPError( $res ); $this->assertSame( 200, (int) $res['response']['code'] ); @@ -317,7 +325,7 @@ public function test_redirect_on_301() { */ public function test_redirect_on_302() { // 5 : 5 & 302. - $res = $this->wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 5 ) ); + $res = $this->wp_remote_request( $this->get_redirection_script() . '?code=302&rt=' . 5, array( 'redirection' => 5 ) ); $this->assertNotWPError( $res ); $this->assertSame( 200, (int) $res['response']['code'] ); @@ -330,7 +338,7 @@ public function test_redirect_on_302() { */ public function test_redirect_on_301_no_redirect() { // 5 > 0 & 301. - $res = $this->wp_remote_request( $this->redirection_script . '?code=301&rt=' . 5, array( 'redirection' => 0 ) ); + $res = $this->wp_remote_request( $this->get_redirection_script() . '?code=301&rt=' . 5, array( 'redirection' => 0 ) ); $this->assertNotWPError( $res ); $this->assertSame( 301, (int) $res['response']['code'] ); @@ -343,7 +351,7 @@ public function test_redirect_on_301_no_redirect() { */ public function test_redirect_on_302_no_redirect() { // 5 > 0 & 302. - $res = $this->wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 0 ) ); + $res = $this->wp_remote_request( $this->get_redirection_script() . '?code=302&rt=' . 5, array( 'redirection' => 0 ) ); $this->assertNotWPError( $res ); $this->assertSame( 302, (int) $res['response']['code'] ); @@ -354,7 +362,7 @@ public function test_redirect_on_302_no_redirect() { */ public function test_redirections_equal() { // 5 - 5. - $res = $this->wp_remote_request( $this->redirection_script . '?rt=' . 5, array( 'redirection' => 5 ) ); + $res = $this->wp_remote_request( $this->get_redirection_script() . '?rt=' . 5, array( 'redirection' => 5 ) ); $this->assertNotWPError( $res ); $this->assertSame( 200, (int) $res['response']['code'] ); @@ -365,7 +373,7 @@ public function test_redirections_equal() { */ public function test_no_head_redirections() { // No redirections on HEAD request. - $res = $this->wp_remote_request( $this->redirection_script . '?code=302&rt=' . 1, array( 'method' => 'HEAD' ) ); + $res = $this->wp_remote_request( $this->get_redirection_script() . '?code=302&rt=' . 1, array( 'method' => 'HEAD' ) ); $this->assertNotWPError( $res ); $this->assertSame( 302, (int) $res['response']['code'] ); @@ -379,7 +387,7 @@ public function test_no_head_redirections() { public function test_redirect_on_head() { // Redirections on HEAD request when Requested. $res = $this->wp_remote_request( - $this->redirection_script . '?rt=' . 5, + $this->get_redirection_script() . '?rt=' . 5, array( 'redirection' => 5, 'method' => 'HEAD', @@ -395,7 +403,7 @@ public function test_redirect_on_head() { */ public function test_redirections_greater() { // 10 > 5. - $res = $this->wp_remote_request( $this->redirection_script . '?rt=' . 10, array( 'redirection' => 5 ) ); + $res = $this->wp_remote_request( $this->get_redirection_script() . '?rt=' . 10, array( 'redirection' => 5 ) ); $this->assertWPError( $res ); } @@ -405,7 +413,7 @@ public function test_redirections_greater() { */ public function test_redirections_greater_edgecase() { // 6 > 5 (close edge case). - $res = $this->wp_remote_request( $this->redirection_script . '?rt=' . 6, array( 'redirection' => 5 ) ); + $res = $this->wp_remote_request( $this->get_redirection_script() . '?rt=' . 6, array( 'redirection' => 5 ) ); $this->assertWPError( $res ); } @@ -415,7 +423,7 @@ public function test_redirections_greater_edgecase() { */ public function test_redirections_less_edgecase() { // 4 < 5 (close edge case). - $res = $this->wp_remote_request( $this->redirection_script . '?rt=' . 4, array( 'redirection' => 5 ) ); + $res = $this->wp_remote_request( $this->get_redirection_script() . '?rt=' . 4, array( 'redirection' => 5 ) ); $this->assertNotWPError( $res ); } @@ -427,7 +435,7 @@ public function test_redirections_less_edgecase() { */ public function test_redirections_zero_redirections_specified() { // 0 redirections asked for, should return the document? - $res = $this->wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 0 ) ); + $res = $this->wp_remote_request( $this->get_redirection_script() . '?code=302&rt=' . 5, array( 'redirection' => 0 ) ); $this->assertNotWPError( $res ); $this->assertSame( 302, (int) $res['response']['code'] ); @@ -442,7 +450,7 @@ public function test_redirections_zero_redirections_specified() { */ public function test_location_header_on_201() { // Prints PASS on initial load, FAIL if the client follows the specified redirection. - $res = $this->wp_remote_request( $this->redirection_script . '?201-location=true' ); + $res = $this->wp_remote_request( $this->get_redirection_script() . '?201-location=true' ); $this->assertNotWPError( $res ); $this->assertSame( 'PASS', $res['body'] ); @@ -457,7 +465,7 @@ public function test_location_header_on_201() { * @covers ::wp_remote_retrieve_body */ public function test_no_redirection_on_PUT() { - $url = $this->redirection_script . '?201-location=1'; + $url = $this->get_redirection_script() . '?201-location=1'; // Test 301 - POST to POST. $res = $this->wp_remote_request( @@ -510,7 +518,7 @@ public function test_send_headers() { 'test2' => 0, 'test3' => '', ); - $res = $this->wp_remote_request( $this->redirection_script . '?header-check', array( 'headers' => $headers ) ); + $res = $this->wp_remote_request( $this->get_redirection_script() . '?header-check', array( 'headers' => $headers ) ); $this->assertNotWPError( $res ); @@ -548,6 +556,43 @@ protected function require_file_stream_fixture() { $this->fail( 'The local HTTP stream fixture server was not started.' ); } + /** + * Ensures the local redirection fixture server is available. + * + * Skips when a prerequisite is missing. Fails with a diagnostic when + * fixture startup failed unexpectedly. Does not fall back to the live + * api.wordpress.org redirection script. + * + * @ticket 63914 + */ + protected function require_redirection_fixture() { + if ( ! empty( $this->redirection_script ) ) { + return; + } + + if ( self::$file_stream_fixture_skip_reason ) { + $this->markTestSkipped( self::$file_stream_fixture_skip_reason ); + } + + if ( self::$file_stream_fixture_fail_reason ) { + $this->fail( self::$file_stream_fixture_fail_reason ); + } + + $this->fail( 'The local HTTP redirection fixture server was not started.' ); + } + + /** + * Returns the local redirection fixture URL, requiring it first. + * + * @ticket 63914 + * + * @return string Local redirection fixture URL. + */ + protected function get_redirection_script() { + $this->require_redirection_fixture(); + return $this->redirection_script; + } + /** * @ticket 63914 * @@ -646,7 +691,7 @@ public function test_request_limited_size() { * @covers ::wp_remote_retrieve_body */ public function test_post_redirect_to_method_300( $response_code, $method ) { - $url = $this->redirection_script . '?post-redirect-to-method=1'; + $url = $this->get_redirection_script() . '?post-redirect-to-method=1'; $res = $this->wp_remote_post( add_query_arg( 'response_code', $response_code, $url ), array( 'timeout' => 30 ) ); @@ -695,7 +740,7 @@ public static function data_post_redirect_to_method_300() { * @covers ::wp_remote_retrieve_body */ public function test_ip_url_with_host_header() { - $parts = wp_parse_url( $this->redirection_script ); + $parts = wp_parse_url( $this->get_redirection_script() ); $this->assertIsArray( $parts ); $this->assertNotEmpty( $parts['host'] ); $this->assertNotEmpty( $parts['path'] ); @@ -743,7 +788,11 @@ public function test_ip_url_with_host_header() { /** * Test HTTP requests where SSL verification is disabled but the CA bundle is still populated. * + * Mocks the response: this assertion is about request-arg population in WP_Http, + * not live TLS against wordpress.org. + * * @ticket 33978 + * @ticket 63914 * * @covers ::wp_remote_head */ @@ -754,15 +803,43 @@ public function test_https_url_without_ssl_verification() { ); add_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) ); + add_filter( 'pre_http_request', array( $this, 'mock_https_head_response' ), 10, 3 ); $res = $this->wp_remote_head( $url, $args ); remove_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) ); + remove_filter( 'pre_http_request', array( $this, 'mock_https_head_response' ), 10 ); $this->assertNotEmpty( $this->http_request_args['sslcertificates'] ); + $this->assertFalse( $this->http_request_args['sslverify'] ); $this->assertNotWPError( $res ); } + /** + * Returns a canned HTTPS HEAD response for offline CA-bundle coverage. + * + * @param false|array|WP_Error $preempt Whether to preempt the request. + * @param array $args Request arguments. + * @param string $url Request URL. + * @return array|false|WP_Error Canned response for the wordpress.org HEAD URL. + */ + public function mock_https_head_response( $preempt, $args, $url ) { + if ( 'https://wordpress.org/' !== $url ) { + return $preempt; + } + + return array( + 'headers' => array(), + 'body' => '', + 'response' => array( + 'code' => 200, + 'message' => 'OK', + ), + 'cookies' => array(), + 'filename' => null, + ); + } + /** * Test HTTP Cookie handling. * @@ -779,7 +856,7 @@ public function test_https_url_without_ssl_verification() { * @covers ::wp_remote_retrieve_body */ public function test_cookie_handling() { - $url = $this->redirection_script . '?cookie-test=1'; + $url = $this->get_redirection_script() . '?cookie-test=1'; $res = $this->wp_remote_get( $url ); @@ -801,6 +878,8 @@ public function test_cookie_handling() { * @covers WP_Http::normalize_cookies */ public function test_parent_domain_cookie_is_sent_on_redirect() { + // Allow the canned Requests transport to run outside external-http. + remove_filter( 'pre_http_request', array( $this, 'block_external_http_request' ), PHP_INT_MAX ); add_action( 'requests-requests.before_request', array( $this, 'inject_parent_domain_cookie_test_transport' ), 10, 5 ); try { @@ -827,10 +906,15 @@ public function inject_parent_domain_cookie_test_transport( $url, $headers, $dat } /** - * Test if HTTPS support works. + * Test if HTTPS support works against a live endpoint. + * + * Remains in external-http: this is intentional live TLS smoke coverage for + * the real transport. Parsing and arg-shape checks live elsewhere offline. * * @group ssl + * @group external-http * @ticket 25007 + * @ticket 63914 * * @covers ::wp_remote_get */ @@ -850,7 +934,7 @@ public function test_ssl() { * @covers ::wp_remote_request */ public function test_url_with_double_slashes_path() { - $url = $this->redirection_script . '?rt=' . 0; + $url = $this->get_redirection_script() . '?rt=' . 0; $path = parse_url( $url, PHP_URL_PATH ); $url = str_replace( $path, '/' . $path, $url ); From 6ccc6ed5bdeaf80a1f3fddf529b7313802bb6d2f Mon Sep 17 00:00:00 2001 From: Shail Mehta Date: Thu, 5 Nov 2026 10:06:39 +0530 Subject: [PATCH 03/10] Tests: Allow loopback requests outside `external-http` --- tests/phpunit/includes/abstract-testcase.php | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 0d56efd9c392d..a48ce009fd56f 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -959,6 +959,15 @@ public function block_external_http_request( $response, array $args, string $url return $response; } + /* + * Loopback requests (local PHPUnit fixtures) are not external network + * calls. Allow them so real-transport coverage can run outside the + * external-http group against 127.0.0.1 / localhost. + */ + if ( $this->is_loopback_http_url( $url ) ) { + return $response; + } + $this->blocked_http_requests[] = $url; return new WP_Error( @@ -967,6 +976,23 @@ public function block_external_http_request( $response, array $args, string $url ); } + /** + * Whether a URL targets a loopback host used by local test fixtures. + * + * @since 7.2.0 + * + * @param string $url Request URL. + * @return bool True when the host is 127.0.0.1, localhost, or ::1. + */ + protected function is_loopback_http_url( string $url ): bool { + $host = wp_parse_url( $url, PHP_URL_HOST ); + if ( ! is_string( $host ) || '' === $host ) { + return false; + } + + return in_array( strtolower( $host ), array( '127.0.0.1', 'localhost', '::1' ), true ); + } + /** * Detects post-test failure conditions. * From e9e7b9d9ec36bcd84761ac2e30de12ba6c239672 Mon Sep 17 00:00:00 2001 From: Shail Mehta Date: Thu, 5 Nov 2026 10:13:39 +0530 Subject: [PATCH 04/10] Fix IPv6 localhost format in test case Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- tests/phpunit/includes/abstract-testcase.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index a48ce009fd56f..c502827af8598 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -990,7 +990,7 @@ protected function is_loopback_http_url( string $url ): bool { return false; } - return in_array( strtolower( $host ), array( '127.0.0.1', 'localhost', '::1' ), true ); + return in_array( strtolower( $host ), array( '127.0.0.1', 'localhost', '[::1]' ), true ); } /** From 8b0790351bfaed840a0f09968b526b7c0b423cee Mon Sep 17 00:00:00 2001 From: Shail Mehta Date: Thu, 5 Nov 2026 10:25:47 +0530 Subject: [PATCH 05/10] Tests: Block external redirects from loopback HTTP fixtures --- tests/phpunit/includes/abstract-testcase.php | 34 ++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index c502827af8598..cd9f2502304bb 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -963,8 +963,16 @@ public function block_external_http_request( $response, array $args, string $url * Loopback requests (local PHPUnit fixtures) are not external network * calls. Allow them so real-transport coverage can run outside the * external-http group against 127.0.0.1 / localhost. + * + * Requests follows redirects inside the same call without returning to + * pre_http_request, so also watch redirect targets and block any that + * leave loopback. */ if ( $this->is_loopback_http_url( $url ) ) { + if ( ! has_action( 'requests-requests.before_redirect', array( $this, 'block_external_http_redirect' ) ) ) { + add_action( 'requests-requests.before_redirect', array( $this, 'block_external_http_redirect' ), 10, 5 ); + } + return $response; } @@ -976,6 +984,32 @@ public function block_external_http_request( $response, array $args, string $url ); } + /** + * Blocks redirects from loopback fixtures to non-loopback URLs. + * + * @since 7.2.0 + * + * @throws WpOrg\Requests\Exception When the redirect target is not loopback. + * + * @param string $location Redirect location (passed by reference by Requests). + * @param array $req_headers Request headers. + * @param array|null $req_data Request data. + * @param array $options Requests options. + * @param WpOrg\Requests\Response $response Response that triggered the redirect. + */ + public function block_external_http_redirect( &$location, &$req_headers, &$req_data, &$options, $response ) { + if ( $this->is_loopback_http_url( $location ) ) { + return; + } + + $this->blocked_http_requests[] = $location; + + throw new WpOrg\Requests\Exception( + 'External HTTP redirects are blocked in tests that are not in the `external-http` group.', + 'test_external_http_blocked' + ); + } + /** * Whether a URL targets a loopback host used by local test fixtures. * From 91e5c2ac223b96a5adb5c0c1766d0d6951f58f28 Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Fri, 6 Nov 2026 04:01:52 +0000 Subject: [PATCH 06/10] Tests: Scope HTTP fixture exemption and restore #33978 coverage. Limit the external-HTTP guard override to WP_HTTP_UnitTestCase fixture host/port, and capture Requests verify options instead of mocking pre_http_request. --- tests/phpunit/includes/abstract-testcase.php | 60 -------- tests/phpunit/tests/http/base.php | 141 +++++++++++++------ 2 files changed, 101 insertions(+), 100 deletions(-) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index cd9f2502304bb..0d56efd9c392d 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -959,23 +959,6 @@ public function block_external_http_request( $response, array $args, string $url return $response; } - /* - * Loopback requests (local PHPUnit fixtures) are not external network - * calls. Allow them so real-transport coverage can run outside the - * external-http group against 127.0.0.1 / localhost. - * - * Requests follows redirects inside the same call without returning to - * pre_http_request, so also watch redirect targets and block any that - * leave loopback. - */ - if ( $this->is_loopback_http_url( $url ) ) { - if ( ! has_action( 'requests-requests.before_redirect', array( $this, 'block_external_http_redirect' ) ) ) { - add_action( 'requests-requests.before_redirect', array( $this, 'block_external_http_redirect' ), 10, 5 ); - } - - return $response; - } - $this->blocked_http_requests[] = $url; return new WP_Error( @@ -984,49 +967,6 @@ public function block_external_http_request( $response, array $args, string $url ); } - /** - * Blocks redirects from loopback fixtures to non-loopback URLs. - * - * @since 7.2.0 - * - * @throws WpOrg\Requests\Exception When the redirect target is not loopback. - * - * @param string $location Redirect location (passed by reference by Requests). - * @param array $req_headers Request headers. - * @param array|null $req_data Request data. - * @param array $options Requests options. - * @param WpOrg\Requests\Response $response Response that triggered the redirect. - */ - public function block_external_http_redirect( &$location, &$req_headers, &$req_data, &$options, $response ) { - if ( $this->is_loopback_http_url( $location ) ) { - return; - } - - $this->blocked_http_requests[] = $location; - - throw new WpOrg\Requests\Exception( - 'External HTTP redirects are blocked in tests that are not in the `external-http` group.', - 'test_external_http_blocked' - ); - } - - /** - * Whether a URL targets a loopback host used by local test fixtures. - * - * @since 7.2.0 - * - * @param string $url Request URL. - * @return bool True when the host is 127.0.0.1, localhost, or ::1. - */ - protected function is_loopback_http_url( string $url ): bool { - $host = wp_parse_url( $url, PHP_URL_HOST ); - if ( ! is_string( $host ) || '' === $host ) { - return false; - } - - return in_array( strtolower( $host ), array( '127.0.0.1', 'localhost', '[::1]' ), true ); - } - /** * Detects post-test failure conditions. * diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index 1c8ad63133214..2e5062cc1fd9f 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -78,7 +78,12 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase { */ private static $file_stream_fixture_fail_reason = null; - protected $http_request_args; + /** + * Captured Requests options from the most recent intercepted request. + * + * @var array + */ + protected $requests_options = array(); /** * Starts a local HTTP fixture server for stream and response-size tests. @@ -304,9 +309,77 @@ public function set_up() { } } - public function filter_http_request_args( array $args ) { - $this->http_request_args = $args; - return $args; + /** + * Allows only this class's local HTTP fixture through the external-HTTP guard. + * + * Other tests continue to use the parent blocker for any non-mocked request. + * + * @param false|array|WP_Error $response A preemptive response, or false when none was given. + * @param array $args Request arguments. + * @param string $url The request URL. + * @return false|array|WP_Error The preemptive response, or an error for a blocked request. + */ + public function block_external_http_request( $response, array $args, string $url ) { + if ( false === $response && $this->is_fixture_url( $url ) ) { + add_action( 'requests-requests.before_redirect', array( $this, 'block_external_fixture_redirect' ) ); + + return $response; + } + + return parent::block_external_http_request( $response, $args, $url ); + } + + /** + * Whether a URL targets this class's local fixture server host and port. + * + * @param string $url Request URL. + * @return bool True when the URL matches the fixture host and port. + */ + protected function is_fixture_url( string $url ): bool { + if ( ! self::$file_stream_fixture_url ) { + return false; + } + + $fixture = wp_parse_url( self::$file_stream_fixture_url ); + $parts = wp_parse_url( $url ); + + if ( ! is_array( $fixture ) || ! is_array( $parts ) ) { + return false; + } + + if ( empty( $fixture['host'] ) || empty( $parts['host'] ) ) { + return false; + } + + $fixture_port = isset( $fixture['port'] ) ? (int) $fixture['port'] : ( 'https' === ( $fixture['scheme'] ?? 'http' ) ? 443 : 80 ); + $request_port = isset( $parts['port'] ) ? (int) $parts['port'] : ( 'https' === ( $parts['scheme'] ?? 'http' ) ? 443 : 80 ); + + return strtolower( $fixture['host'] ) === strtolower( $parts['host'] ) + && $fixture_port === $request_port; + } + + /** + * Blocks redirects from the local fixture to non-fixture URLs. + * + * @throws WpOrg\Requests\Exception When the redirect target leaves the fixture server. + * + * @param string $location Redirect location (passed by reference by Requests). + * @param array $req_headers Request headers. + * @param array|null $req_data Request data. + * @param array $options Requests options. + * @param WpOrg\Requests\Response $response Response that triggered the redirect. + */ + public function block_external_fixture_redirect( &$location, &$req_headers, &$req_data, &$options, $response ) { + if ( $this->is_fixture_url( $location ) ) { + return; + } + + $this->blocked_http_requests[] = $location; + + throw new WpOrg\Requests\Exception( + 'External HTTP redirects are blocked in tests that are not in the `external-http` group.', + 'test_external_http_blocked' + ); } /** @@ -786,10 +859,10 @@ public function test_ip_url_with_host_header() { } /** - * Test HTTP requests where SSL verification is disabled but the CA bundle is still populated. + * Test HTTP requests where SSL verification is disabled. * - * Mocks the response: this assertion is about request-arg population in WP_Http, - * not live TLS against wordpress.org. + * Captures Requests options after WP_Http::request() maps sslverify=false to + * verify/verifyname, which is the #33978 / r34639 behavior. Stops before connecting. * * @ticket 33978 * @ticket 63914 @@ -797,47 +870,35 @@ public function test_ip_url_with_host_header() { * @covers ::wp_remote_head */ public function test_https_url_without_ssl_verification() { - $url = 'https://wordpress.org/'; - $args = array( - 'sslverify' => false, - ); - - add_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) ); - add_filter( 'pre_http_request', array( $this, 'mock_https_head_response' ), 10, 3 ); - - $res = $this->wp_remote_head( $url, $args ); + remove_filter( 'pre_http_request', array( $this, 'block_external_http_request' ), PHP_INT_MAX ); + add_action( 'requests-requests.before_request', array( $this, 'capture_requests_options' ), 10, 5 ); - remove_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) ); - remove_filter( 'pre_http_request', array( $this, 'mock_https_head_response' ), 10 ); + try { + $res = $this->wp_remote_head( 'https://wordpress.org/', array( 'sslverify' => false ) ); + } finally { + remove_action( 'requests-requests.before_request', array( $this, 'capture_requests_options' ), 10 ); + } - $this->assertNotEmpty( $this->http_request_args['sslcertificates'] ); - $this->assertFalse( $this->http_request_args['sslverify'] ); - $this->assertNotWPError( $res ); + $this->assertWPError( $res ); + $this->assertFalse( $this->requests_options['verify'] ); + $this->assertFalse( $this->requests_options['verifyname'] ); } /** - * Returns a canned HTTPS HEAD response for offline CA-bundle coverage. + * Captures Requests options and stops before a network connection. + * + * @throws WpOrg\Requests\Exception Always, after capturing options. * - * @param false|array|WP_Error $preempt Whether to preempt the request. - * @param array $args Request arguments. - * @param string $url Request URL. - * @return array|false|WP_Error Canned response for the wordpress.org HEAD URL. + * @param string $url Request URL. + * @param array $headers Request headers. + * @param string|array $data Request data. + * @param string $type HTTP method. + * @param array $options Request options (passed by reference). */ - public function mock_https_head_response( $preempt, $args, $url ) { - if ( 'https://wordpress.org/' !== $url ) { - return $preempt; - } + public function capture_requests_options( $url, $headers, $data, $type, &$options ) { + $this->requests_options = $options; - return array( - 'headers' => array(), - 'body' => '', - 'response' => array( - 'code' => 200, - 'message' => 'OK', - ), - 'cookies' => array(), - 'filename' => null, - ); + throw new WpOrg\Requests\Exception( 'Stopped after capturing the request options.', 'test_stopped' ); } /** From 75185627e21623c152a94861067f9ce4534b8f6b Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Fri, 6 Nov 2026 04:03:35 +0000 Subject: [PATCH 07/10] Tests: --- tests/phpunit/tests/http/base.php | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index 2e5062cc1fd9f..87dccc1d79949 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -340,27 +340,23 @@ protected function is_fixture_url( string $url ): bool { return false; } - $fixture = wp_parse_url( self::$file_stream_fixture_url ); - $parts = wp_parse_url( $url ); - - if ( ! is_array( $fixture ) || ! is_array( $parts ) ) { - return false; - } - - if ( empty( $fixture['host'] ) || empty( $parts['host'] ) ) { - return false; - } - - $fixture_port = isset( $fixture['port'] ) ? (int) $fixture['port'] : ( 'https' === ( $fixture['scheme'] ?? 'http' ) ? 443 : 80 ); - $request_port = isset( $parts['port'] ) ? (int) $parts['port'] : ( 'https' === ( $parts['scheme'] ?? 'http' ) ? 443 : 80 ); - - return strtolower( $fixture['host'] ) === strtolower( $parts['host'] ) - && $fixture_port === $request_port; + $fixture_host = wp_parse_url( self::$file_stream_fixture_url, PHP_URL_HOST ); + $fixture_port = wp_parse_url( self::$file_stream_fixture_url, PHP_URL_PORT ); + $host = wp_parse_url( $url, PHP_URL_HOST ); + $port = wp_parse_url( $url, PHP_URL_PORT ); + + return is_string( $fixture_host ) + && is_string( $host ) + && strtolower( $fixture_host ) === strtolower( $host ) + && (int) $fixture_port === (int) $port; } /** * Blocks redirects from the local fixture to non-fixture URLs. * + * Defensive only: the current redirection fixture never leaves this host/port, + * so this callback is not exercised by existing tests. + * * @throws WpOrg\Requests\Exception When the redirect target leaves the fixture server. * * @param string $location Redirect location (passed by reference by Requests). From 3bace18b78f9dac059fe6c82f7de9fb9b6a6527d Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Fri, 6 Nov 2026 04:18:05 +0000 Subject: [PATCH 08/10] Tests: --- tests/phpunit/tests/http/base.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index 87dccc1d79949..76968f4e64665 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -321,7 +321,9 @@ public function set_up() { */ public function block_external_http_request( $response, array $args, string $url ) { if ( false === $response && $this->is_fixture_url( $url ) ) { - add_action( 'requests-requests.before_redirect', array( $this, 'block_external_fixture_redirect' ) ); + if ( ! has_action( 'requests-requests.before_redirect', array( $this, 'block_external_fixture_redirect' ) ) ) { + add_action( 'requests-requests.before_redirect', array( $this, 'block_external_fixture_redirect' ), 10, 5 ); + } return $response; } From 3d313b1be5e3cefdd8070a4635af4f6a5cd4368a Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Fri, 6 Nov 2026 15:54:49 +0000 Subject: [PATCH 09/10] Tests: Restore sslcertificates assertion for offline SSL coverage. Keeps the #33978 CA-bundle path check alongside the Requests verify options captured before connecting. --- tests/phpunit/tests/http/base.php | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index 76968f4e64665..674b2b9bc0c94 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -78,6 +78,13 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase { */ private static $file_stream_fixture_fail_reason = null; + /** + * Captured HTTP request args from the most recent filtered request. + * + * @var array + */ + protected $http_request_args; + /** * Captured Requests options from the most recent intercepted request. * @@ -859,8 +866,9 @@ public function test_ip_url_with_host_header() { /** * Test HTTP requests where SSL verification is disabled. * - * Captures Requests options after WP_Http::request() maps sslverify=false to - * verify/verifyname, which is the #33978 / r34639 behavior. Stops before connecting. + * Asserts that sslcertificates stays set when sslverify is false (#33978 / + * r34639), and that WP_Http::request() maps that to Requests verify / + * verifyname. Stops before connecting. * * @ticket 33978 * @ticket 63914 @@ -869,19 +877,33 @@ public function test_ip_url_with_host_header() { */ public function test_https_url_without_ssl_verification() { remove_filter( 'pre_http_request', array( $this, 'block_external_http_request' ), PHP_INT_MAX ); + add_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) ); add_action( 'requests-requests.before_request', array( $this, 'capture_requests_options' ), 10, 5 ); try { $res = $this->wp_remote_head( 'https://wordpress.org/', array( 'sslverify' => false ) ); } finally { + remove_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) ); remove_action( 'requests-requests.before_request', array( $this, 'capture_requests_options' ), 10 ); } $this->assertWPError( $res ); + $this->assertNotEmpty( $this->http_request_args['sslcertificates'] ); $this->assertFalse( $this->requests_options['verify'] ); $this->assertFalse( $this->requests_options['verifyname'] ); } + /** + * Stores HTTP request args for later assertion. + * + * @param array $args Request arguments. + * @return array Unmodified request arguments. + */ + public function filter_http_request_args( array $args ) { + $this->http_request_args = $args; + return $args; + } + /** * Captures Requests options and stops before a network connection. * From bc13ffd60b58c27f53e1f6f0554842980f6f2bdd Mon Sep 17 00:00:00 2001 From: shail-mehta Date: Fri, 6 Nov 2026 16:01:39 +0000 Subject: [PATCH 10/10] Tests: Avoid retry delays in offline SSL verification coverage. Use the global wp_remote_head() so the intentional capture error does not sleep through retry_on_error(). --- tests/phpunit/tests/http/base.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index 674b2b9bc0c94..ba78b8b2ed15d 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -881,7 +881,9 @@ public function test_https_url_without_ssl_verification() { add_action( 'requests-requests.before_request', array( $this, 'capture_requests_options' ), 10, 5 ); try { - $res = $this->wp_remote_head( 'https://wordpress.org/', array( 'sslverify' => false ) ); + // Call the global directly: the intentional WP_Error from the capture + // callback must not go through retry_on_error()'s 5s sleeps. + $res = wp_remote_head( 'https://wordpress.org/', array( 'sslverify' => false ) ); } finally { remove_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) ); remove_action( 'requests-requests.before_request', array( $this, 'capture_requests_options' ), 10 );