Make WordPress Core

Changeset 54990 for branches/4.5


Ignore:
Timestamp:
12/15/2022 04:48:15 AM (12 months 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.5 branch.
Fixes #57306.
See #56793.

Location:
branches/4.5
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.5

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

    r50090 r54990  
    362362
    363363    /**
    364      * Test HTTP Redirects with multiple Location headers specified
    365      *
    366      * @ticket 16890
    367      */
    368     function test_multiple_location_headers() {
    369         $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?multiple-location-headers=1';
    370         $res = wp_remote_head( $url, array( 'timeout' => 30 ) );
    371 
    372         $this->skipTestOnTimeout( $res );
    373         $this->assertInternalType( 'array', wp_remote_retrieve_header( $res, 'location' ) );
    374         $this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) );
    375 
    376         $res = wp_remote_get( $url, array( 'timeout' => 30 ) );
    377 
    378         $this->skipTestOnTimeout( $res );
    379         $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
    380 
    381     }
    382 
    383     /**
    384364     * Test HTTP Cookie handling
    385365     *
  • branches/4.5/tests/phpunit/tests/http/http.php

    r36749 r54990  
    119119
    120120    }
     121
     122    /**
     123     * Test HTTP Redirects with multiple Location headers specified.
     124     *
     125     * Ensure the WP_HTTP::handle_redirects() method handles multiple Location headers
     126     * and the HTTP request it makes uses the last Location header.
     127     *
     128     * @ticket 16890
     129     * @ticket 57306
     130     *
     131     * @covers WP_HTTP::handle_redirects
     132     */
     133    public function test_multiple_location_headers() {
     134        $pre_http_request_filter_has_run = false;
     135        // Filter the response made by WP_HTTP::handle_redirects().
     136        add_filter(
     137            'pre_http_request',
     138            array( $this, 'filter_for_multiple_location_headers' ),
     139            10,
     140            3
     141        );
     142
     143        $headers = array(
     144            'server'       => 'nginx',
     145            'date'         => 'Sun, 11 Dec 2022 23:11:22 GMT',
     146            'content-type' => 'text/html; charset=utf-8',
     147            'location'     => array(
     148                'http://example.com/?multiple-location-headers=1&redirected=one',
     149                'http://example.com/?multiple-location-headers=1&redirected=two',
     150            ),
     151        );
     152
     153        // Test the tests: ensure multiple locations are passed to WP_HTTP::handle_redirects().
     154        $this->assertTrue( is_array( $headers['location'] ), 'Location header is expected to be an array.' );
     155        $this->assertCount( 2, $headers['location'], 'Location header is expected to contain two values.' );
     156
     157        $args = array(
     158            'timeout'      => 30,
     159            '_redirection' => 3,
     160            'redirection'  => 2,
     161            'method'       => 'GET',
     162        );
     163
     164        $redirect_response = _wp_http_get_object()->handle_redirects(
     165            'http://example.com/?multiple-location-headers=1',
     166            $args,
     167            array(
     168                'headers'  => $headers,
     169                'body'     => '',
     170                'cookies'  => array(),
     171                'filename' => null,
     172                'response' => array(
     173                    'code'    => 302,
     174                    'message' => 'Found',
     175                ),
     176            )
     177        );
     178        $this->assertSame( 'PASS', wp_remote_retrieve_body( $redirect_response ), 'Redirect response body is expected to be PASS.' );
     179    }
     180   
     181    public function filter_for_multiple_location_headers( $response, $args, $url ) {
     182        if ( 'http://example.com/?multiple-location-headers=1&redirected=two' === $url ) {
     183            $body = 'PASS';
     184        } else {
     185            $body = 'FAIL';
     186        }
     187
     188        return array(
     189            'headers'  => array(),
     190            'body'     => $body,
     191            'response' => array(
     192                'code'    => 200,
     193                'message' => 'OK',
     194            ),
     195            'cookies'  => array(),
     196            'filename' => null,
     197        );     
     198    }
    121199}
Note: See TracChangeset for help on using the changeset viewer.