Make WordPress Core

Changeset 64096


Ignore:
Timestamp:
10/05/2026 04:37:40 PM (11 days ago)
Author:
lancewillett
Message:

Tests: Remove live requests from HTTP function tests.

Use a test-only Requests transport to return canned HTTP responses while preserving real cookie normalization, redirect handling, and response conversion. Retain direct HEAD and GET coverage and move Tests_HTTP_Functions out of the external-http group.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/13951

Props shailu25, adrianmoldovanwp.
See #63914.

Location:
trunk/tests/phpunit
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/http/functions.php

    r62621 r64096  
    11<?php
     2
     3require_once DIR_TESTDATA . '/../includes/class-wp-http-unit-test-transport.php';
    24
    35/**
    46 * @group http
    5  * @group external-http
     7 *
     8 * @ticket 63914
    69 */
    710class Tests_HTTP_Functions extends WP_UnitTestCase {
    811
    912        /**
     13         * Whether the current test is using the fake Requests transport.
     14         *
     15         * @var bool
     16         */
     17        private $using_mock_transport = false;
     18
     19        /**
     20         * Tear down the fake transport hook when used.
     21         */
     22        public function tear_down() {
     23                if ( $this->using_mock_transport ) {
     24                        remove_action( 'requests-requests.before_request', array( $this, 'inject_mock_transport' ), 10 );
     25                        $this->using_mock_transport = false;
     26                }
     27
     28                parent::tear_down();
     29        }
     30
     31        /**
     32         * Enables the fake Requests transport for tests that must exercise
     33         * WP_Http request handling without live network access.
     34         *
     35         * Removes the core external-HTTP blocker so the request proceeds past
     36         * `pre_http_request` into cookie normalization, transport execution, and
     37         * response conversion.
     38         */
     39        private function use_mock_transport() {
     40                remove_filter( 'pre_http_request', array( $this, 'block_external_http_request' ), PHP_INT_MAX );
     41                add_action( 'requests-requests.before_request', array( $this, 'inject_mock_transport' ), 10, 5 );
     42                $this->using_mock_transport = true;
     43        }
     44
     45        /**
     46         * Injects the fake Requests transport into request options.
     47         *
     48         * @param string       $url     Request URL.
     49         * @param array        $headers Request headers.
     50         * @param string|array $data    Request data.
     51         * @param string       $type    HTTP method.
     52         * @param array        $options Request options (passed by reference).
     53         */
     54        public function inject_mock_transport( $url, $headers, $data, $type, &$options ) {
     55                $options['transport'] = new WP_Http_Unit_Test_Transport();
     56        }
     57
     58        /**
    1059         * @covers ::wp_remote_head
     60         * @covers ::wp_remote_retrieve_headers
     61         * @covers ::wp_remote_retrieve_response_code
    1162         */
    1263        public function test_head_request() {
     64                $this->use_mock_transport();
     65
    1366                // This URL gives a direct 200 response.
    1467                $url      = 'https://s.w.org/screenshots/3.9/dashboard.png';
    15                 $response = $this->wp_remote_head( $url );
     68                $response = wp_remote_head( $url );
    1669
    1770                $this->assertNotWPError( $response );
    … …  
    2982         */
    3083        public function test_head_redirect() {
    31                 // This URL will 301 redirect.
     84                $this->use_mock_transport();
     85
     86                // This URL will 301 redirect. HEAD requests do not follow redirects by default.
    3287                $url      = 'https://wp.org/screenshots/3.9/dashboard.png';
    33                 $response = $this->wp_remote_head( $url );
     88                $response = wp_remote_head( $url );
    3489
    3590                $this->assertNotWPError( $response );
    … …  
    3994        /**
    4095         * @covers ::wp_remote_head
     96         * @covers ::wp_remote_retrieve_response_code
    4197         */
    4298        public function test_head_404() {
     99                $this->use_mock_transport();
     100
    43101                $url      = 'https://wordpress.org/screenshots/3.9/awefasdfawef.jpg';
    44                 $response = $this->wp_remote_head( $url );
     102                $response = wp_remote_head( $url );
    45103
    46104                $this->assertNotWPError( $response );
    … …  
    54112         */
    55113        public function test_get_request() {
     114                $this->use_mock_transport();
     115
    56116                $url = 'https://s.w.org/screenshots/3.9/dashboard.png';
    57117
    58                 $response = $this->wp_remote_get( $url );
     118                $response = wp_remote_get( $url );
    59119
    60120                $this->assertNotWPError( $response );
    … …  
    74134         */
    75135        public function test_get_redirect() {
     136                $this->use_mock_transport();
     137
    76138                // This will redirect to wordpress.org.
    77139                $url = 'https://wp.org/screenshots/3.9/dashboard.png';
    78140
    79                 $response = $this->wp_remote_get( $url );
     141                $response = wp_remote_get( $url );
    80142
    81143                $this->assertNotWPError( $response );
    … …  
    83145                $headers = wp_remote_retrieve_headers( $response );
    84146
    85                 // Should return the same headers as a HEAD request.
     147                // GET follows the redirect and returns the final image response.
    86148                $this->assertSame( 200, wp_remote_retrieve_response_code( $response ) );
    87149                $this->assertSame( 'image/png', $headers['Content-Type'] );
    … …  
    93155         */
    94156        public function test_get_redirect_limit_exceeded() {
     157                $this->use_mock_transport();
     158
    95159                // This will redirect to wordpress.org.
    96160                $url = 'https://wp.org/screenshots/3.9/dashboard.png';
    97161
    98162                // Pretend we've already redirected 5 times.
    99                 $response = $this->wp_remote_get( $url, array( 'redirection' => -1 ) );
     163                $response = wp_remote_get( $url, array( 'redirection' => -1 ) );
    100164
    101165                $this->assertWPError( $response );
     166                $this->assertSame( 'http_request_failed', $response->get_error_code() );
    102167        }
    103168
    … …  
    109174         * @covers ::wp_remote_retrieve_cookie
    110175         * @covers ::wp_remote_retrieve_cookie_value
     176         * @covers WP_HTTP_Requests_Response::get_cookies
    111177         */
    112178        public function test_get_response_cookies() {
     179                $this->use_mock_transport();
     180
    113181                $url = 'https://login.wordpress.org/wp-login.php';
    114182
    115                 $response = $this->wp_remote_head( $url );
     183                $response = wp_remote_head( $url );
    116184
    117185                $this->assertNotWPError( $response );
    … …  
    142210         * @covers ::wp_remote_retrieve_cookies
    143211         * @covers ::wp_remote_retrieve_cookie
     212         * @covers WP_Http::normalize_cookies
    144213         */
    145214        public function test_get_response_cookies_with_wp_http_cookie_object() {
     215                $this->use_mock_transport();
     216
    146217                $url = 'https://login.wordpress.org/wp-login.php';
    147218
    148                 $response = $this->wp_remote_get(
     219                $response = wp_remote_get(
    149220                        $url,
    150221                        array(
    … …  
    178249         * @covers ::wp_remote_retrieve_cookies
    179250         * @covers ::wp_remote_retrieve_cookie
     251         * @covers WP_Http::normalize_cookies
    180252         */
    181253        public function test_get_response_cookies_with_name_value_array() {
     254                $this->use_mock_transport();
     255
    182256                $url = 'https://login.wordpress.org/wp-login.php';
    183257
    184                 $response = $this->wp_remote_get(
     258                $response = wp_remote_get(
    185259                        $url,
    186260                        array(
Note: See TracChangeset for help on using the changeset viewer.