Skip to content
Permalink
Browse files

Tests: Introduce `Tests_HTTP_Functions::skipTestOnTimeout()`, mirrori…

…ng the same `WP_HTTP_UnitTestCase` method.

Merges [43512] to the 4.9 branch.
Fixes #44613.

git-svn-id: https://develop.svn.wordpress.org/branches/4.9@43626 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information...
SergeyBiryukov committed Sep 3, 2018
1 parent 9e8090f commit 4c6175e800d92e6e96086aa55d296689c1b931e4
Showing with 52 additions and 7 deletions.
  1. +1 −1 tests/phpunit/tests/http/base.php
  2. +51 −6 tests/phpunit/tests/http/functions.php
@@ -18,7 +18,7 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
protected $http_request_args;
/**
* Mark test as skipped if the HTTP request times out
* Mark test as skipped if the HTTP request times out.
*/
function skipTestOnTimeout( $response ) {
if( ! is_wp_error( $response ) ){
@@ -5,6 +5,28 @@
* @group external-http
*/
class Tests_HTTP_Functions extends WP_UnitTestCase {
/**
* Mark test as skipped if the HTTP request times out.
*/
function skipTestOnTimeout( $response ) {
if ( ! is_wp_error( $response ) ) {
return;
}
if ( 'connect() timed out!' === $response->get_error_message() ) {
$this->markTestSkipped( 'HTTP timeout' );
}
if ( false !== strpos( $response->get_error_message(), 'timed out after' ) ) {
$this->markTestSkipped( 'HTTP timeout' );
}
if ( 0 === strpos( $response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80' ) ) {
$this->markTestSkipped( 'HTTP timeout' );
}
}
public function setUp() {
if ( ! extension_loaded( 'openssl' ) ) {
$this->markTestSkipped( 'Tests_HTTP_Functions requires openssl.' );
@@ -17,6 +39,9 @@ function test_head_request() {
// this url give a direct 200 response
$url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
$response = wp_remote_head( $url );
$this->skipTestOnTimeout( $response );
$headers = wp_remote_retrieve_headers( $response );
$this->assertInternalType( 'array', $response );
@@ -30,20 +55,26 @@ function test_head_redirect() {
// this url will 301 redirect
$url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
$response = wp_remote_head( $url );
$this->skipTestOnTimeout( $response );
$this->assertEquals( '301', wp_remote_retrieve_response_code( $response ) );
}
function test_head_404() {
$url = 'https://asdftestblog1.files.wordpress.com/2007/09/awefasdfawef.jpg';
$headers = wp_remote_head( $url );
$url = 'https://asdftestblog1.files.wordpress.com/2007/09/awefasdfawef.jpg';
$response = wp_remote_head( $url );
$this->assertEquals( '404', wp_remote_retrieve_response_code( $headers ) );
$this->skipTestOnTimeout( $response );
$this->assertEquals( '404', wp_remote_retrieve_response_code( $response ) );
}
function test_get_request() {
$url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg';
$response = wp_remote_get( $url );
$this->skipTestOnTimeout( $response );
$headers = wp_remote_retrieve_headers( $response );
$this->assertInternalType( 'array', $response );
@@ -59,6 +90,9 @@ function test_get_redirect() {
$url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
$response = wp_remote_get( $url );
$this->skipTestOnTimeout( $response );
$headers = wp_remote_retrieve_headers( $response );
// should return the same headers as a head request
@@ -73,6 +107,8 @@ function test_get_redirect_limit_exceeded() {
// pretend we've already redirected 5 times
$response = wp_remote_get( $url, array( 'redirection' => -1 ) );
$this->skipTestOnTimeout( $response );
$this->assertWPError( $response );
}
@@ -83,7 +119,10 @@ function test_get_response_cookies() {
$url = 'https://login.wordpress.org/wp-login.php';
$response = wp_remote_head( $url );
$cookies = wp_remote_retrieve_cookies( $response );
$this->skipTestOnTimeout( $response );
$cookies = wp_remote_retrieve_cookies( $response );
$this->assertNotEmpty( $cookies );
@@ -113,7 +152,10 @@ function test_get_response_cookies_with_wp_http_cookie_object() {
new WP_Http_Cookie( array( 'name' => 'test', 'value' => 'foo' ) ),
),
) );
$cookies = wp_remote_retrieve_cookies( $response );
$this->skipTestOnTimeout( $response );
$cookies = wp_remote_retrieve_cookies( $response );
$this->assertNotEmpty( $cookies );
@@ -134,7 +176,10 @@ function test_get_response_cookies_with_name_value_array() {
'test' => 'foo',
),
) );
$cookies = wp_remote_retrieve_cookies( $response );
$this->skipTestOnTimeout( $response );
$cookies = wp_remote_retrieve_cookies( $response );
$this->assertNotEmpty( $cookies );

0 comments on commit 4c6175e

Please sign in to comment.
You can’t perform that action at this time.