base.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * Note, When running these tests, remember that some things are done differently
  4. * based on safe_mode. You can run the test in safe_mode like such:
  5. *
  6. * phpunit -d safe_mode=on --group http
  7. *
  8. * You may also need `-d safe_mode_gid=1` to relax the safe_mode checks to allow
  9. * inclusion of PEAR.
  10. *
  11. * The WP_HTTP tests require a class-http.php file of r17550 or later.
  12. */
  13. abstract class WP_HTTP_UnitTestCase extends WP_UnitTestCase {
  14. // You can use your own version of data/WPHTTP-testcase-redirection-script.php here.
  15. var $redirection_script = 'http://api.wordpress.org/core/tests/1.0/redirection.php';
  16. function setUp() {
  17. if ( is_callable( array('WP_HTTP', '_getTransport') ) ) {
  18. $this->markTestSkipped('The WP_HTTP tests require a class-http.php file of r17550 or later.');
  19. return;
  20. }
  21. $class = "WP_HTTP_" . $this->transport;
  22. if ( !call_user_func( array($class, 'test') ) ) {
  23. $this->markTestSkipped( sprintf('The transport %s is not supported on this system', $this->transport) );
  24. }
  25. // Disable all transports aside from this one.
  26. foreach ( array( 'curl', 'streams', 'fsockopen' ) as $t ) {
  27. remove_filter( "use_{$t}_transport", '__return_false' ); // Just strip them all
  28. if ( $t != $this->transport )
  29. add_filter( "use_{$t}_transport", '__return_false' ); // and add it back if need be..
  30. }
  31. }
  32. function tearDown() {
  33. foreach ( array( 'curl', 'streams', 'fsockopen' ) as $t ) {
  34. remove_filter( "use_{$t}_transport", '__return_false' );
  35. }
  36. parent::tearDown();
  37. }
  38. function test_redirect_on_301() {
  39. // 5 : 5 & 301
  40. $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 5) );
  41. $this->assertFalse( is_wp_error($res) );
  42. $this->assertEquals(200, (int)$res['response']['code'] );
  43. }
  44. function test_redirect_on_302() {
  45. // 5 : 5 & 302
  46. $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 5) );
  47. $this->assertFalse( is_wp_error($res) );
  48. $this->assertEquals(200, (int)$res['response']['code'] );
  49. }
  50. /**
  51. * @ticket 16855
  52. */
  53. function test_redirect_on_301_no_redirect() {
  54. // 5 > 0 & 301
  55. $res = wp_remote_request($this->redirection_script . '?code=301&rt=' . 5, array('redirection' => 0) );
  56. $this->assertFalse( is_wp_error($res) );
  57. $this->assertEquals(301, (int)$res['response']['code'] );
  58. }
  59. /**
  60. * @ticket 16855
  61. */
  62. function test_redirect_on_302_no_redirect() {
  63. // 5 > 0 & 302
  64. $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) );
  65. $this->assertFalse( is_wp_error($res) );
  66. $this->assertEquals(302, (int)$res['response']['code'] );
  67. }
  68. function test_redirections_equal() {
  69. // 5 - 5
  70. $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5) );
  71. $this->assertFalse( is_wp_error($res) );
  72. $this->assertEquals(200, (int)$res['response']['code'] );
  73. }
  74. function test_no_head_redirections() {
  75. // No redirections on HEAD request:
  76. $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 1, array('method' => 'HEAD') );
  77. $this->assertFalse( is_wp_error($res) );
  78. $this->assertEquals( 302, (int)$res['response']['code'] );
  79. }
  80. /**
  81. * @ticket 16855
  82. */
  83. function test_redirect_on_head() {
  84. // Redirections on HEAD request when Requested
  85. $res = wp_remote_request($this->redirection_script . '?rt=' . 5, array('redirection' => 5, 'method' => 'HEAD') );
  86. $this->assertFalse( is_wp_error($res) );
  87. $this->assertEquals( 200, (int)$res['response']['code'] );
  88. }
  89. function test_redirections_greater() {
  90. // 10 > 5
  91. $res = wp_remote_request($this->redirection_script . '?rt=' . 10, array('redirection' => 5) );
  92. $this->assertTrue( is_wp_error($res), print_r($res, true) );
  93. }
  94. function test_redirections_greater_edgecase() {
  95. // 6 > 5 (close edgecase)
  96. $res = wp_remote_request($this->redirection_script . '?rt=' . 6, array('redirection' => 5) );
  97. $this->assertTrue( is_wp_error($res) );
  98. }
  99. function test_redirections_less_edgecase() {
  100. // 4 < 5 (close edgecase)
  101. $res = wp_remote_request($this->redirection_script . '?rt=' . 4, array('redirection' => 5) );
  102. $this->assertFalse( is_wp_error($res) );
  103. }
  104. /**
  105. * @ticket 16855
  106. */
  107. function test_redirections_zero_redirections_specified() {
  108. // 0 redirections asked for, Should return the document?
  109. $res = wp_remote_request($this->redirection_script . '?code=302&rt=' . 5, array('redirection' => 0) );
  110. $this->assertFalse( is_wp_error($res) );
  111. $this->assertEquals( 302, (int)$res['response']['code'] );
  112. }
  113. /**
  114. * Do not redirect on non 3xx status codes
  115. *
  116. * @ticket 16889
  117. */
  118. function test_location_header_on_201() {
  119. // Prints PASS on initial load, FAIL if the client follows the specified redirection
  120. $res = wp_remote_request( $this->redirection_script . '?201-location=true' );
  121. $this->assertFalse( is_wp_error( $res ) );
  122. $this->assertEquals( 'PASS', $res['body']);
  123. }
  124. /**
  125. * Test handling of PUT requests on redirects
  126. *
  127. * @ticket 16889
  128. */
  129. function test_no_redirection_on_PUT() {
  130. $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?201-location=1';
  131. // Test 301 - POST to POST
  132. $res = wp_remote_request( $url, array( 'method' => 'PUT', 'timeout' => 30 ) );
  133. $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
  134. $this->assertTrue( !empty( $res['headers']['location'] ) );
  135. }
  136. /**
  137. * @ticket 11888
  138. */
  139. function test_send_headers() {
  140. // Test that the headers sent are recieved by the server
  141. $headers = array('test1' => 'test', 'test2' => 0, 'test3' => '');
  142. $res = wp_remote_request( $this->redirection_script . '?header-check', array('headers' => $headers) );
  143. $this->assertFalse( is_wp_error($res) );
  144. $headers = array();
  145. foreach ( explode("\n", $res['body']) as $key => $value ) {
  146. if ( empty($value) )
  147. continue;
  148. $parts = explode(':', $value,2);
  149. unset($headers[$key]);
  150. $headers[ $parts[0] ] = $parts[1];
  151. }
  152. $this->assertTrue( isset($headers['test1']) && 'test' == $headers['test1'] );
  153. $this->assertTrue( isset($headers['test2']) && '0' === $headers['test2'] );
  154. // cURL/HTTP Extension Note: Will never pass, cURL does not pass headers with an empty value.
  155. // Should it be that empty headers with empty values are NOT sent?
  156. //$this->assertTrue( isset($headers['test3']) && '' === $headers['test3'] );
  157. }
  158. function test_file_stream() {
  159. $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 data
  160. $size = 87348;
  161. $res = wp_remote_request( $url, array( 'stream' => true, 'timeout' => 30 ) ); //Auto generate the filename.
  162. $this->assertFalse( is_wp_error( $res ) );
  163. $this->assertEquals( '', $res['body'] ); // The body should be empty.
  164. $this->assertEquals( $size, $res['headers']['content-length'] ); // Check the headers are returned (and the size is the same..)
  165. $this->assertEquals( $size, filesize($res['filename']) ); // Check that the file is written to disk correctly without any extra characters
  166. unlink($res['filename']); // Remove the temporary file
  167. }
  168. /**
  169. * Test POST redirection methods
  170. *
  171. * @ticket 17588
  172. */
  173. function test_post_redirect_to_method_300() {
  174. $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?post-redirect-to-method=1';
  175. // Test 300 - POST to POST
  176. $res = wp_remote_post( add_query_arg( 'response_code', 300, $url ), array( 'timeout' => 30 ) );
  177. $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) );
  178. // Test 301 - POST to POST
  179. $res = wp_remote_post( add_query_arg( 'response_code', 301, $url ), array( 'timeout' => 30 ) );
  180. $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) );
  181. // Test 302 - POST to GET
  182. $res = wp_remote_post( add_query_arg( 'response_code', 302, $url ), array( 'timeout' => 30 ) );
  183. $this->assertEquals( 'GET', wp_remote_retrieve_body( $res ) );
  184. // Test 303 - POST to GET
  185. $res = wp_remote_post( add_query_arg( 'response_code', 303, $url ), array( 'timeout' => 30 ) );
  186. $this->assertEquals( 'GET', wp_remote_retrieve_body( $res ) );
  187. // Test 304 - POST to POST
  188. $res = wp_remote_post( add_query_arg( 'response_code', 304, $url ), array( 'timeout' => 30 ) );
  189. $this->assertEquals( 'POST', wp_remote_retrieve_body( $res ) );
  190. }
  191. /**
  192. * Test HTTP Requests using an IP url, with a HOST header specified
  193. *
  194. * @ticket 24182
  195. */
  196. function test_ip_url_with_host_header() {
  197. $ip = gethostbyname( 'api.wordpress.org' );
  198. $url = 'http://' . $ip . '/core/tests/1.0/redirection.php?print-pass=1';
  199. $args = array(
  200. 'headers' => array(
  201. 'Host' => 'api.wordpress.org',
  202. ),
  203. 'timeout' => 30,
  204. 'redirection' => 0,
  205. );
  206. $res = wp_remote_get( $url, $args );
  207. $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
  208. }
  209. /**
  210. * Test HTTP Redirects with multiple Location headers specified
  211. *
  212. * @ticket 16890
  213. */
  214. function test_multiple_location_headers() {
  215. $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?multiple-location-headers=1';
  216. $res = wp_remote_head( $url, array( 'timeout' => 30 ) );
  217. $this->assertInternalType( 'array', wp_remote_retrieve_header( $res, 'location' ) );
  218. $this->assertCount( 2, wp_remote_retrieve_header( $res, 'location' ) );
  219. $res = wp_remote_get( $url, array( 'timeout' => 30 ) );
  220. $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
  221. }
  222. /**
  223. * Test HTTP Cookie handling
  224. *
  225. * @ticket 21182
  226. */
  227. function test_cookie_handling() {
  228. $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?cookie-test=1';
  229. $res = wp_remote_get( $url );
  230. $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
  231. }
  232. /**
  233. * Test if HTTPS support works
  234. *
  235. * @group ssl
  236. * @ticket 25007
  237. */
  238. function test_ssl() {
  239. if ( ! wp_http_supports( array( 'ssl' ) ) )
  240. $this->markTestSkipped( 'This install of PHP does not support SSL' );
  241. $res = wp_remote_get( 'https://wordpress.org/' );
  242. $this->assertTrue( ! is_wp_error( $res ), print_r( $res, true ) );
  243. }
  244. }