Changeset 904 in tests for trunk/tests/http/http.php
- Timestamp:
- 07/18/2012 07:01:41 PM (13 years ago)
- Location:
- trunk/tests/http
- Files:
-
- 1 added
- 1 copied
-
. (added)
-
http.php (copied) (copied from trunk/tests/test_http.php) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/http/http.php
r903 r904 1 1 <?php 2 /**3 * Note, When running these tests, remember that some things are done differently4 * based on safe_mode. You can run the test in safe_mode like such:5 *6 * phpunit -d safe_mode=on --group http7 *8 * You may also need `-d safe_mode_gid=1` to relax the safe_mode checks to allow9 * inclusion of PEAR.10 *11 * The WP_HTTP tests require a class-http.php file of r17550 or later.12 */13 14 /**15 * @group http16 */17 abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {18 // You can use your own version of data/WPHTTP-testcase-redirection-script.php here.19 var $redirection_script = 'http://api.wordpress.org/core/tests/1.0/redirection.php';20 21 function setUp() {22 23 if ( is_callable( array('WP_HTTP', '_getTransport') ) ) {24 $this->markTestSkipped('The WP_HTTP tests require a class-http.php file of r17550 or later.');25 return;26 }27 28 $class = "WP_HTTP_" . $this->transport;29 if ( !call_user_func( array($class, 'test') ) ) {30 $this->markTestSkipped( sprintf('The transport %s is not supported on this system', $this->transport) );31 }32 33 // Disable all transports aside from this one.34 foreach ( array( 'curl', 'streams', 'fsockopen' ) as $t ) {35 remove_filter( "use_{$t}_transport", '__return_false' ); // Just strip them all36 if ( $t != $this->transport )37 add_filter( "use_{$t}_transport", '__return_false' ); // and add it back if need be..38 }39 }40 41 function tearDown() {42 foreach ( array( 'curl', 'streams', 'fsockopen' ) as $t ) {43 remove_filter( "use_{$t}_transport", '__return_false' );44 }45 }46 47 function test_redirect_on_301() {48 // 5 : 5 & 30149 $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 5) );50 $this->assertFalse( is_wp_error($res) );51 $this->assertEquals(200, (int)$res['response']['code'] );52 }53 54 function test_redirect_on_302() {55 // 5 : 5 & 30256 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 5) );57 $this->assertFalse( is_wp_error($res) );58 $this->assertEquals(200, (int)$res['response']['code'] );59 }60 61 /**62 * @ticket 1685563 */64 function test_redirect_on_301_no_redirect() {65 // 5 > 0 & 30166 $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 0) );67 $this->assertFalse( is_wp_error($res) );68 $this->assertEquals(301, (int)$res['response']['code'] );69 }70 71 /**72 * @ticket 1685573 */74 function test_redirect_on_302_no_redirect() {75 // 5 > 0 & 30276 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) );77 $this->assertFalse( is_wp_error($res) );78 $this->assertEquals(302, (int)$res['response']['code'] );79 }80 81 function test_redirections_equal() {82 // 5 - 583 $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5) );84 $this->assertFalse( is_wp_error($res) );85 $this->assertEquals(200, (int)$res['response']['code'] );86 }87 88 function test_no_head_redirections() {89 // No redirections on HEAD request:90 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 1, array('method' => 'HEAD') );91 $this->assertFalse( is_wp_error($res) );92 $this->assertEquals( 302, (int)$res['response']['code'] );93 }94 95 /**96 * @ticket 1685597 */98 function test_redirect_on_head() {99 // Redirections on HEAD request when Requested100 $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5, 'method' => 'HEAD') );101 $this->assertFalse( is_wp_error($res) );102 $this->assertEquals( 200, (int)$res['response']['code'] );103 }104 105 function test_redirections_greater() {106 // 10 > 5107 $res = wp_remote_request($this->redirection_script . '?rt=' . 10, array('redirection' => 5) );108 $this->assertTrue( is_wp_error($res), print_r($res, true) );109 }110 111 function test_redirections_greater_edgecase() {112 // 6 > 5 (close edgecase)113 $res = wp_remote_request($this->redirection_script . '?rt=' . 6, array('redirection' => 5) );114 $this->assertTrue( is_wp_error($res) );115 }116 117 function test_redirections_less_edgecase() {118 // 4 < 5 (close edgecase)119 $res = wp_remote_request($this->redirection_script . '?rt=' . 4, array('redirection' => 5) );120 $this->assertFalse( is_wp_error($res) );121 }122 123 /**124 * @ticket 16855125 */126 function test_redirections_zero_redirections_specified() {127 // 0 redirections asked for, Should return the document?128 $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) );129 $this->assertFalse( is_wp_error($res) );130 $this->assertEquals( 302, (int)$res['response']['code'] );131 }132 133 /**134 * Do not redirect on non 3xx status codes135 *136 * @ticket 16889137 *138 * Is this valid? Streams, cURL and therefore PHP Extension (ie. all PHP Internal) follow redirects139 * on all status codes, currently all of WP_HTTP follows this template.140 */141 function test_location_header_on_200() {142 // Prints PASS on initial load, FAIL if the client follows the specified redirection143 $res = wp_remote_request( $this->redirection_script . '?200-location=true' );144 $this->assertEquals( 'PASS', $res['body']);145 }146 147 /**148 * @ticket 11888149 */150 function test_send_headers() {151 // Test that the headers sent are recieved by the server152 $headers = array('test1' => 'test', 'test2' => 0, 'test3' => '');153 $res = wp_remote_request( $this->redirection_script . '?header-check', array('headers' => $headers) );154 155 $this->assertFalse( is_wp_error($res) );156 157 $headers = array();158 foreach ( explode("\n", $res['body']) as $key => $value ) {159 if ( empty($value) )160 continue;161 $parts = explode(':', $value,2);162 unset($heaers[$key]);163 $headers[ $parts[0] ] = $parts[1];164 }165 166 $this->assertTrue( isset($headers['test1']) && 'test' == $headers['test1'] );167 $this->assertTrue( isset($headers['test2']) && '0' === $headers['test2'] );168 // cURL/HTTP Extension Note: Will never pass, cURL does not pass headers with an empty value.169 // Should it be that empty headers with empty values are NOT sent?170 //$this->assertTrue( isset($headers['test3']) && '' === $headers['test3'] );171 }172 173 function test_file_stream() {174 $url = 'http://unit-tests.svn.wordpress.org/trunk/data/images/2004-07-22-DSC_0007.jpg'; // we'll test against a file in the unit test data175 $size = 87348;176 $res = wp_remote_request( $url, array( 'stream' => true, 'timeout' => 30 ) ); //Auto generate the filename.177 178 $this->assertFalse( is_wp_error( $res ) );179 $this->assertEquals( '', $res['body'] ); // The body should be empty.180 $this->assertEquals( $size, $res['headers']['content-length'] ); // Check the headers are returned (and the size is the same..)181 $this->assertEquals( $size, filesize($res['filename']) ); // Check that the file is written to disk correctly without any extra characters182 183 unlink($res['filename']); // Remove the temporary file184 }185 }186 187 // Stubs to test each transport188 /**189 * @group http190 */191 class WPHTTP_curl extends WP_HTTP_UnitTestCase {192 var $transport = 'curl';193 }194 /**195 * @group http196 */197 class WPHTTP_streams extends WP_HTTP_UnitTestCase {198 var $transport = 'streams';199 }200 /**201 * @group http202 */203 class WPHTTP_fsockopen extends WP_HTTP_UnitTestCase {204 var $transport = 'fsockopen';205 }206 207 2 /** 208 3 * Non-transport-specific WP_HTTP Tests … … 210 5 * @group http 211 6 */ 212 class WPHTTP extends WP_UnitTestCase {7 class Tests_HTTP_HTTP extends WP_UnitTestCase { 213 8 214 9 /**
Note: See TracChangeset
for help on using the changeset viewer.