Changeset 42343 for trunk/tests/phpunit/tests/http/base.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/tests/http/base.php (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/http/base.php
r41289 r42343 14 14 // You can use your own version of data/WPHTTP-testcase-redirection-script.php here. 15 15 var $redirection_script = 'http://api.wordpress.org/core/tests/1.0/redirection.php'; 16 var $fileStreamUrl = 'http://s.w.org/screenshots/3.9/dashboard.png';16 var $fileStreamUrl = 'http://s.w.org/screenshots/3.9/dashboard.png'; 17 17 18 18 protected $http_request_args; … … 22 22 */ 23 23 function skipTestOnTimeout( $response ) { 24 if ( ! is_wp_error( $response ) ){24 if ( ! is_wp_error( $response ) ) { 25 25 return; 26 26 } 27 if ( 'connect() timed out!' === $response->get_error_message() ) {27 if ( 'connect() timed out!' === $response->get_error_message() ) { 28 28 $this->markTestSkipped( 'HTTP timeout' ); 29 29 } 30 30 31 if ( 0 === strpos( $response->get_error_message(), 'Operation timed out after' ) ) {31 if ( 0 === strpos( $response->get_error_message(), 'Operation timed out after' ) ) { 32 32 $this->markTestSkipped( 'HTTP timeout' ); 33 33 } … … 41 41 function setUp() { 42 42 43 if ( is_callable( array( 'WP_Http', '_getTransport') ) ) {44 $this->markTestSkipped( 'The WP_Http tests require a class-http.php file of r17550 or later.');43 if ( is_callable( array( 'WP_Http', '_getTransport' ) ) ) { 44 $this->markTestSkipped( 'The WP_Http tests require a class-http.php file of r17550 or later.' ); 45 45 return; 46 46 } 47 47 48 $class = "WP_Http_". ucfirst( $this->transport );49 if ( ! call_user_func( array($class, 'test') ) ) {50 $this->markTestSkipped( sprintf( 'The transport %s is not supported on this system', $this->transport) );48 $class = 'WP_Http_' . ucfirst( $this->transport ); 49 if ( ! call_user_func( array( $class, 'test' ) ) ) { 50 $this->markTestSkipped( sprintf( 'The transport %s is not supported on this system', $this->transport ) ); 51 51 } 52 52 … … 54 54 foreach ( array( 'curl', 'streams', 'fsockopen' ) as $t ) { 55 55 remove_filter( "use_{$t}_transport", '__return_false' ); // Just strip them all 56 if ( $t != $this->transport ) 56 if ( $t != $this->transport ) { 57 57 add_filter( "use_{$t}_transport", '__return_false' ); // and add it back if need be.. 58 } 58 59 } 59 60 } … … 73 74 function test_redirect_on_301() { 74 75 // 5 : 5 & 301 75 $res = wp_remote_request( $this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 5) );76 $this->assertNotWPError( $res ); 77 $this->assertEquals( 200, (int)$res['response']['code'] );76 $res = wp_remote_request( $this->redirection_script . '?code=301&rt=' . 5, array( 'redirection' => 5 ) ); 77 $this->assertNotWPError( $res ); 78 $this->assertEquals( 200, (int) $res['response']['code'] ); 78 79 } 79 80 80 81 function test_redirect_on_302() { 81 82 // 5 : 5 & 302 82 $res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 5) );83 $this->assertNotWPError( $res ); 84 $this->assertEquals( 200, (int)$res['response']['code'] );83 $res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 5 ) ); 84 $this->assertNotWPError( $res ); 85 $this->assertEquals( 200, (int) $res['response']['code'] ); 85 86 } 86 87 … … 90 91 function test_redirect_on_301_no_redirect() { 91 92 // 5 > 0 & 301 92 $res = wp_remote_request( $this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 0) );93 $this->assertNotWPError( $res ); 94 $this->assertEquals( 301, (int)$res['response']['code'] );93 $res = wp_remote_request( $this->redirection_script . '?code=301&rt=' . 5, array( 'redirection' => 0 ) ); 94 $this->assertNotWPError( $res ); 95 $this->assertEquals( 301, (int) $res['response']['code'] ); 95 96 } 96 97 … … 100 101 function test_redirect_on_302_no_redirect() { 101 102 // 5 > 0 & 302 102 $res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) );103 $this->assertNotWPError( $res ); 104 $this->assertEquals( 302, (int)$res['response']['code'] );103 $res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 0 ) ); 104 $this->assertNotWPError( $res ); 105 $this->assertEquals( 302, (int) $res['response']['code'] ); 105 106 } 106 107 107 108 function test_redirections_equal() { 108 109 // 5 - 5 109 $res = wp_remote_request( $this->redirection_script . '?rt=' . 5, array('redirection' => 5) );110 $this->assertNotWPError( $res ); 111 $this->assertEquals( 200, (int)$res['response']['code'] );110 $res = wp_remote_request( $this->redirection_script . '?rt=' . 5, array( 'redirection' => 5 ) ); 111 $this->assertNotWPError( $res ); 112 $this->assertEquals( 200, (int) $res['response']['code'] ); 112 113 } 113 114 114 115 function test_no_head_redirections() { 115 116 // No redirections on HEAD request: 116 $res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 1, array('method' => 'HEAD') );117 $this->assertNotWPError( $res ); 118 $this->assertEquals( 302, (int) $res['response']['code'] );117 $res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 1, array( 'method' => 'HEAD' ) ); 118 $this->assertNotWPError( $res ); 119 $this->assertEquals( 302, (int) $res['response']['code'] ); 119 120 } 120 121 … … 124 125 function test_redirect_on_head() { 125 126 // Redirections on HEAD request when Requested 126 $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5, 'method' => 'HEAD') ); 127 $this->assertNotWPError( $res ); 128 $this->assertEquals( 200, (int)$res['response']['code'] ); 127 $res = wp_remote_request( 128 $this->redirection_script . '?rt=' . 5, array( 129 'redirection' => 5, 130 'method' => 'HEAD', 131 ) 132 ); 133 $this->assertNotWPError( $res ); 134 $this->assertEquals( 200, (int) $res['response']['code'] ); 129 135 } 130 136 131 137 function test_redirections_greater() { 132 138 // 10 > 5 133 $res = wp_remote_request( $this->redirection_script . '?rt=' . 10, array('redirection' => 5) );139 $res = wp_remote_request( $this->redirection_script . '?rt=' . 10, array( 'redirection' => 5 ) ); 134 140 $this->assertWPError( $res ); 135 141 } … … 137 143 function test_redirections_greater_edgecase() { 138 144 // 6 > 5 (close edgecase) 139 $res = wp_remote_request( $this->redirection_script . '?rt=' . 6, array('redirection' => 5) );145 $res = wp_remote_request( $this->redirection_script . '?rt=' . 6, array( 'redirection' => 5 ) ); 140 146 $this->assertWPError( $res ); 141 147 } … … 143 149 function test_redirections_less_edgecase() { 144 150 // 4 < 5 (close edgecase) 145 $res = wp_remote_request( $this->redirection_script . '?rt=' . 4, array('redirection' => 5) );151 $res = wp_remote_request( $this->redirection_script . '?rt=' . 4, array( 'redirection' => 5 ) ); 146 152 $this->assertNotWPError( $res ); 147 153 } … … 152 158 function test_redirections_zero_redirections_specified() { 153 159 // 0 redirections asked for, Should return the document? 154 $res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) );155 $this->assertNotWPError( $res ); 156 $this->assertEquals( 302, (int) $res['response']['code'] );160 $res = wp_remote_request( $this->redirection_script . '?code=302&rt=' . 5, array( 'redirection' => 0 ) ); 161 $this->assertNotWPError( $res ); 162 $this->assertEquals( 302, (int) $res['response']['code'] ); 157 163 } 158 164 … … 166 172 $res = wp_remote_request( $this->redirection_script . '?201-location=true' ); 167 173 $this->assertNotWPError( $res ); 168 $this->assertEquals( 'PASS', $res['body'] );174 $this->assertEquals( 'PASS', $res['body'] ); 169 175 } 170 176 … … 178 184 179 185 // Test 301 - POST to POST 180 $res = wp_remote_request( $url, array( 'method' => 'PUT', 'timeout' => 30 ) ); 186 $res = wp_remote_request( 187 $url, array( 188 'method' => 'PUT', 189 'timeout' => 30, 190 ) 191 ); 181 192 $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) ); 182 $this->assertTrue( ! empty( $res['headers']['location'] ) );193 $this->assertTrue( ! empty( $res['headers']['location'] ) ); 183 194 } 184 195 … … 188 199 function test_send_headers() { 189 200 // Test that the headers sent are recieved by the server 190 $headers = array('test1' => 'test', 'test2' => 0, 'test3' => ''); 191 $res = wp_remote_request( $this->redirection_script . '?header-check', array('headers' => $headers) ); 201 $headers = array( 202 'test1' => 'test', 203 'test2' => 0, 204 'test3' => '', 205 ); 206 $res = wp_remote_request( $this->redirection_script . '?header-check', array( 'headers' => $headers ) ); 192 207 193 208 $this->assertNotWPError( $res ); 194 209 195 210 $headers = array(); 196 foreach ( explode( "\n", $res['body']) as $key => $value ) {197 if ( empty( $value) )211 foreach ( explode( "\n", $res['body'] ) as $key => $value ) { 212 if ( empty( $value ) ) { 198 213 continue; 199 $parts = explode(':', $value,2); 200 unset($headers[$key]); 214 } 215 $parts = explode( ':', $value, 2 ); 216 unset( $headers[ $key ] ); 201 217 $headers[ $parts[0] ] = $parts[1]; 202 218 } 203 219 204 $this->assertTrue( isset( $headers['test1']) && 'test' == $headers['test1'] );205 $this->assertTrue( isset( $headers['test2']) && '0' === $headers['test2'] );220 $this->assertTrue( isset( $headers['test1'] ) && 'test' == $headers['test1'] ); 221 $this->assertTrue( isset( $headers['test2'] ) && '0' === $headers['test2'] ); 206 222 // cURL/HTTP Extension Note: Will never pass, cURL does not pass headers with an empty value. 207 223 // Should it be that empty headers with empty values are NOT sent? … … 210 226 211 227 function test_file_stream() { 212 $url = $this->fileStreamUrl;228 $url = $this->fileStreamUrl; 213 229 $size = 153204; 214 $res = wp_remote_request( $url, array( 'stream' => true, 'timeout' => 30 ) ); //Auto generate the filename. 230 $res = wp_remote_request( 231 $url, array( 232 'stream' => true, 233 'timeout' => 30, 234 ) 235 ); //Auto generate the filename. 215 236 216 237 // Cleanup before we assert, as it'll return early. … … 233 254 */ 234 255 function test_file_stream_limited_size() { 235 $url = $this->fileStreamUrl;256 $url = $this->fileStreamUrl; 236 257 $size = 10000; 237 $res = wp_remote_request( $url, array( 'stream' => true, 'timeout' => 30, 'limit_response_size' => $size ) ); //Auto generate the filename. 258 $res = wp_remote_request( 259 $url, array( 260 'stream' => true, 261 'timeout' => 30, 262 'limit_response_size' => $size, 263 ) 264 ); //Auto generate the filename. 238 265 239 266 // Cleanup before we assert, as it'll return early. … … 256 283 */ 257 284 function test_request_limited_size() { 258 $url = $this->fileStreamUrl;285 $url = $this->fileStreamUrl; 259 286 $size = 10000; 260 287 261 $res = wp_remote_request( $url, array( 'timeout' => 30, 'limit_response_size' => $size ) ); 288 $res = wp_remote_request( 289 $url, array( 290 'timeout' => 30, 291 'limit_response_size' => $size, 292 ) 293 ); 262 294 263 295 $this->skipTestOnTimeout( $res ); … … 312 344 */ 313 345 function test_ip_url_with_host_header() { 314 $ip = gethostbyname( 'api.wordpress.org' );315 $url = 'http://' . $ip . '/core/tests/1.0/redirection.php?print-pass=1';346 $ip = gethostbyname( 'api.wordpress.org' ); 347 $url = 'http://' . $ip . '/core/tests/1.0/redirection.php?print-pass=1'; 316 348 $args = array( 317 'headers' => array(349 'headers' => array( 318 350 'Host' => 'api.wordpress.org', 319 351 ), 320 'timeout' => 30,352 'timeout' => 30, 321 353 'redirection' => 0, 322 354 ); … … 333 365 */ 334 366 function test_https_url_without_ssl_verification() { 335 $url = 'https://wordpress.org/';367 $url = 'https://wordpress.org/'; 336 368 $args = array( 337 369 'sslverify' => false, … … 384 416 */ 385 417 function test_ssl() { 386 if ( ! wp_http_supports( array( 'ssl' ) ) ) 418 if ( ! wp_http_supports( array( 'ssl' ) ) ) { 387 419 $this->fail( 'This installation of PHP does not support SSL' ); 420 } 388 421 389 422 $res = wp_remote_get( 'https://wordpress.org/' ); … … 398 431 399 432 $path = parse_url( $url, PHP_URL_PATH ); 400 $url = str_replace( $path, '/' . $path, $url );433 $url = str_replace( $path, '/' . $path, $url ); 401 434 402 435 $res = wp_remote_request( $url );
Note: See TracChangeset
for help on using the changeset viewer.