Make WordPress Core


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

Location:
branches/4.6
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.6

  • branches/4.6/tests/phpunit/tests/http/http.php

    r38461 r54989  
    150150                }
    151151        }
     152
     153        /**
     154         * Test HTTP Redirects with multiple Location headers specified.
     155         *
     156         * Ensure the WP_HTTP::handle_redirects() method handles multiple Location headers
     157         * and the HTTP request it makes uses the last Location header.
     158         *
     159         * @ticket 16890
     160         * @ticket 57306
     161         *
     162         * @covers WP_HTTP::handle_redirects
     163         */
     164        public function test_multiple_location_headers() {
     165                $pre_http_request_filter_has_run = false;
     166                // Filter the response made by WP_HTTP::handle_redirects().
     167                add_filter(
     168                        'pre_http_request',
     169                        array( $this, 'filter_for_multiple_location_headers' ),
     170                        10,
     171                        3
     172                );
     173
     174                $headers = array(
     175                        'server'       => 'nginx',
     176                        'date'         => 'Sun, 11 Dec 2022 23:11:22 GMT',
     177                        'content-type' => 'text/html; charset=utf-8',
     178                        'location'     => array(
     179                                'http://example.com/?multiple-location-headers=1&redirected=one',
     180                                'http://example.com/?multiple-location-headers=1&redirected=two',
     181                        ),
     182                );
     183
     184                // Test the tests: ensure multiple locations are passed to WP_HTTP::handle_redirects().
     185                $this->assertTrue( is_array( $headers['location'] ), 'Location header is expected to be an array.' );
     186                $this->assertCount( 2, $headers['location'], 'Location header is expected to contain two values.' );
     187
     188                $args = array(
     189                        'timeout'      => 30,
     190                        '_redirection' => 3,
     191                        'redirection'  => 2,
     192                        'method'       => 'GET',
     193                );
     194
     195                $redirect_response = _wp_http_get_object()->handle_redirects(
     196                        'http://example.com/?multiple-location-headers=1',
     197                        $args,
     198                        array(
     199                                'headers'  => $headers,
     200                                'body'     => '',
     201                                'cookies'  => array(),
     202                                'filename' => null,
     203                                'response' => array(
     204                                        'code'    => 302,
     205                                        'message' => 'Found',
     206                                ),
     207                        )
     208                );
     209                $this->assertSame( 'PASS', wp_remote_retrieve_body( $redirect_response ), 'Redirect response body is expected to be PASS.' );
     210        }
     211       
     212        public function filter_for_multiple_location_headers( $response, $args, $url ) {
     213                if ( 'http://example.com/?multiple-location-headers=1&redirected=two' === $url ) {
     214                        $body = 'PASS';
     215                } else {
     216                        $body = 'FAIL';
     217                }
     218
     219                return array(
     220                        'headers'  => array(),
     221                        'body'     => $body,
     222                        'response' => array(
     223                                'code'    => 200,
     224                                'message' => 'OK',
     225                        ),
     226                        'cookies'  => array(),
     227                        'filename' => null,
     228                );             
     229        }
    152230}
Note: See TracChangeset for help on using the changeset viewer.