| | 1 | <?php |
| | 2 | |
| | 3 | /** |
| | 4 | * @group http |
| | 5 | */ |
| | 6 | class Tests_Get_HTTP_Headers extends WP_UnitTestCase { |
| | 7 | |
| | 8 | /** |
| | 9 | * Set up the environment |
| | 10 | */ |
| | 11 | public function setUp() { |
| | 12 | parent::setUp(); |
| | 13 | |
| | 14 | // Hook a fake HTTP request response. |
| | 15 | add_filter( 'pre_http_request', array( $this, 'fake_http_request' ), 10, 3 ); |
| | 16 | } |
| | 17 | |
| | 18 | /** |
| | 19 | * Clean up environment |
| | 20 | */ |
| | 21 | public function tearDown() { |
| | 22 | parent::tearDown(); |
| | 23 | |
| | 24 | // Clear the hook for the fake HTTP request response. |
| | 25 | remove_filter( 'pre_http_request', array( $this, 'fake_http_request' ) ); |
| | 26 | } |
| | 27 | |
| | 28 | /** |
| | 29 | * Test with a valid URL |
| | 30 | */ |
| | 31 | public function test_wp_get_http_headers_valid_url() { |
| | 32 | $result = wp_get_http_headers( 'http://example.com' ); |
| | 33 | $this->assertTrue( $result ); |
| | 34 | } |
| | 35 | |
| | 36 | /** |
| | 37 | * Test with an invalid URL |
| | 38 | */ |
| | 39 | public function test_wp_get_http_headers_invalid_url() { |
| | 40 | $result = wp_get_http_headers( 'not_an_url' ); |
| | 41 | $this->assertFalse( $result ); |
| | 42 | } |
| | 43 | |
| | 44 | /** |
| | 45 | * Test to see if the deprecated argument is working |
| | 46 | */ |
| | 47 | public function test_wp_get_http_headers_deprecated_argument() { |
| | 48 | $this->setExpectedDeprecated( 'wp_get_http_headers' ); |
| | 49 | |
| | 50 | wp_get_http_headers( 'does_not_matter', $deprecated = true ); |
| | 51 | } |
| | 52 | |
| | 53 | /** |
| | 54 | * Mock the HTTP request response |
| | 55 | * |
| | 56 | * @param bool $false False. |
| | 57 | * @param array $arguments Request arguments. |
| | 58 | * @param string $url Request URL. |
| | 59 | * |
| | 60 | * @return array|bool |
| | 61 | */ |
| | 62 | public function fake_http_request( $false, $arguments, $url ) { |
| | 63 | if ( 'http://example.com' === $url ) { |
| | 64 | return array( 'headers' => true ); |
| | 65 | } |
| | 66 | |
| | 67 | return false; |
| | 68 | } |
| | 69 | } |