Make WordPress Core


Ignore:
Timestamp:
03/04/2018 05:13:35 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Filesystem API: Allow download_url() to return the response code and body on error as an additional WP_Error object data.

The error response body size is limited to 1 KB by default to avoid taking up too much memory. The size can be increased using download_url_error_max_body_size filter.

Props soulseekah, campusboy1987, mihdan, SergeyBiryukov.
Fixes #43329.

File:
1 edited

Legend:

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

    r42343 r42773  
    3232        $_SERVER['SCRIPT_FILENAME'] = $sfn;
    3333    }
     34
     35    /**
     36     * @ticket 43329
     37     */
     38    public function test_download_url_non_200_response_code() {
     39        add_filter( 'pre_http_request', array( $this, '_fake_download_url_non_200_response_code' ), 10, 3 );
     40
     41        $error = download_url( 'test_download_url_non_200' );
     42        $this->assertWPError( $error );
     43        $this->assertEquals( array(
     44            'code' => 418,
     45            'body' => 'This is an unexpected error message from your favorite server.',
     46        ), $error->get_error_data() );
     47
     48        add_filter( 'download_url_error_max_body_size', array( $this, '__return_5' ) );
     49
     50        $error = download_url( 'test_download_url_non_200' );
     51        $this->assertWPError( $error );
     52        $this->assertEquals( array(
     53            'code' => 418,
     54            'body' => 'This ',
     55        ), $error->get_error_data() );
     56
     57        remove_filter( 'download_url_error_max_body_size', array( $this, '__return_5' ) );
     58        remove_filter( 'pre_http_request', array( $this, '_fake_download_url_non_200_response_code' ) );
     59    }
     60
     61    public function _fake_download_url_non_200_response_code( $response, $args, $url ) {
     62        file_put_contents( $args['filename'], 'This is an unexpected error message from your favorite server.' );
     63        return array(
     64            'response' => array(
     65                'code'    => 418,
     66                'message' => "I'm a teapot!",
     67            ),
     68        );
     69    }
     70
     71    public function __return_5() {
     72        return 5;
     73    }
    3474}
Note: See TracChangeset for help on using the changeset viewer.