Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions tests/phpunit/tests/http/allowedHttpRequestHosts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

/**
* Tests for the allowed_http_request_hosts() function.
*
* @group http
*
* @covers ::allowed_http_request_hosts
*/
class Tests_HTTP_AllowedHttpRequestHosts extends WP_UnitTestCase {

/**
* Tests that a host that is already external stays external, whatever the host is.
*
* @ticket 66501
*/
public function test_should_keep_a_host_that_is_already_external() {
$this->assertTrue( allowed_http_request_hosts( true, 'unknown.example.com' ) );
$this->assertTrue( allowed_http_request_hosts( true, WP_TESTS_DOMAIN ) );
}

/**
* Tests that the host of the site is allowed.
*
* @ticket 66501
*/
public function test_should_allow_the_host_of_the_site() {
$this->assertTrue( allowed_http_request_hosts( false, WP_TESTS_DOMAIN ) );
}

/**
* Tests that a host that the site does not allow is not made external.
*
* @ticket 66501
*/
public function test_should_not_allow_other_hosts() {
$this->assertFalse( allowed_http_request_hosts( false, 'unknown.example.com' ) );
}

/**
* Tests that the hosts that are added with the `allowed_redirect_hosts` filter are allowed.
*
* @ticket 66501
*/
public function test_should_allow_the_hosts_of_the_allowed_redirect_hosts_filter() {
add_filter(
'allowed_redirect_hosts',
static function ( $hosts ) {
$hosts[] = 'allowed-by-filter.example.com';

return $hosts;
}
);

$this->assertTrue( allowed_http_request_hosts( false, 'allowed-by-filter.example.com' ), 'The host of the filter should be allowed.' );
$this->assertFalse( allowed_http_request_hosts( false, 'not-in-the-filter.example.com' ), 'A host that is not in the filter should not be allowed.' );
}

/**
* Tests that the function is used by default for the requests of the HTTP API.
*
* @ticket 66501
*/
public function test_should_be_hooked_to_the_filter_of_external_hosts() {
$this->assertSame( 10, has_filter( 'http_request_host_is_external', 'allowed_http_request_hosts' ) );
}
}
98 changes: 98 additions & 0 deletions tests/phpunit/tests/http/msAllowedHttpRequestHosts.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

/**
* Tests for the ms_allowed_http_request_hosts() function.
*
* @group http
* @group ms-required
*
* @covers ::ms_allowed_http_request_hosts
*/
class Tests_HTTP_MsAllowedHttpRequestHosts extends WP_UnitTestCase {

/**
* Tests that a host that is already external stays external, without a query.
*
* @ticket 66501
*/
public function test_should_keep_a_host_that_is_already_external_without_a_query() {
global $wpdb;

$queries = $wpdb->num_queries;

$this->assertTrue( ms_allowed_http_request_hosts( true, 'ms-external.example.com' ) );
$this->assertSame( $queries, $wpdb->num_queries, 'The database should not be queried.' );
}

/**
* Tests that the domain of the network is allowed, without a query.
*
* @ticket 66501
*/
public function test_should_allow_the_domain_of_the_network_without_a_query() {
global $wpdb;

$queries = $wpdb->num_queries;

$this->assertTrue( ms_allowed_http_request_hosts( false, get_network()->domain ) );
$this->assertSame( $queries, $wpdb->num_queries, 'The database should not be queried.' );
}

/**
* Tests that the domain of a site of the network is allowed.
*
* @ticket 66501
*/
public function test_should_allow_the_domain_of_a_site_of_the_network() {
self::factory()->blog->create(
array(
'domain' => 'ms-allowed-site.example.net',
'path' => '/',
)
);

$this->assertTrue( ms_allowed_http_request_hosts( false, 'ms-allowed-site.example.net' ) );
}

/**
* Tests that a domain that is not one of the network or of its sites is not allowed.
*
* @ticket 66501
*/
public function test_should_not_allow_other_hosts() {
$this->assertFalse( ms_allowed_http_request_hosts( false, 'ms-unknown-host.example.net' ) );
}

/**
* Tests that the database is queried only once for the same host.
*
* @ticket 66501
*/
public function test_should_query_the_database_once_for_the_same_host() {
$queries = 0;
add_filter(
'query',
static function ( $query ) use ( &$queries ) {
if ( str_contains( $query, 'ms-cached-host.example.net' ) ) {
++$queries;
}

return $query;
}
);

$this->assertFalse( ms_allowed_http_request_hosts( false, 'ms-cached-host.example.net' ), 'The first call should not allow the host.' );
$this->assertFalse( ms_allowed_http_request_hosts( false, 'ms-cached-host.example.net' ), 'The second call should not allow the host.' );

$this->assertSame( 1, $queries, 'The host should be queried once.' );
}

/**
* Tests that the function is used by default for the requests of the HTTP API in a network.
*
* @ticket 66501
*/
public function test_should_be_hooked_to_the_filter_of_external_hosts() {
$this->assertSame( 20, has_filter( 'http_request_host_is_external', 'ms_allowed_http_request_hosts' ) );
}
}
131 changes: 131 additions & 0 deletions tests/phpunit/tests/http/wpRemoteRetrieveHeader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

/**
* Tests for the wp_remote_retrieve_header() function.
*
* @group http
*
* @covers ::wp_remote_retrieve_header
*/
class Tests_HTTP_WpRemoteRetrieveHeader extends WP_UnitTestCase {

/**
* Tests that the value of a header is returned.
*
* @ticket 66501
*/
public function test_should_return_the_value_of_the_header() {
$response = array(
'headers' => array(
'content-type' => 'text/html',
'x-custom' => 'custom value',
),
);

$this->assertSame( 'text/html', wp_remote_retrieve_header( $response, 'content-type' ) );
$this->assertSame( 'custom value', wp_remote_retrieve_header( $response, 'x-custom' ) );
}

/**
* Tests that the values of the headers that are repeated are returned as they are.
*
* @ticket 66501
*/
public function test_should_return_the_value_of_a_header_that_is_repeated() {
$response = array(
'headers' => array(
'set-cookie' => array( 'a=1', 'b=2' ),
),
);

$this->assertSame( array( 'a=1', 'b=2' ), wp_remote_retrieve_header( $response, 'set-cookie' ) );
}

/**
* Tests that the headers of the HTTP API are looked up without taking the case into account.
*
* @ticket 66501
*/
public function test_should_find_the_header_of_a_case_insensitive_dictionary_in_any_case() {
$response = array(
'headers' => new WpOrg\Requests\Utility\CaseInsensitiveDictionary( array( 'Content-Type' => 'application/json' ) ),
);

$this->assertSame( 'application/json', wp_remote_retrieve_header( $response, 'content-type' ), 'The lowercase name should be found.' );
$this->assertSame( 'application/json', wp_remote_retrieve_header( $response, 'Content-Type' ), 'The original name should be found.' );
$this->assertSame( 'application/json', wp_remote_retrieve_header( $response, 'CONTENT-TYPE' ), 'The uppercase name should be found.' );
}

/**
* Tests that the name of a header of an array is case sensitive.
*
* @ticket 66501
*/
public function test_should_not_ignore_the_case_of_the_header_of_an_array() {
$response = array(
'headers' => array( 'content-type' => 'text/html' ),
);

$this->assertSame( '', wp_remote_retrieve_header( $response, 'Content-Type' ) );
}

/**
* Tests that an empty string is returned when the header is not set.
*
* @ticket 66501
*/
public function test_should_return_an_empty_string_when_the_header_is_not_set() {
$response = array(
'headers' => array( 'content-type' => 'text/html' ),
);

$this->assertSame( '', wp_remote_retrieve_header( $response, 'x-missing' ) );
}

/**
* Tests that the value of a header is returned even when it is empty or falsy.
*
* @ticket 66501
*
* @dataProvider data_falsy_values
*
* @param mixed $value Value of the header.
*/
public function test_should_return_the_value_of_a_header_that_is_falsy( $value ) {
$response = array(
'headers' => array( 'x-falsy' => $value ),
);

$this->assertSame( $value, wp_remote_retrieve_header( $response, 'x-falsy' ) );
}

/**
* Data provider.
*
* @return array[]
*/
public static function data_falsy_values() {
return array(
'zero as a string' => array( '0' ),
'empty string' => array( '' ),
);
}

/**
* Tests that an empty string is returned when the response is an error.
*
* @ticket 66501
*/
public function test_should_return_an_empty_string_for_an_error() {
$this->assertSame( '', wp_remote_retrieve_header( new WP_Error( 'http_request_failed', 'Failed' ), 'content-type' ) );
}

/**
* Tests that an empty string is returned when the response has no headers.
*
* @ticket 66501
*/
public function test_should_return_an_empty_string_when_the_response_has_no_headers() {
$this->assertSame( '', wp_remote_retrieve_header( array( 'body' => 'text' ), 'content-type' ) );
}
}
Loading
Loading