Make WordPress Core

Changeset 54979


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

Location:
branches/5.6
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/5.6

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

    r49184 r54979  
    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->assertSame( 'PASS', wp_remote_retrieve_body( $res ) );
    413 
    414         }
    415 
    416         /**
    417397         * Test HTTP Cookie handling.
    418398         *
  • branches/5.6/tests/phpunit/tests/http/http.php

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