Make WordPress Core

Changeset 54986 for branches/4.9


Ignore:
Timestamp:
12/15/2022 04:42:04 AM (2 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:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

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

    r50096 r54986  
    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.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.