Make WordPress Core

Changeset 54987


Ignore:
Timestamp:
12/15/2022 04:43:39 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.8 branch.
Fixes #57306.
See #56793.

Location:
branches/4.8
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.8

  • branches/4.8/tests/phpunit/tests/http/base.php

    r50087 r54987  
    357357
    358358        /**
    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 
    378         /**
    379359         * Test HTTP Cookie handling
    380360         *
  • branches/4.8/tests/phpunit/tests/http/http.php

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