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.3 branch.
Fixes #57306.
See #56793.



git-svn-id: https://develop.svn.wordpress.org/branches/4.3@54992 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2022-12-15 04:51:12 +00:00
parent 0a8e2e969d
commit 3c512136d3
2 changed files with 78 additions and 20 deletions

View File

@ -331,26 +331,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
*

View File

@ -102,6 +102,84 @@ 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,
);
}
}
/**