Tests: Introduce Tests_HTTP_Functions::skipTestOnTimeout(), mirroring 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
This commit is contained in:
Sergey Biryukov 2018-09-03 21:55:42 +00:00
parent 9e8090fcee
commit 4c6175e800
2 changed files with 52 additions and 7 deletions

View File

@ -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 ) ){

View File

@ -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 @@ class Tests_HTTP_Functions extends WP_UnitTestCase {
// 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 @@ class Tests_HTTP_Functions extends WP_UnitTestCase {
// 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 @@ class Tests_HTTP_Functions extends WP_UnitTestCase {
$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 @@ class Tests_HTTP_Functions extends WP_UnitTestCase {
// 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 @@ class Tests_HTTP_Functions extends WP_UnitTestCase {
$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 @@ class Tests_HTTP_Functions extends WP_UnitTestCase {
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 @@ class Tests_HTTP_Functions extends WP_UnitTestCase {
'test' => 'foo',
),
) );
$cookies = wp_remote_retrieve_cookies( $response );
$this->skipTestOnTimeout( $response );
$cookies = wp_remote_retrieve_cookies( $response );
$this->assertNotEmpty( $cookies );