diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index ee42056bb3..ad104afe52 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -435,31 +435,6 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase { $this->assertNotWPError( $res ); } - /** - * Test HTTP Redirects with multiple Location headers specified. - * - * @ticket 16890 - * - * @covers ::wp_remote_head - * @covers ::wp_remote_retrieve_header - * @covers ::wp_remote_get - * @covers ::wp_remote_retrieve_body - */ - public function test_multiple_location_headers() { - $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?multiple-location-headers=1'; - $res = wp_remote_head( $url, array( 'timeout' => 30 ) ); - - $this->skipTestOnTimeout( $res ); - $this->assertIsArray( wp_remote_retrieve_header( $res, 'location' ) ); - $this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) ); - - $res = wp_remote_get( $url, array( 'timeout' => 30 ) ); - - $this->skipTestOnTimeout( $res ); - $this->assertSame( 'PASS', wp_remote_retrieve_body( $res ) ); - - } - /** * Test HTTP Cookie handling. * diff --git a/tests/phpunit/tests/http/http.php b/tests/phpunit/tests/http/http.php index 23a28f4d75..805eeeabac 100644 --- a/tests/phpunit/tests/http/http.php +++ b/tests/phpunit/tests/http/http.php @@ -567,4 +567,89 @@ class Tests_HTTP_HTTP extends WP_UnitTestCase { public function callback_remove_safe_ports( $ports ) { return array(); } + + /** + * Test HTTP Redirects with multiple Location headers specified. + * + * Ensure the WP_HTTP::handle_redirects() method handles multiple Location headers + * and the HTTP request it makes uses the last Location header. + * + * @ticket 16890 + * @ticket 57306 + * + * @covers WP_HTTP::handle_redirects + */ + public function test_multiple_location_headers() { + $pre_http_request_filter_has_run = false; + // Filter the response made by WP_HTTP::handle_redirects(). + add_filter( + 'pre_http_request', + function( $response, $args, $url ) use ( &$pre_http_request_filter_has_run ) { + $pre_http_request_filter_has_run = true; + + // Assert the redirect URL is correct. + $this->assertSame( + $url, + 'http://example.com/?multiple-location-headers=1&redirected=two' + ); + + if ( 'http://example.com/?multiple-location-headers=1&redirected=two' === $url ) { + $body = 'PASS'; + } else { + $body = 'FAIL'; + } + + return array( + 'headers' => array(), + 'body' => $body, + 'response' => array( + 'code' => 200, + 'message' => 'OK', + ), + 'cookies' => array(), + 'filename' => null, + ); + }, + 10, + 3 + ); + + $headers = array( + 'server' => 'nginx', + 'date' => 'Sun, 11 Dec 2022 23:11:22 GMT', + 'content-type' => 'text/html; charset=utf-8', + 'location' => array( + 'http://example.com/?multiple-location-headers=1&redirected=one', + 'http://example.com/?multiple-location-headers=1&redirected=two', + ), + ); + + // Test the tests: ensure multiple locations are passed to WP_HTTP::handle_redirects(). + $this->assertIsArray( $headers['location'], 'Location header is expected to be an array.' ); + $this->assertCount( 2, $headers['location'], 'Location header is expected to contain two values.' ); + + $args = array( + 'timeout' => 30, + '_redirection' => 3, + 'redirection' => 2, + 'method' => 'GET', + ); + + $redirect_response = WP_HTTP::handle_redirects( + 'http://example.com/?multiple-location-headers=1', + $args, + array( + 'headers' => $headers, + 'body' => '', + 'cookies' => array(), + 'filename' => null, + 'response' => array( + 'code' => 302, + 'message' => 'Found', + ), + ) + ); + $this->assertSame( 'PASS', wp_remote_retrieve_body( $redirect_response ), 'Redirect response body is expected to be PASS.' ); + $this->assertTrue( $pre_http_request_filter_has_run, 'The pre_http_request filter is expected to run.' ); + } }