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



git-svn-id: https://develop.svn.wordpress.org/branches/5.3@54982 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2022-12-15 04:32:08 +00:00
parent e51cf19526
commit edcc32c2a6
2 changed files with 84 additions and 20 deletions

View File

@ -393,26 +393,6 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
$this->assertNotWPError( $res );
}
/**
* 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

@ -372,4 +372,88 @@ 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',
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.' );
}
}