Skip to content

Commit 9fe93c8

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 4.7 branch. Fixes #57306. See #56793. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@54988 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7a84b80 commit 9fe93c8

File tree

2 files changed

+77
-20
lines changed

2 files changed

+77
-20
lines changed

tests/phpunit/tests/http/base.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -355,26 +355,6 @@ function test_https_url_without_ssl_verification() {
355355
$this->assertNotWPError( $res );
356356
}
357357

358-
/**
359-
* Test HTTP Redirects with multiple Location headers specified
360-
*
361-
* @ticket 16890
362-
*/
363-
function test_multiple_location_headers() {
364-
$url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?multiple-location-headers=1';
365-
$res = wp_remote_head( $url, array( 'timeout' => 30 ) );
366-
367-
$this->skipTestOnTimeout( $res );
368-
$this->assertInternalType( 'array', wp_remote_retrieve_header( $res, 'location' ) );
369-
$this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) );
370-
371-
$res = wp_remote_get( $url, array( 'timeout' => 30 ) );
372-
373-
$this->skipTestOnTimeout( $res );
374-
$this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
375-
376-
}
377-
378358
/**
379359
* Test HTTP Cookie handling
380360
*

tests/phpunit/tests/http/http.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,81 @@ function wp_translate_php_url_constant_to_key_testcases() {
314314
);
315315
}
316316

317+
/**
318+
* Test HTTP Redirects with multiple Location headers specified.
319+
*
320+
* Ensure the WP_HTTP::handle_redirects() method handles multiple Location headers
321+
* and the HTTP request it makes uses the last Location header.
322+
*
323+
* @ticket 16890
324+
* @ticket 57306
325+
*
326+
* @covers WP_HTTP::handle_redirects
327+
*/
328+
public function test_multiple_location_headers() {
329+
$pre_http_request_filter_has_run = false;
330+
// Filter the response made by WP_HTTP::handle_redirects().
331+
add_filter(
332+
'pre_http_request',
333+
array( $this, 'filter_for_multiple_location_headers' ),
334+
10,
335+
3
336+
);
337+
338+
$headers = array(
339+
'server' => 'nginx',
340+
'date' => 'Sun, 11 Dec 2022 23:11:22 GMT',
341+
'content-type' => 'text/html; charset=utf-8',
342+
'location' => array(
343+
'http://example.com/?multiple-location-headers=1&redirected=one',
344+
'http://example.com/?multiple-location-headers=1&redirected=two',
345+
),
346+
);
347+
348+
// Test the tests: ensure multiple locations are passed to WP_HTTP::handle_redirects().
349+
$this->assertTrue( is_array( $headers['location'] ), 'Location header is expected to be an array.' );
350+
$this->assertCount( 2, $headers['location'], 'Location header is expected to contain two values.' );
351+
352+
$args = array(
353+
'timeout' => 30,
354+
'_redirection' => 3,
355+
'redirection' => 2,
356+
'method' => 'GET',
357+
);
358+
359+
$redirect_response = _wp_http_get_object()->handle_redirects(
360+
'http://example.com/?multiple-location-headers=1',
361+
$args,
362+
array(
363+
'headers' => $headers,
364+
'body' => '',
365+
'cookies' => array(),
366+
'filename' => null,
367+
'response' => array(
368+
'code' => 302,
369+
'message' => 'Found',
370+
),
371+
)
372+
);
373+
$this->assertSame( 'PASS', wp_remote_retrieve_body( $redirect_response ), 'Redirect response body is expected to be PASS.' );
374+
}
375+
376+
public function filter_for_multiple_location_headers( $response, $args, $url ) {
377+
if ( 'http://example.com/?multiple-location-headers=1&redirected=two' === $url ) {
378+
$body = 'PASS';
379+
} else {
380+
$body = 'FAIL';
381+
}
382+
383+
return array(
384+
'headers' => array(),
385+
'body' => $body,
386+
'response' => array(
387+
'code' => 200,
388+
'message' => 'OK',
389+
),
390+
'cookies' => array(),
391+
'filename' => null,
392+
);
393+
}
317394
}

0 commit comments

Comments
 (0)