diff options
author | Peter Wilson <peterwilsoncc@git.wordpress.org> | 2022-12-15 04:43:39 +0000 |
---|---|---|
committer | Peter Wilson <peterwilsoncc@git.wordpress.org> | 2022-12-15 04:43:39 +0000 |
commit | fc66834cdaa244ee106f009649f9958681b987b0 (patch) | |
tree | 47755577c7c5eb78a31b6a069b55409212d27848 | |
parent | de2a6eef25eb99712442fa00f437050355422d32 (diff) | |
download | wordpress-fc66834cdaa244ee106f009649f9958681b987b0.tar.gz wordpress-fc66834cdaa244ee106f009649f9958681b987b0.zip |
Built/Test tools, HTTP API: Refactor test for multiple location headers.
Remove wordpress.org as an external dependency testing `WP_HTTP::handle_redirects()`.
This refactors and reenables an existing test to call the `WP_HTTP::handle_redirects()` method directly with a mocked array of HTTP headers containing multiple location headers.
The test is moved from the external-http group to the http test group as it no longer makes an HTTP request.
Follow up to [54955].
Props SergeyBiryukov, dd32, peterwilsoncc.
Merges [54968] to the 4.8 branch.
Fixes #57306.
See #56793.
git-svn-id: https://develop.svn.wordpress.org/branches/4.8@54987 602fd350-edb4-49c9-b593-d223f7449a82
-rw-r--r-- | tests/phpunit/tests/http/base.php | 20 | ||||
-rw-r--r-- | tests/phpunit/tests/http/http.php | 77 |
2 files changed, 77 insertions, 20 deletions
diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php index 8e39b08899..bd897801c0 100644 --- a/tests/phpunit/tests/http/base.php +++ b/tests/phpunit/tests/http/base.php @@ -356,26 +356,6 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase { } /** - * Test HTTP Redirects with multiple Location headers specified - * - * @ticket 16890 - */ - 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->assertInternalType( 'array', 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->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) ); - - } - - /** * Test HTTP Cookie handling * * @ticket 21182 diff --git a/tests/phpunit/tests/http/http.php b/tests/phpunit/tests/http/http.php index 78b9f85e87..95c480eb75 100644 --- a/tests/phpunit/tests/http/http.php +++ b/tests/phpunit/tests/http/http.php @@ -314,4 +314,81 @@ class Tests_HTTP_HTTP extends WP_UnitTestCase { ); } + /** + * 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', + array( $this, 'filter_for_multiple_location_headers' ), + 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->assertTrue( is_array( $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_get_object()->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.' ); + } + + public function filter_for_multiple_location_headers( $response, $args, $url ) { + 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, + ); + } } |