diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index a4120f8e7437c..ba78b8b2ed15d 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()). @@ -70,8 +78,20 @@ 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. + * + * @var array + */ + protected $requests_options = array(); + /** * Starts a local HTTP fixture server for stream and response-size tests. * @@ -296,9 +316,75 @@ 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 ) ) { + 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; + } + + 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_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). + * @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' + ); } /** @@ -306,7 +392,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 +403,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 +416,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 +429,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 +440,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 +451,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 +465,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 +481,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 +491,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 +501,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 +513,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 +528,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 +543,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 +596,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 +634,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 +769,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 +818,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'] ); @@ -741,26 +864,63 @@ 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. + * + * 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 * * @covers ::wp_remote_head */ public function test_https_url_without_ssl_verification() { - $url = 'https://wordpress.org/'; - $args = array( - 'sslverify' => false, - ); - + 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 ); - $res = $this->wp_remote_head( $url, $args ); - - remove_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) ); + try { + // 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 ); + } + $this->assertWPError( $res ); $this->assertNotEmpty( $this->http_request_args['sslcertificates'] ); - $this->assertNotWPError( $res ); + $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. + * + * @throws WpOrg\Requests\Exception Always, after capturing options. + * + * @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 capture_requests_options( $url, $headers, $data, $type, &$options ) { + $this->requests_options = $options; + + throw new WpOrg\Requests\Exception( 'Stopped after capturing the request options.', 'test_stopped' ); } /** @@ -779,7 +939,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 +961,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 +989,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 +1017,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 ); 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';