HTTP: Add unit tests for `wp_get_http_headers()` and `wp_remote_retrieve_headers()`.

Props borgesbruno, jipmoors.
Fixes #37090.

git-svn-id: https://develop.svn.wordpress.org/trunk@37907 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dominik Schilling (ocean90) 2016-06-29 12:46:59 +00:00
parent faa1a087e3
commit 970b7c43ab
2 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,69 @@
<?php
/**
* @group http
*/
class Tests_HTTP_GetHttpHeaders extends WP_UnitTestCase {
/**
* Set up the environment
*/
public function setUp() {
parent::setUp();
// Hook a fake HTTP request response.
add_filter( 'pre_http_request', array( $this, 'fake_http_request' ), 10, 3 );
}
/**
* Clean up environment
*/
public function tearDown() {
parent::tearDown();
// Clear the hook for the fake HTTP request response.
remove_filter( 'pre_http_request', array( $this, 'fake_http_request' ) );
}
/**
* Test with a valid URL
*/
public function test_wp_get_http_headers_valid_url() {
$result = wp_get_http_headers( 'http://example.com' );
$this->assertTrue( $result );
}
/**
* Test with an invalid URL
*/
public function test_wp_get_http_headers_invalid_url() {
$result = wp_get_http_headers( 'not_an_url' );
$this->assertFalse( $result );
}
/**
* Test to see if the deprecated argument is working
*/
public function test_wp_get_http_headers_deprecated_argument() {
$this->setExpectedDeprecated( 'wp_get_http_headers' );
wp_get_http_headers( 'does_not_matter', $deprecated = true );
}
/**
* Mock the HTTP request response
*
* @param bool $false False.
* @param array $arguments Request arguments.
* @param string $url Request URL.
*
* @return array|bool
*/
public function fake_http_request( $false, $arguments, $url ) {
if ( 'http://example.com' === $url ) {
return array( 'headers' => true );
}
return false;
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
* @group http
*/
class Tests_HTTP_RemoteRetrieveHeaders extends WP_UnitTestCase {
/**
* Valid response
*/
function test_remote_retrieve_headers_valid_response() {
$headers = 'headers_data';
$response = array( 'headers' => $headers );
$result = wp_remote_retrieve_headers( $response );
$this->assertEquals( $headers, $result );
}
/**
* Response is a WP_Error
*/
function test_remote_retrieve_headers_is_error() {
$response = new WP_Error( 'Some error' );
$result = wp_remote_retrieve_headers( $response );
$this->assertEquals( array(), $result );
}
/**
* Response does not contain 'headers'
*/
function test_remote_retrieve_headers_invalid_response() {
$response = array( 'no_headers' => 'set');
$result = wp_remote_retrieve_headers( $response );
$this->assertEquals( array(), $result );
}
}