Skip to content

Commit 4b4faae

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. Fixes #57306. See #56793. git-svn-id: https://develop.svn.wordpress.org/trunk@54968 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 40c097e commit 4b4faae

File tree

2 files changed

+85
-28
lines changed

2 files changed

+85
-28
lines changed

tests/phpunit/tests/http/base.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -435,34 +435,6 @@ public function test_https_url_without_ssl_verification() {
435435
$this->assertNotWPError( $res );
436436
}
437437

438-
/**
439-
* Test HTTP Redirects with multiple Location headers specified.
440-
*
441-
* This test has been disabled due to api.wordpress.org failing to
442-
* send two Location headers. See #57306.
443-
*
444-
* @ticket 16890
445-
*
446-
* @covers ::wp_remote_head
447-
* @covers ::wp_remote_retrieve_header
448-
* @covers ::wp_remote_get
449-
* @covers ::wp_remote_retrieve_body
450-
*/
451-
public function disabled_test_multiple_location_headers() {
452-
$url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?multiple-location-headers=1';
453-
$res = wp_remote_head( $url, array( 'timeout' => 30 ) );
454-
455-
$this->skipTestOnTimeout( $res );
456-
$this->assertIsArray( wp_remote_retrieve_header( $res, 'location' ) );
457-
$this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) );
458-
459-
$res = wp_remote_get( $url, array( 'timeout' => 30 ) );
460-
461-
$this->skipTestOnTimeout( $res );
462-
$this->assertSame( 'PASS', wp_remote_retrieve_body( $res ) );
463-
464-
}
465-
466438
/**
467439
* Test HTTP Cookie handling.
468440
*

tests/phpunit/tests/http/http.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,4 +567,89 @@ public function callback_custom_safe_ports( $ports ) {
567567
public function callback_remove_safe_ports( $ports ) {
568568
return array();
569569
}
570+
571+
/**
572+
* Test HTTP Redirects with multiple Location headers specified.
573+
*
574+
* Ensure the WP_HTTP::handle_redirects() method handles multiple Location headers
575+
* and the HTTP request it makes uses the last Location header.
576+
*
577+
* @ticket 16890
578+
* @ticket 57306
579+
*
580+
* @covers WP_HTTP::handle_redirects
581+
*/
582+
public function test_multiple_location_headers() {
583+
$pre_http_request_filter_has_run = false;
584+
// Filter the response made by WP_HTTP::handle_redirects().
585+
add_filter(
586+
'pre_http_request',
587+
function( $response, $args, $url ) use ( &$pre_http_request_filter_has_run ) {
588+
$pre_http_request_filter_has_run = true;
589+
590+
// Assert the redirect URL is correct.
591+
$this->assertSame(
592+
$url,
593+
'http://example.com/?multiple-location-headers=1&redirected=two'
594+
);
595+
596+
if ( 'http://example.com/?multiple-location-headers=1&redirected=two' === $url ) {
597+
$body = 'PASS';
598+
} else {
599+
$body = 'FAIL';
600+
}
601+
602+
return array(
603+
'headers' => array(),
604+
'body' => $body,
605+
'response' => array(
606+
'code' => 200,
607+
'message' => 'OK',
608+
),
609+
'cookies' => array(),
610+
'filename' => null,
611+
);
612+
},
613+
10,
614+
3
615+
);
616+
617+
$headers = array(
618+
'server' => 'nginx',
619+
'date' => 'Sun, 11 Dec 2022 23:11:22 GMT',
620+
'content-type' => 'text/html; charset=utf-8',
621+
'location' => array(
622+
'http://example.com/?multiple-location-headers=1&redirected=one',
623+
'http://example.com/?multiple-location-headers=1&redirected=two',
624+
),
625+
);
626+
627+
// Test the tests: ensure multiple locations are passed to WP_HTTP::handle_redirects().
628+
$this->assertIsArray( $headers['location'], 'Location header is expected to be an array.' );
629+
$this->assertCount( 2, $headers['location'], 'Location header is expected to contain two values.' );
630+
631+
$args = array(
632+
'timeout' => 30,
633+
'_redirection' => 3,
634+
'redirection' => 2,
635+
'method' => 'GET',
636+
);
637+
638+
$redirect_response = WP_HTTP::handle_redirects(
639+
'http://example.com/?multiple-location-headers=1',
640+
$args,
641+
array(
642+
'headers' => $headers,
643+
'body' => '',
644+
'cookies' => array(),
645+
'filename' => null,
646+
'response' => array(
647+
'code' => 302,
648+
'message' => 'Found',
649+
),
650+
)
651+
);
652+
$this->assertSame( 'PASS', wp_remote_retrieve_body( $redirect_response ), 'Redirect response body is expected to be PASS.' );
653+
$this->assertTrue( $pre_http_request_filter_has_run, 'The pre_http_request filter is expected to run.' );
654+
}
570655
}

0 commit comments

Comments
 (0)