summaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorSergey Biryukov <sergeybiryukov@git.wordpress.org>2021-01-30 21:01:52 +0000
committerSergey Biryukov <sergeybiryukov@git.wordpress.org>2021-01-30 21:01:52 +0000
commit36a96f04ac7535b63b492e771f067c565b6ba47e (patch)
treef6a22c23d6c3fa88dbcf8ae3edf6fa155b8ef463
parent3b8037e69fab99b08fa1776641f7a1cc8358d62e (diff)
downloadwordpress-36a96f04ac7535b63b492e771f067c565b6ba47e.tar.gz
wordpress-36a96f04ac7535b63b492e771f067c565b6ba47e.zip
Tests: Use `skipTestOnTimeout()` in more HTTP tests.
Adjust it to handle more types of timeouts, e.g. "Resolving timed out", "Connection timed out". Merges [38757], [43511], [43512], [46682], [46996] to the 4.1 branch. See #51669. git-svn-id: https://develop.svn.wordpress.org/branches/4.1@50103 602fd350-edb4-49c9-b593-d223f7449a82
-rw-r--r--tests/phpunit/includes/testcase.php23
-rw-r--r--tests/phpunit/tests/http/base.php93
-rw-r--r--tests/phpunit/tests/http/functions.php46
3 files changed, 123 insertions, 39 deletions
diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php
index 22d1d86654..f19a513347 100644
--- a/tests/phpunit/includes/testcase.php
+++ b/tests/phpunit/includes/testcase.php
@@ -101,6 +101,29 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
/**
+ * Allow tests to be skipped if the HTTP request times out.
+ *
+ * @param array|WP_Error $response HTTP response.
+ */
+ public 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' );
+ }
+
+ }
+
+ /**
* Unregister existing post types and register defaults.
*
* Run before each test in order to clean up the global scope, in case
diff --git a/tests/phpunit/tests/http/base.php b/tests/phpunit/tests/http/base.php
index 63a5aa14bc..bdf60e91a1 100644
--- a/tests/phpunit/tests/http/base.php
+++ b/tests/phpunit/tests/http/base.php
@@ -44,6 +44,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
function test_redirect_on_301() {
// 5 : 5 & 301
$res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 5) );
+
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error($res) );
$this->assertEquals(200, (int)$res['response']['code'] );
}
@@ -51,6 +53,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
function test_redirect_on_302() {
// 5 : 5 & 302
$res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 5) );
+
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error($res) );
$this->assertEquals(200, (int)$res['response']['code'] );
}
@@ -61,6 +65,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
function test_redirect_on_301_no_redirect() {
// 5 > 0 & 301
$res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 0) );
+
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error($res) );
$this->assertEquals(301, (int)$res['response']['code'] );
}
@@ -71,6 +77,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
function test_redirect_on_302_no_redirect() {
// 5 > 0 & 302
$res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) );
+
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error($res) );
$this->assertEquals(302, (int)$res['response']['code'] );
}
@@ -78,6 +86,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
function test_redirections_equal() {
// 5 - 5
$res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5) );
+
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error($res) );
$this->assertEquals(200, (int)$res['response']['code'] );
}
@@ -85,6 +95,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
function test_no_head_redirections() {
// No redirections on HEAD request:
$res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 1, array('method' => 'HEAD') );
+
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error($res) );
$this->assertEquals( 302, (int)$res['response']['code'] );
}
@@ -95,6 +107,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
function test_redirect_on_head() {
// Redirections on HEAD request when Requested
$res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5, 'method' => 'HEAD') );
+
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error($res) );
$this->assertEquals( 200, (int)$res['response']['code'] );
}
@@ -102,18 +116,24 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
function test_redirections_greater() {
// 10 > 5
$res = wp_remote_request($this->redirection_script . '?rt=' . 10, array('redirection' => 5) );
+
+ $this->skipTestOnTimeout( $res );
$this->assertTrue( is_wp_error($res), print_r($res, true) );
}
function test_redirections_greater_edgecase() {
// 6 > 5 (close edgecase)
$res = wp_remote_request($this->redirection_script . '?rt=' . 6, array('redirection' => 5) );
+
+ $this->skipTestOnTimeout( $res );
$this->assertTrue( is_wp_error($res) );
}
function test_redirections_less_edgecase() {
// 4 < 5 (close edgecase)
$res = wp_remote_request($this->redirection_script . '?rt=' . 4, array('redirection' => 5) );
+
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error($res) );
}
@@ -123,6 +143,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
function test_redirections_zero_redirections_specified() {
// 0 redirections asked for, Should return the document?
$res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) );
+
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error($res) );
$this->assertEquals( 302, (int)$res['response']['code'] );
}
@@ -135,6 +157,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
function test_location_header_on_201() {
// Prints PASS on initial load, FAIL if the client follows the specified redirection
$res = wp_remote_request( $this->redirection_script . '?201-location=true' );
+
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error( $res ) );
$this->assertEquals( 'PASS', $res['body']);
}
@@ -149,6 +173,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
// Test 301 - POST to POST
$res = wp_remote_request( $url, array( 'method' => 'PUT', 'timeout' => 30 ) );
+
+ $this->skipTestOnTimeout( $res );
$this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
$this->assertTrue( !empty( $res['headers']['location'] ) );
}
@@ -161,6 +187,7 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
$headers = array('test1' => 'test', 'test2' => 0, 'test3' => '');
$res = wp_remote_request( $this->redirection_script . '?header-check', array('headers' => $headers) );
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error($res) );
$headers = array();
@@ -188,8 +215,9 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
if ( ! is_wp_error( $res ) ) {
$filesize = filesize( $res['filename'] );
unlink( $res['filename'] );
- }
+ }
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error( $res ) );
$this->assertEquals( '', $res['body'] ); // The body should be empty.
$this->assertEquals( $size, $res['headers']['content-length'] ); // Check the headers are returned (and the size is the same..)
@@ -210,6 +238,7 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
unlink( $res['filename'] );
}
+ $this->skipTestOnTimeout( $res );
$this->assertFalse( is_wp_error( $res ) );
$this->assertEquals( $size, $filesize ); // Check that the file is written to disk correctly without any extra characters
@@ -218,30 +247,47 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
/**
* Test POST redirection methods
*
+ * @dataProvider data_post_redirect_to_method_300
+ *
* @ticket 17588
*/
- function test_post_redirect_to_method_300() {
+ function test_post_redirect_to_method_300( $response_code, $method ) {
$url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?post-redirect-to-method=1';
- // Test 300 - POST to POST
- $res = wp_remote_post( add_query_arg( 'response_code', 300, $url ), array( 'timeout' => 30 ) );
- $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) );
+ $res = wp_remote_post( add_query_arg( 'response_code', $response_code, $url ), array( 'timeout' => 30 ) );
- // Test 301 - POST to POST
- $res = wp_remote_post( add_query_arg( 'response_code', 301, $url ), array( 'timeout' => 30 ) );
- $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) );
-
- // Test 302 - POST to GET
- $res = wp_remote_post( add_query_arg( 'response_code', 302, $url ), array( 'timeout' => 30 ) );
- $this->assertEquals( 'GET', wp_remote_retrieve_body( $res ) );
-
- // Test 303 - POST to GET
- $res = wp_remote_post( add_query_arg( 'response_code', 303, $url ), array( 'timeout' => 30 ) );
- $this->assertEquals( 'GET', wp_remote_retrieve_body( $res ) );
+ $this->skipTestOnTimeout( $res );
+ $this->assertEquals( $method, wp_remote_retrieve_body( $res ) );
+ }
- // Test 304 - POST to POST
- $res = wp_remote_post( add_query_arg( 'response_code', 304, $url ), array( 'timeout' => 30 ) );
- $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) );
+ public function data_post_redirect_to_method_300() {
+ return array(
+ // Test 300 - POST to POST
+ array(
+ 300,
+ 'POST',
+ ),
+ // Test 301 - POST to POST
+ array(
+ 301,
+ 'POST',
+ ),
+ // Test 302 - POST to GET
+ array(
+ 302,
+ 'GET',
+ ),
+ // Test 303 - POST to GET
+ array(
+ 303,
+ 'GET',
+ ),
+ // Test 304 - POST to POST
+ array(
+ 304,
+ 'POST',
+ ),
+ );
}
/**
@@ -261,6 +307,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
);
$res = wp_remote_get( $url, $args );
+
+ $this->skipTestOnTimeout( $res );
$this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
}
@@ -274,10 +322,13 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
$url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?multiple-location-headers=1';
$res = wp_remote_head( $url, array( 'timeout' => 30 ) );
+ $this->skipTestOnTimeout( $res );
$this->assertInternalType( 'array', wp_remote_retrieve_header( $res, 'location' ) );
$this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) );
$res = wp_remote_get( $url, array( 'timeout' => 30 ) );
+
+ $this->skipTestOnTimeout( $res );
$this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
}
@@ -291,6 +342,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
$url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?cookie-test=1';
$res = wp_remote_get( $url );
+
+ $this->skipTestOnTimeout( $res );
$this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
}
@@ -305,6 +358,8 @@ abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
$this->markTestSkipped( 'This install of PHP does not support SSL' );
$res = wp_remote_get( 'https://wordpress.org/' );
+
+ $this->skipTestOnTimeout( $res );
$this->assertTrue( ! is_wp_error( $res ), print_r( $res, true ) );
}
diff --git a/tests/phpunit/tests/http/functions.php b/tests/phpunit/tests/http/functions.php
index 7bb60e863f..ca7bca581d 100644
--- a/tests/phpunit/tests/http/functions.php
+++ b/tests/phpunit/tests/http/functions.php
@@ -17,6 +17,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', $headers, "Reply wasn't array." );
@@ -29,58 +32,61 @@ 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 );
+ $response = wp_remote_head( $url );
- $this->assertInternalType( 'array', $headers, "Reply wasn't array." );
- $this->assertEquals( '404', wp_remote_retrieve_response_code( $headers ) );
+ $this->skipTestOnTimeout( $response );
+ $this->assertInternalType( 'array', $response, "Reply wasn't array." );
+ $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';
- $file = tempnam('/tmp', 'testfile');
- $headers = wp_get_http($url, $file);
+ $response = wp_remote_get( $url );
+
+ $this->skipTestOnTimeout( $response );
+
+ $headers = wp_remote_retrieve_headers( $response );
// should return the same headers as a head request
$this->assertInternalType( 'array', $headers, "Reply wasn't array." );
$this->assertEquals( 'image/jpeg', $headers['content-type'] );
$this->assertEquals( '40148', $headers['content-length'] );
- $this->assertEquals( '200', $headers['response'] );
-
- // make sure the file is ok
- $this->assertEquals( 40148, filesize($file) );
- $this->assertEquals( 'b0371a0fc575fcf77f62cd298571f53b', md5_file($file) );
+ $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) );
}
function test_get_redirect() {
// this will redirect to asdftestblog1.files.wordpress.com
$url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
- $file = tempnam('/tmp', 'testfile');
- $headers = wp_get_http($url, $file);
+ $response = wp_remote_get( $url );
+
+ $this->skipTestOnTimeout( $response );
+
+ $headers = wp_remote_retrieve_headers( $response );
// should return the same headers as a head request
$this->assertInternalType( 'array', $headers, "Reply wasn't array." );
$this->assertEquals( 'image/jpeg', $headers['content-type'] );
$this->assertEquals( '40148', $headers['content-length'] );
- $this->assertEquals( '200', $headers['response'] );
-
- // make sure the file is ok
- $this->assertEquals( 40148, filesize($file) );
- $this->assertEquals( 'b0371a0fc575fcf77f62cd298571f53b', md5_file($file) );
+ $this->assertEquals( '200', wp_remote_retrieve_response_code( $response ) );
}
function test_get_redirect_limit_exceeded() {
// this will redirect to asdftestblog1.files.wordpress.com
$url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg';
- $file = tempnam('/tmp', 'testfile');
+
// pretend we've already redirected 5 times
- $headers = wp_get_http( $url, $file, 6 );
- $this->assertFalse( $headers );
+ $response = wp_remote_get( $url, array( 'redirection' => -1 ) );
+
+ $this->skipTestOnTimeout( $response );
+ $this->assertTrue( is_wp_error( $response ) );
}
}