- Timestamp:
- 01/29/2025 06:17:34 PM (7 weeks ago)
- Location:
- trunk/tests/phpunit
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/includes/abstract-testcase.php
r59057 r59729 1695 1695 touch( $file ); 1696 1696 } 1697 1698 /** 1699 * Wrapper for `wp_safe_remote_request()` that retries on error and skips the test on timeout. 1700 * 1701 * @param string $url URL to retrieve. 1702 * @param array $args Optional. Request arguments. Default empty array. 1703 * @return array|WP_Error The response or WP_Error on failure. 1704 */ 1705 protected function wp_safe_remote_request( $url, $args = array() ) { 1706 return self::retry_on_error( 'wp_safe_remote_request', $url, $args ); 1707 } 1708 1709 /** 1710 * Wrapper for `wp_safe_remote_get()` that retries on error and skips the test on timeout. 1711 * 1712 * @param string $url URL to retrieve. 1713 * @param array $args Optional. Request arguments. Default empty array. 1714 * @return array|WP_Error The response or WP_Error on failure. 1715 */ 1716 protected function wp_safe_remote_get( $url, $args = array() ) { 1717 return self::retry_on_error( 'wp_safe_remote_get', $url, $args ); 1718 } 1719 1720 /** 1721 * Wrapper for `wp_safe_remote_post()` that retries on error and skips the test on timeout. 1722 * 1723 * @param string $url URL to retrieve. 1724 * @param array $args Optional. Request arguments. Default empty array. 1725 * @return array|WP_Error The response or WP_Error on failure. 1726 */ 1727 protected function wp_safe_remote_post( $url, $args = array() ) { 1728 return self::retry_on_error( 'wp_safe_remote_post', $url, $args ); 1729 } 1730 1731 /** 1732 * Wrapper for `wp_safe_remote_head()` that retries on error and skips the test on timeout. 1733 * 1734 * @param string $url URL to retrieve. 1735 * @param array $args Optional. Request arguments. Default empty array. 1736 * @return array|WP_Error The response or WP_Error on failure. 1737 */ 1738 protected function wp_safe_remote_head( $url, $args = array() ) { 1739 return self::retry_on_error( 'wp_safe_remote_head', $url, $args ); 1740 } 1741 1742 /** 1743 * Wrapper for `wp_remote_request()` that retries on error and skips the test on timeout. 1744 * 1745 * @param string $url URL to retrieve. 1746 * @param array $args Optional. Request arguments. Default empty array. 1747 * @return array|WP_Error The response or WP_Error on failure. 1748 */ 1749 protected function wp_remote_request( $url, $args = array() ) { 1750 return self::retry_on_error( 'wp_remote_request', $url, $args ); 1751 } 1752 1753 /** 1754 * Wrapper for `wp_remote_get()` that retries on error and skips the test on timeout. 1755 * 1756 * @param string $url URL to retrieve. 1757 * @param array $args Optional. Request arguments. Default empty array. 1758 * @return array|WP_Error The response or WP_Error on failure. 1759 */ 1760 protected function wp_remote_get( $url, $args = array() ) { 1761 return self::retry_on_error( 'wp_remote_get', $url, $args ); 1762 } 1763 1764 /** 1765 * Wrapper for `wp_remote_post()` that retries on error and skips the test on timeout. 1766 * 1767 * @param string $url URL to retrieve. 1768 * @param array $args Optional. Request arguments. Default empty array. 1769 * @return array|WP_Error The response or WP_Error on failure. 1770 */ 1771 protected function wp_remote_post( $url, $args = array() ) { 1772 return self::retry_on_error( 'wp_remote_post', $url, $args ); 1773 } 1774 1775 /** 1776 * Wrapper for `wp_remote_head()` that retries on error and skips the test on timeout. 1777 * 1778 * @param string $url URL to retrieve. 1779 * @param array $args Optional. Request arguments. Default empty array. 1780 * @return array|WP_Error The response or WP_Error on failure. 1781 */ 1782 protected function wp_remote_head( $url, $args = array() ) { 1783 return self::retry_on_error( 'wp_remote_head', $url, $args ); 1784 } 1785 1786 /** 1787 * Retries an HTTP API request up to three times and skips the test on timeout. 1788 * 1789 * @param callable $callback The HTTP API request function to call. 1790 * @param string $url URL to retrieve. 1791 * @param array $args Request arguments. 1792 * @return array|WP_Error The response or WP_Error on failure. 1793 */ 1794 private function retry_on_error( callable $callback, $url, $args ) { 1795 $attempts = 0; 1796 1797 while ( $attempts < 3 ) { 1798 $result = call_user_func( $callback, $url, $args ); 1799 1800 if ( ! is_wp_error( $result ) ) { 1801 return $result; 1802 } 1803 1804 ++$attempts; 1805 sleep( 5 ); 1806 } 1807 1808 $this->skipTestOnTimeout( $result ); 1809 1810 return $result; 1811 } 1697 1812 } -
trunk/tests/phpunit/tests/http/base.php
r58108 r59729 45 45 public function test_redirect_on_301() { 46 46 // 5 : 5 & 301. 47 $res = wp_remote_request( $this->redirection_script . '?code=301&rt=' . 5, array( 'redirection' => 5 ) ); 48 49 $this->skipTestOnTimeout( $res ); 47 $res = $this->wp_remote_request( $this->redirection_script . '?code=301&rt=' . 5, array( 'redirection' => 5 ) ); 48 50 49 $this->assertNotWPError( $res ); 51 50 $this->assertSame( 200, (int) $res['response']['code'] ); … … 57 56 public function test_redirect_on_302() { 58 57 // 5 : 5 & 302. 59 $res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 5 ) ); 60 61 $this->skipTestOnTimeout( $res ); 58 $res = $this->wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 5 ) ); 59 62 60 $this->assertNotWPError( $res ); 63 61 $this->assertSame( 200, (int) $res['response']['code'] ); … … 71 69 public function test_redirect_on_301_no_redirect() { 72 70 // 5 > 0 & 301. 73 $res = wp_remote_request( $this->redirection_script . '?code=301&rt=' . 5, array( 'redirection' => 0 ) ); 74 75 $this->skipTestOnTimeout( $res ); 71 $res = $this->wp_remote_request( $this->redirection_script . '?code=301&rt=' . 5, array( 'redirection' => 0 ) ); 72 76 73 $this->assertNotWPError( $res ); 77 74 $this->assertSame( 301, (int) $res['response']['code'] ); … … 85 82 public function test_redirect_on_302_no_redirect() { 86 83 // 5 > 0 & 302. 87 $res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 0 ) ); 88 89 $this->skipTestOnTimeout( $res ); 84 $res = $this->wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 0 ) ); 85 90 86 $this->assertNotWPError( $res ); 91 87 $this->assertSame( 302, (int) $res['response']['code'] ); … … 97 93 public function test_redirections_equal() { 98 94 // 5 - 5. 99 $res = wp_remote_request( $this->redirection_script . '?rt=' . 5, array( 'redirection' => 5 ) ); 100 101 $this->skipTestOnTimeout( $res ); 95 $res = $this->wp_remote_request( $this->redirection_script . '?rt=' . 5, array( 'redirection' => 5 ) ); 96 102 97 $this->assertNotWPError( $res ); 103 98 $this->assertSame( 200, (int) $res['response']['code'] ); … … 109 104 public function test_no_head_redirections() { 110 105 // No redirections on HEAD request. 111 $res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 1, array( 'method' => 'HEAD' ) ); 112 113 $this->skipTestOnTimeout( $res ); 106 $res = $this->wp_remote_request( $this->redirection_script . '?code=302&rt=' . 1, array( 'method' => 'HEAD' ) ); 107 114 108 $this->assertNotWPError( $res ); 115 109 $this->assertSame( 302, (int) $res['response']['code'] ); … … 123 117 public function test_redirect_on_head() { 124 118 // Redirections on HEAD request when Requested. 125 $res = wp_remote_request(119 $res = $this->wp_remote_request( 126 120 $this->redirection_script . '?rt=' . 5, 127 121 array( … … 131 125 ); 132 126 133 $this->skipTestOnTimeout( $res );134 127 $this->assertNotWPError( $res ); 135 128 $this->assertSame( 200, (int) $res['response']['code'] ); … … 141 134 public function test_redirections_greater() { 142 135 // 10 > 5. 143 $res = wp_remote_request( $this->redirection_script . '?rt=' . 10, array( 'redirection' => 5 ) ); 144 145 $this->skipTestOnTimeout( $res ); 136 $res = $this->wp_remote_request( $this->redirection_script . '?rt=' . 10, array( 'redirection' => 5 ) ); 137 146 138 $this->assertWPError( $res ); 147 139 } … … 152 144 public function test_redirections_greater_edgecase() { 153 145 // 6 > 5 (close edge case). 154 $res = wp_remote_request( $this->redirection_script . '?rt=' . 6, array( 'redirection' => 5 ) ); 155 156 $this->skipTestOnTimeout( $res ); 146 $res = $this->wp_remote_request( $this->redirection_script . '?rt=' . 6, array( 'redirection' => 5 ) ); 147 157 148 $this->assertWPError( $res ); 158 149 } … … 163 154 public function test_redirections_less_edgecase() { 164 155 // 4 < 5 (close edge case). 165 $res = wp_remote_request( $this->redirection_script . '?rt=' . 4, array( 'redirection' => 5 ) ); 166 167 $this->skipTestOnTimeout( $res ); 156 $res = $this->wp_remote_request( $this->redirection_script . '?rt=' . 4, array( 'redirection' => 5 ) ); 157 168 158 $this->assertNotWPError( $res ); 169 159 } … … 176 166 public function test_redirections_zero_redirections_specified() { 177 167 // 0 redirections asked for, should return the document? 178 $res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 0 ) ); 179 180 $this->skipTestOnTimeout( $res ); 168 $res = $this->wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 0 ) ); 169 181 170 $this->assertNotWPError( $res ); 182 171 $this->assertSame( 302, (int) $res['response']['code'] ); … … 192 181 public function test_location_header_on_201() { 193 182 // Prints PASS on initial load, FAIL if the client follows the specified redirection. 194 $res = wp_remote_request( $this->redirection_script . '?201-location=true' ); 195 196 $this->skipTestOnTimeout( $res ); 183 $res = $this->wp_remote_request( $this->redirection_script . '?201-location=true' ); 184 197 185 $this->assertNotWPError( $res ); 198 186 $this->assertSame( 'PASS', $res['body'] ); … … 211 199 212 200 // Test 301 - POST to POST. 213 $res = wp_remote_request(201 $res = $this->wp_remote_request( 214 202 $url, 215 203 array( … … 219 207 ); 220 208 221 $this->skipTestOnTimeout( $res );222 209 $this->assertNotWPError( $res ); 223 210 $this->assertSame( 'PASS', wp_remote_retrieve_body( $res ) ); … … 237 224 'test3' => '', 238 225 ); 239 $res = wp_remote_request( $this->redirection_script . '?header-check', array( 'headers' => $headers ) ); 240 241 $this->skipTestOnTimeout( $res ); 226 $res = $this->wp_remote_request( $this->redirection_script . '?header-check', array( 'headers' => $headers ) ); 227 242 228 $this->assertNotWPError( $res ); 243 229 … … 268 254 $url = $this->file_stream_url; 269 255 $size = 153204; 270 $res = wp_remote_request(256 $res = $this->wp_remote_request( 271 257 $url, 272 258 array( … … 282 268 } 283 269 284 $this->skipTestOnTimeout( $res );285 270 $this->assertNotWPError( $res ); 286 271 $this->assertSame( '', $res['body'] ); // The body should be empty. … … 298 283 $url = $this->file_stream_url; 299 284 $size = 10000; 300 $res = wp_remote_request(285 $res = $this->wp_remote_request( 301 286 $url, 302 287 array( … … 313 298 } 314 299 315 $this->skipTestOnTimeout( $res );316 300 $this->assertNotWPError( $res ); 317 301 $this->assertSame( $size, $filesize ); // Check that the file is written to disk correctly without any extra characters. … … 329 313 $size = 10000; 330 314 331 $res = wp_remote_request(315 $res = $this->wp_remote_request( 332 316 $url, 333 317 array( … … 337 321 ); 338 322 339 $this->skipTestOnTimeout( $res );340 323 $this->assertNotWPError( $res ); 341 324 $this->assertSame( $size, strlen( $res['body'] ) ); … … 355 338 $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?post-redirect-to-method=1'; 356 339 357 $res = wp_remote_post( add_query_arg( 'response_code', $response_code, $url ), array( 'timeout' => 30 ) ); 358 359 $this->skipTestOnTimeout( $res ); 340 $res = $this->wp_remote_post( add_query_arg( 'response_code', $response_code, $url ), array( 'timeout' => 30 ) ); 341 360 342 $this->assertNotWPError( $res ); 361 343 $this->assertSame( $method, wp_remote_retrieve_body( $res ) ); … … 406 388 ); 407 389 408 $res = wp_remote_get( $url, $args ); 409 410 $this->skipTestOnTimeout( $res ); 390 $res = $this->wp_remote_get( $url, $args ); 391 411 392 $this->assertNotWPError( $res ); 412 393 $this->assertSame( 'PASS', wp_remote_retrieve_body( $res ) ); … … 428 409 add_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) ); 429 410 430 $res = wp_remote_head( $url, $args );411 $res = $this->wp_remote_head( $url, $args ); 431 412 432 413 remove_filter( 'http_request_args', array( $this, 'filter_http_request_args' ) ); 433 414 434 $this->skipTestOnTimeout( $res );435 415 $this->assertNotEmpty( $this->http_request_args['sslcertificates'] ); 436 416 $this->assertNotWPError( $res ); … … 448 428 $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?cookie-test=1'; 449 429 450 $res = wp_remote_get( $url ); 451 452 $this->skipTestOnTimeout( $res ); 430 $res = $this->wp_remote_get( $url ); 431 453 432 $this->assertNotWPError( $res ); 454 433 $this->assertSame( 'PASS', wp_remote_retrieve_body( $res ) ); … … 468 447 } 469 448 470 $res = wp_remote_get( 'https://wordpress.org/' ); 471 472 $this->skipTestOnTimeout( $res ); 449 $res = $this->wp_remote_get( 'https://wordpress.org/' ); 450 473 451 $this->assertNotWPError( $res ); 474 452 } … … 485 463 $url = str_replace( $path, '/' . $path, $url ); 486 464 487 $res = wp_remote_request( $url ); 488 489 $this->skipTestOnTimeout( $res ); 465 $res = $this->wp_remote_request( $url ); 466 490 467 $this->assertNotWPError( $res ); 491 468 } -
trunk/tests/phpunit/tests/http/functions.php
r58108 r59729 13 13 // This URL gives a direct 200 response. 14 14 $url = 'https://s.w.org/screenshots/3.9/dashboard.png'; 15 $response = wp_remote_head( $url ); 16 17 $this->skipTestOnTimeout( $response ); 15 $response = $this->wp_remote_head( $url ); 16 18 17 $this->assertNotWPError( $response ); 19 18 … … 32 31 // This URL will 301 redirect. 33 32 $url = 'https://wp.org/screenshots/3.9/dashboard.png'; 34 $response = wp_remote_head( $url ); 35 36 $this->skipTestOnTimeout( $response ); 33 $response = $this->wp_remote_head( $url ); 34 37 35 $this->assertNotWPError( $response ); 38 36 $this->assertSame( 301, wp_remote_retrieve_response_code( $response ) ); … … 44 42 public function test_head_404() { 45 43 $url = 'https://wordpress.org/screenshots/3.9/awefasdfawef.jpg'; 46 $response = wp_remote_head( $url ); 47 48 $this->skipTestOnTimeout( $response ); 44 $response = $this->wp_remote_head( $url ); 45 49 46 $this->assertNotWPError( $response ); 50 47 $this->assertSame( 404, wp_remote_retrieve_response_code( $response ) ); … … 59 56 $url = 'https://s.w.org/screenshots/3.9/dashboard.png'; 60 57 61 $response = wp_remote_get( $url ); 62 63 $this->skipTestOnTimeout( $response ); 58 $response = $this->wp_remote_get( $url ); 59 64 60 $this->assertNotWPError( $response ); 65 61 … … 81 77 $url = 'https://wp.org/screenshots/3.9/dashboard.png'; 82 78 83 $response = wp_remote_get( $url ); 84 85 $this->skipTestOnTimeout( $response ); 79 $response = $this->wp_remote_get( $url ); 80 86 81 $this->assertNotWPError( $response ); 87 82 … … 102 97 103 98 // Pretend we've already redirected 5 times. 104 $response = wp_remote_get( $url, array( 'redirection' => -1 ) ); 105 106 $this->skipTestOnTimeout( $response ); 99 $response = $this->wp_remote_get( $url, array( 'redirection' => -1 ) ); 100 107 101 $this->assertWPError( $response ); 108 102 } … … 119 113 $url = 'https://login.wordpress.org/wp-login.php'; 120 114 121 $response = wp_remote_head( $url ); 122 123 $this->skipTestOnTimeout( $response ); 115 $response = $this->wp_remote_head( $url ); 116 124 117 $this->assertNotWPError( $response ); 125 118 … … 153 146 $url = 'https://login.wordpress.org/wp-login.php'; 154 147 155 $response = wp_remote_get(148 $response = $this->wp_remote_get( 156 149 $url, 157 150 array( … … 167 160 ); 168 161 169 $this->skipTestOnTimeout( $response );170 162 $this->assertNotWPError( $response ); 171 163 … … 190 182 $url = 'https://login.wordpress.org/wp-login.php'; 191 183 192 $response = wp_remote_get(184 $response = $this->wp_remote_get( 193 185 $url, 194 186 array( … … 199 191 ); 200 192 201 $this->skipTestOnTimeout( $response );202 193 $this->assertNotWPError( $response ); 203 194 -
trunk/tests/phpunit/tests/readme.php
r58108 r59729 92 92 */ 93 93 public function get_response_body( $url ) { 94 $response = wp_remote_get( $url );94 $response = $this->wp_remote_get( $url ); 95 95 96 $this->skipTestOnTimeout( $response );97 96 $this->assertNotWPError( $response ); 98 97
Note: See TracChangeset
for help on using the changeset viewer.