Make WordPress Core


Ignore:
Timestamp:
12/15/2022 04:42:04 AM (4 years ago)
Author:
peterwilsoncc
Message:

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.9 branch.
Fixes #57306.
See #56793.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/tests/phpunit/tests/http/http.php

    r41367 r54986  
    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}
Note: See TracChangeset for help on using the changeset viewer.