Skip to content

Commit c88f03d

Browse files
committed
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.7 branch. Fixes #57306. See #56793. git-svn-id: https://develop.svn.wordpress.org/branches/5.7@54978 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c1ddf40 commit c88f03d

File tree

2 files changed

+84
-25
lines changed

2 files changed

+84
-25
lines changed

tests/phpunit/tests/http/base.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -444,31 +444,6 @@ function test_https_url_without_ssl_verification() {
444444
$this->assertNotWPError( $res );
445445
}
446446

447-
/**
448-
* Test HTTP Redirects with multiple Location headers specified.
449-
*
450-
* @ticket 16890
451-
*
452-
* @covers ::wp_remote_head
453-
* @covers ::wp_remote_retrieve_header
454-
* @covers ::wp_remote_get
455-
* @covers ::wp_remote_retrieve_body
456-
*/
457-
function test_multiple_location_headers() {
458-
$url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?multiple-location-headers=1';
459-
$res = wp_remote_head( $url, array( 'timeout' => 30 ) );
460-
461-
$this->skipTestOnTimeout( $res );
462-
$this->assertInternalType( 'array', wp_remote_retrieve_header( $res, 'location' ) );
463-
$this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) );
464-
465-
$res = wp_remote_get( $url, array( 'timeout' => 30 ) );
466-
467-
$this->skipTestOnTimeout( $res );
468-
$this->assertSame( 'PASS', wp_remote_retrieve_body( $res ) );
469-
470-
}
471-
472447
/**
473448
* Test HTTP Cookie handling.
474449
*

tests/phpunit/tests/http/http.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,88 @@ function wp_translate_php_url_constant_to_key_testcases() {
392392
);
393393
}
394394

395+
/**
396+
* Test HTTP Redirects with multiple Location headers specified.
397+
*
398+
* Ensure the WP_HTTP::handle_redirects() method handles multiple Location headers
399+
* and the HTTP request it makes uses the last Location header.
400+
*
401+
* @ticket 16890
402+
* @ticket 57306
403+
*
404+
* @covers WP_HTTP::handle_redirects
405+
*/
406+
public function test_multiple_location_headers() {
407+
$pre_http_request_filter_has_run = false;
408+
// Filter the response made by WP_HTTP::handle_redirects().
409+
add_filter(
410+
'pre_http_request',
411+
function( $response, $args, $url ) use ( &$pre_http_request_filter_has_run ) {
412+
$pre_http_request_filter_has_run = true;
413+
414+
// Assert the redirect URL is correct.
415+
$this->assertSame(
416+
$url,
417+
'http://example.com/?multiple-location-headers=1&redirected=two'
418+
);
419+
420+
if ( 'http://example.com/?multiple-location-headers=1&redirected=two' === $url ) {
421+
$body = 'PASS';
422+
} else {
423+
$body = 'FAIL';
424+
}
425+
426+
return array(
427+
'headers' => array(),
428+
'body' => $body,
429+
'response' => array(
430+
'code' => 200,
431+
'message' => 'OK',
432+
),
433+
'cookies' => array(),
434+
'filename' => null,
435+
);
436+
},
437+
10,
438+
3
439+
);
440+
441+
$headers = array(
442+
'server' => 'nginx',
443+
'date' => 'Sun, 11 Dec 2022 23:11:22 GMT',
444+
'content-type' => 'text/html; charset=utf-8',
445+
'location' => array(
446+
'http://example.com/?multiple-location-headers=1&redirected=one',
447+
'http://example.com/?multiple-location-headers=1&redirected=two',
448+
),
449+
);
450+
451+
// Test the tests: ensure multiple locations are passed to WP_HTTP::handle_redirects().
452+
$this->assertIsArray( $headers['location'], 'Location header is expected to be an array.' );
453+
$this->assertCount( 2, $headers['location'], 'Location header is expected to contain two values.' );
454+
455+
$args = array(
456+
'timeout' => 30,
457+
'_redirection' => 3,
458+
'redirection' => 2,
459+
'method' => 'GET',
460+
);
461+
462+
$redirect_response = WP_HTTP::handle_redirects(
463+
'http://example.com/?multiple-location-headers=1',
464+
$args,
465+
array(
466+
'headers' => $headers,
467+
'body' => '',
468+
'cookies' => array(),
469+
'filename' => null,
470+
'response' => array(
471+
'code' => 302,
472+
'message' => 'Found',
473+
),
474+
)
475+
);
476+
$this->assertSame( 'PASS', wp_remote_retrieve_body( $redirect_response ), 'Redirect response body is expected to be PASS.' );
477+
$this->assertTrue( $pre_http_request_filter_has_run, 'The pre_http_request filter is expected to run.' );
478+
}
395479
}

0 commit comments

Comments
 (0)