Skip to content

Commit 03eb35c

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.0 branch. Fixes #57306. See #56793. git-svn-id: https://develop.svn.wordpress.org/branches/5.0@54985 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 034080b commit 03eb35c

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
@@ -309,4 +309,81 @@ function wp_translate_php_url_constant_to_key_testcases() {
309309
);
310310
}
311311

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

0 commit comments

Comments
 (0)