summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorPeter Wilson <peterwilsoncc@git.wordpress.org>2022-12-15 04:49:42 +0000
committerPeter Wilson <peterwilsoncc@git.wordpress.org>2022-12-15 04:49:42 +0000
commit475cdd285302dc9fddc218831e41d155e9be3bc5 (patch)
tree1187b6400acefed77c00e131c701fa314921a7e5
parent983c277978bb524c9f2e6f6851384b2f1f855b86 (diff)
downloadwordpress-475cdd285302dc9fddc218831e41d155e9be3bc5.tar.gz
wordpress-475cdd285302dc9fddc218831e41d155e9be3bc5.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.4 branch. Fixes #57306. See #56793. git-svn-id: https://develop.svn.wordpress.org/branches/4.4@54991 602fd350-edb4-49c9-b593-d223f7449a82
-rw-r--r--tests/phpunit/tests/http/base.php20
-rw-r--r--tests/phpunit/tests/http/http.php78
2 files changed, 78 insertions, 20 deletions
diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php
index 18a4e9df65..d6b7c17640 100644
--- a/tests/phpunit/tests/http/base.php
+++ b/tests/phpunit/tests/http/base.php
@@ -361,26 +361,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 80b83f9d33..a6b6d98150 100644
--- a/tests/phpunit/tests/http/http.php
+++ b/tests/phpunit/tests/http/http.php
@@ -102,4 +102,82 @@ class Tests_HTTP_HTTP extends WP_UnitTestCase {
- ://example.com - assumed path in PHP >= 5.4.7, fails in <5.4.7
*/
}
+
+ /**
+ * 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,
+ );
+ }
}