Make WordPress Core

Changeset 52382


Ignore:
Timestamp:
12/15/2021 07:59:32 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: Mock the HTTP request response in download_url() tests.

This aims to speed up the tests and minimize unrelated failures by avoiding an unnecessary external HTTP request, while still performing the intended functionality checks.

Update similar helpers in some other tests to use more consistent terminology.

Follow-up to [37907], [46175], [51626].

See #54420, #53363.

Location:
trunk/tests/phpunit/tests
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesFile.php

    r52242 r52382  
    292292     */
    293293    public function test_download_url_no_warning_for_url_without_path() {
     294        // Hook a mocked HTTP request response.
     295        add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
     296
    294297        $result = download_url( 'https://example.com' );
    295298
     
    307310     */
    308311    public function test_download_url_no_warning_for_url_without_path_with_signature_verification() {
     312        // Hook a mocked HTTP request response.
     313        add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
     314
    309315        add_filter(
    310316            'wp_signature_hosts',
     
    327333        $this->assertSame( 'signature_verification_no_signature', $error->get_error_code() );
    328334    }
     335
     336    /**
     337     * Mock the HTTP request response.
     338     *
     339     * @param bool   $false     False.
     340     * @param array  $arguments Request arguments.
     341     * @param string $url       Request URL.
     342     * @return array|bool
     343     */
     344    public function mock_http_request( $false, $arguments, $url ) {
     345        if ( 'https://example.com' === $url ) {
     346            return array(
     347                'response' => array(
     348                    'code' => 200,
     349                ),
     350            );
     351        }
     352
     353        return $false;
     354    }
    329355}
  • trunk/tests/phpunit/tests/functions/doEnclose.php

    r51627 r52382  
    2626    public function set_up() {
    2727        parent::set_up();
    28         add_filter( 'pre_http_request', array( $this, 'fake_http_request' ), 10, 3 );
     28        add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
    2929    }
    3030
     
    251251
    252252    /**
    253      * Fake the HTTP request response.
     253     * Mock the HTTP request response.
    254254     *
    255255     * @since 5.3.0
     
    258258     * @param array  $arguments Request arguments.
    259259     * @param string $url       Request URL.
    260      *
    261260     * @return array            Header.
    262261     */
    263     public function fake_http_request( $false, $arguments, $url ) {
     262    public function mock_http_request( $false, $arguments, $url ) {
    264263
    265264        // Video and audio headers.
  • trunk/tests/phpunit/tests/http/wpGetHttpHeaders.php

    r51568 r52382  
    1313        parent::set_up();
    1414
    15         // Hook a fake HTTP request response.
    16         add_filter( 'pre_http_request', array( $this, 'fake_http_request' ), 10, 3 );
     15        // Hook a mocked HTTP request response.
     16        add_filter( 'pre_http_request', array( $this, 'mock_http_request' ), 10, 3 );
    1717    }
    1818
     
    4848     * @param array  $arguments Request arguments.
    4949     * @param string $url       Request URL.
    50      *
    5150     * @return array|bool
    5251     */
    53     public function fake_http_request( $false, $arguments, $url ) {
     52    public function mock_http_request( $false, $arguments, $url ) {
    5453        if ( 'http://example.com' === $url ) {
    5554            return array( 'headers' => true );
Note: See TracChangeset for help on using the changeset viewer.