Make WordPress Core

Changeset 54984 for branches/5.1


Ignore:
Timestamp:
12/15/2022 04:35:10 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 5.1 branch.
Fixes #57306.
See #56793.

Location:
branches/5.1
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.1

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

    r50094 r54984  
    395395
    396396    /**
    397      * Test HTTP Redirects with multiple Location headers specified
    398      *
    399      * @ticket 16890
    400      */
    401     function test_multiple_location_headers() {
    402         $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?multiple-location-headers=1';
    403         $res = wp_remote_head( $url, array( 'timeout' => 30 ) );
    404 
    405         $this->skipTestOnTimeout( $res );
    406         $this->assertInternalType( 'array', wp_remote_retrieve_header( $res, 'location' ) );
    407         $this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) );
    408 
    409         $res = wp_remote_get( $url, array( 'timeout' => 30 ) );
    410 
    411         $this->skipTestOnTimeout( $res );
    412         $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
    413 
    414     }
    415 
    416     /**
    417397     * Test HTTP Cookie handling
    418398     *
  • branches/5.1/tests/phpunit/tests/http/http.php

    r43571 r54984  
    373373    }
    374374
     375    /**
     376     * Test HTTP Redirects with multiple Location headers specified.
     377     *
     378     * Ensure the WP_HTTP::handle_redirects() method handles multiple Location headers
     379     * and the HTTP request it makes uses the last Location header.
     380     *
     381     * @ticket 16890
     382     * @ticket 57306
     383     *
     384     * @covers WP_HTTP::handle_redirects
     385     */
     386    public function test_multiple_location_headers() {
     387        // Filter the response made by WP_HTTP::handle_redirects().
     388        add_filter(
     389            'pre_http_request',
     390            array( $this, 'filter_for_multiple_location_headers' ),
     391            10,
     392            3
     393        );
     394
     395        $headers = array(
     396            'server'       => 'nginx',
     397            'date'         => 'Sun, 11 Dec 2022 23:11:22 GMT',
     398            'content-type' => 'text/html; charset=utf-8',
     399            'location'     => array(
     400                'http://example.com/?multiple-location-headers=1&redirected=one',
     401                'http://example.com/?multiple-location-headers=1&redirected=two',
     402            ),
     403        );
     404
     405        // Test the tests: ensure multiple locations are passed to WP_HTTP::handle_redirects().
     406        $this->assertTrue( is_array( $headers['location'] ), 'Location header is expected to be an array.' );
     407        $this->assertCount( 2, $headers['location'], 'Location header is expected to contain two values.' );
     408
     409        $args = array(
     410            'timeout'      => 30,
     411            '_redirection' => 3,
     412            'redirection'  => 2,
     413            'method'       => 'GET',
     414        );
     415
     416        $redirect_response = _wp_http_get_object()->handle_redirects(
     417            'http://example.com/?multiple-location-headers=1',
     418            $args,
     419            array(
     420                'headers'  => $headers,
     421                'body'     => '',
     422                'cookies'  => array(),
     423                'filename' => null,
     424                'response' => array(
     425                    'code'    => 302,
     426                    'message' => 'Found',
     427                ),
     428            )
     429        );
     430        $this->assertSame( 'PASS', wp_remote_retrieve_body( $redirect_response ), 'Redirect response body is expected to be PASS.' );
     431    }
     432
     433    public function filter_for_multiple_location_headers( $response, $args, $url ) {
     434        if ( 'http://example.com/?multiple-location-headers=1&redirected=two' === $url ) {
     435            $body = 'PASS';
     436        } else {
     437            $body = 'FAIL';
     438        }
     439
     440        return array(
     441            'headers'  => array(),
     442            'body'     => $body,
     443            'response' => array(
     444                'code'    => 200,
     445                'message' => 'OK',
     446            ),
     447            'cookies'  => array(),
     448            'filename' => null,
     449        );
     450    }
    375451}
Note: See TracChangeset for help on using the changeset viewer.