55 * @group external-http
66 */
77class Tests_HTTP_Functions extends WP_UnitTestCase {
8+
9+ /**
10+ * Mark test as skipped if the HTTP request times out.
11+ */
12+ function skipTestOnTimeout ( $ response ) {
13+ if ( ! is_wp_error ( $ response ) ) {
14+ return ;
15+ }
16+ if ( 'connect() timed out! ' === $ response ->get_error_message () ) {
17+ $ this ->markTestSkipped ( 'HTTP timeout ' );
18+ }
19+
20+ if ( false !== strpos ( $ response ->get_error_message (), 'timed out after ' ) ) {
21+ $ this ->markTestSkipped ( 'HTTP timeout ' );
22+ }
23+
24+ if ( 0 === strpos ( $ response ->get_error_message (), 'stream_socket_client(): unable to connect to tcp://s.w.org:80 ' ) ) {
25+ $ this ->markTestSkipped ( 'HTTP timeout ' );
26+ }
27+
28+ }
29+
830 public function setUp () {
931 if ( ! extension_loaded ( 'openssl ' ) ) {
1032 $ this ->markTestSkipped ( 'Tests_HTTP_Functions requires openssl. ' );
@@ -17,6 +39,9 @@ function test_head_request() {
1739 // this url give a direct 200 response
1840 $ url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg ' ;
1941 $ response = wp_remote_head ( $ url );
42+
43+ $ this ->skipTestOnTimeout ( $ response );
44+
2045 $ headers = wp_remote_retrieve_headers ( $ response );
2146
2247 $ this ->assertInternalType ( 'array ' , $ response );
@@ -30,20 +55,26 @@ function test_head_redirect() {
3055 // this url will 301 redirect
3156 $ url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg ' ;
3257 $ response = wp_remote_head ( $ url );
58+
59+ $ this ->skipTestOnTimeout ( $ response );
3360 $ this ->assertEquals ( '301 ' , wp_remote_retrieve_response_code ( $ response ) );
3461 }
3562
3663 function test_head_404 () {
37- $ url = 'https://asdftestblog1.files.wordpress.com/2007/09/awefasdfawef.jpg ' ;
38- $ headers = wp_remote_head ( $ url );
64+ $ url = 'https://asdftestblog1.files.wordpress.com/2007/09/awefasdfawef.jpg ' ;
65+ $ response = wp_remote_head ( $ url );
3966
40- $ this ->assertEquals ( '404 ' , wp_remote_retrieve_response_code ( $ headers ) );
67+ $ this ->skipTestOnTimeout ( $ response );
68+ $ this ->assertEquals ( '404 ' , wp_remote_retrieve_response_code ( $ response ) );
4169 }
4270
4371 function test_get_request () {
4472 $ url = 'https://asdftestblog1.files.wordpress.com/2007/09/2007-06-30-dsc_4700-1.jpg ' ;
4573
4674 $ response = wp_remote_get ( $ url );
75+
76+ $ this ->skipTestOnTimeout ( $ response );
77+
4778 $ headers = wp_remote_retrieve_headers ( $ response );
4879
4980 $ this ->assertInternalType ( 'array ' , $ response );
@@ -59,6 +90,9 @@ function test_get_redirect() {
5990 $ url = 'https://asdftestblog1.wordpress.com/files/2007/09/2007-06-30-dsc_4700-1.jpg ' ;
6091
6192 $ response = wp_remote_get ( $ url );
93+
94+ $ this ->skipTestOnTimeout ( $ response );
95+
6296 $ headers = wp_remote_retrieve_headers ( $ response );
6397
6498 // should return the same headers as a head request
@@ -73,6 +107,8 @@ function test_get_redirect_limit_exceeded() {
73107
74108 // pretend we've already redirected 5 times
75109 $ response = wp_remote_get ( $ url , array ( 'redirection ' => -1 ) );
110+
111+ $ this ->skipTestOnTimeout ( $ response );
76112 $ this ->assertWPError ( $ response );
77113 }
78114
@@ -83,7 +119,10 @@ function test_get_response_cookies() {
83119 $ url = 'https://login.wordpress.org/wp-login.php ' ;
84120
85121 $ response = wp_remote_head ( $ url );
86- $ cookies = wp_remote_retrieve_cookies ( $ response );
122+
123+ $ this ->skipTestOnTimeout ( $ response );
124+
125+ $ cookies = wp_remote_retrieve_cookies ( $ response );
87126
88127 $ this ->assertNotEmpty ( $ cookies );
89128
@@ -113,7 +152,10 @@ function test_get_response_cookies_with_wp_http_cookie_object() {
113152 new WP_Http_Cookie ( array ( 'name ' => 'test ' , 'value ' => 'foo ' ) ),
114153 ),
115154 ) );
116- $ cookies = wp_remote_retrieve_cookies ( $ response );
155+
156+ $ this ->skipTestOnTimeout ( $ response );
157+
158+ $ cookies = wp_remote_retrieve_cookies ( $ response );
117159
118160 $ this ->assertNotEmpty ( $ cookies );
119161
@@ -134,7 +176,10 @@ function test_get_response_cookies_with_name_value_array() {
134176 'test ' => 'foo ' ,
135177 ),
136178 ) );
137- $ cookies = wp_remote_retrieve_cookies ( $ response );
179+
180+ $ this ->skipTestOnTimeout ( $ response );
181+
182+ $ cookies = wp_remote_retrieve_cookies ( $ response );
138183
139184 $ this ->assertNotEmpty ( $ cookies );
140185
0 commit comments