Skip to content

Commit 3c51213

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.3 branch. Fixes #57306. See #56793. git-svn-id: https://develop.svn.wordpress.org/branches/4.3@54992 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0a8e2e9 commit 3c51213

File tree

2 files changed

+78
-20
lines changed

2 files changed

+78
-20
lines changed

tests/phpunit/tests/http/base.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -331,26 +331,6 @@ function test_ip_url_with_host_header() {
331331

332332
}
333333

334-
/**
335-
* Test HTTP Redirects with multiple Location headers specified
336-
*
337-
* @ticket 16890
338-
*/
339-
function test_multiple_location_headers() {
340-
$url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?multiple-location-headers=1';
341-
$res = wp_remote_head( $url, array( 'timeout' => 30 ) );
342-
343-
$this->skipTestOnTimeout( $res );
344-
$this->assertInternalType( 'array', wp_remote_retrieve_header( $res, 'location' ) );
345-
$this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) );
346-
347-
$res = wp_remote_get( $url, array( 'timeout' => 30 ) );
348-
349-
$this->skipTestOnTimeout( $res );
350-
$this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
351-
352-
}
353-
354334
/**
355335
* Test HTTP Cookie handling
356336
*

tests/phpunit/tests/http/http.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,84 @@ function parse_url_testcases() {
102102
- ://example.com - assumed path in PHP >= 5.4.7, fails in <5.4.7
103103
*/
104104
}
105+
106+
/**
107+
* Test HTTP Redirects with multiple Location headers specified.
108+
*
109+
* Ensure the WP_HTTP::handle_redirects() method handles multiple Location headers
110+
* and the HTTP request it makes uses the last Location header.
111+
*
112+
* @ticket 16890
113+
* @ticket 57306
114+
*
115+
* @covers WP_HTTP::handle_redirects
116+
*/
117+
public function test_multiple_location_headers() {
118+
$pre_http_request_filter_has_run = false;
119+
// Filter the response made by WP_HTTP::handle_redirects().
120+
add_filter(
121+
'pre_http_request',
122+
array( $this, 'filter_for_multiple_location_headers' ),
123+
10,
124+
3
125+
);
126+
127+
$headers = array(
128+
'server' => 'nginx',
129+
'date' => 'Sun, 11 Dec 2022 23:11:22 GMT',
130+
'content-type' => 'text/html; charset=utf-8',
131+
'location' => array(
132+
'http://example.com/?multiple-location-headers=1&redirected=one',
133+
'http://example.com/?multiple-location-headers=1&redirected=two',
134+
),
135+
);
136+
137+
// Test the tests: ensure multiple locations are passed to WP_HTTP::handle_redirects().
138+
$this->assertTrue( is_array( $headers['location'] ), 'Location header is expected to be an array.' );
139+
$this->assertCount( 2, $headers['location'], 'Location header is expected to contain two values.' );
140+
141+
$args = array(
142+
'timeout' => 30,
143+
'_redirection' => 3,
144+
'redirection' => 2,
145+
'method' => 'GET',
146+
);
147+
148+
$redirect_response = _wp_http_get_object()->handle_redirects(
149+
'http://example.com/?multiple-location-headers=1',
150+
$args,
151+
array(
152+
'headers' => $headers,
153+
'body' => '',
154+
'cookies' => array(),
155+
'filename' => null,
156+
'response' => array(
157+
'code' => 302,
158+
'message' => 'Found',
159+
),
160+
)
161+
);
162+
$this->assertSame( 'PASS', wp_remote_retrieve_body( $redirect_response ), 'Redirect response body is expected to be PASS.' );
163+
}
164+
165+
public function filter_for_multiple_location_headers( $response, $args, $url ) {
166+
if ( 'http://example.com/?multiple-location-headers=1&redirected=two' === $url ) {
167+
$body = 'PASS';
168+
} else {
169+
$body = 'FAIL';
170+
}
171+
172+
return array(
173+
'headers' => array(),
174+
'body' => $body,
175+
'response' => array(
176+
'code' => 200,
177+
'message' => 'OK',
178+
),
179+
'cookies' => array(),
180+
'filename' => null,
181+
);
182+
}
105183
}
106184

107185
/**

0 commit comments

Comments
 (0)