Changeset 64096
- Timestamp:
- 10/05/2026 04:37:40 PM (11 days ago)
- Location:
- trunk/tests/phpunit
- Files:
-
- 1 added
- 1 edited
-
includes/class-wp-http-unit-test-transport.php (added)
-
tests/http/functions.php (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/http/functions.php
r62621 r64096 1 1 <?php 2 3 require_once DIR_TESTDATA . '/../includes/class-wp-http-unit-test-transport.php'; 2 4 3 5 /** 4 6 * @group http 5 * @group external-http 7 * 8 * @ticket 63914 6 9 */ 7 10 class Tests_HTTP_Functions extends WP_UnitTestCase { 8 11 9 12 /** 13 * Whether the current test is using the fake Requests transport. 14 * 15 * @var bool 16 */ 17 private $using_mock_transport = false; 18 19 /** 20 * Tear down the fake transport hook when used. 21 */ 22 public function tear_down() { 23 if ( $this->using_mock_transport ) { 24 remove_action( 'requests-requests.before_request', array( $this, 'inject_mock_transport' ), 10 ); 25 $this->using_mock_transport = false; 26 } 27 28 parent::tear_down(); 29 } 30 31 /** 32 * Enables the fake Requests transport for tests that must exercise 33 * WP_Http request handling without live network access. 34 * 35 * Removes the core external-HTTP blocker so the request proceeds past 36 * `pre_http_request` into cookie normalization, transport execution, and 37 * response conversion. 38 */ 39 private function use_mock_transport() { 40 remove_filter( 'pre_http_request', array( $this, 'block_external_http_request' ), PHP_INT_MAX ); 41 add_action( 'requests-requests.before_request', array( $this, 'inject_mock_transport' ), 10, 5 ); 42 $this->using_mock_transport = true; 43 } 44 45 /** 46 * Injects the fake Requests transport into request options. 47 * 48 * @param string $url Request URL. 49 * @param array $headers Request headers. 50 * @param string|array $data Request data. 51 * @param string $type HTTP method. 52 * @param array $options Request options (passed by reference). 53 */ 54 public function inject_mock_transport( $url, $headers, $data, $type, &$options ) { 55 $options['transport'] = new WP_Http_Unit_Test_Transport(); 56 } 57 58 /** 10 59 * @covers ::wp_remote_head 60 * @covers ::wp_remote_retrieve_headers 61 * @covers ::wp_remote_retrieve_response_code 11 62 */ 12 63 public function test_head_request() { 64 $this->use_mock_transport(); 65 13 66 // This URL gives a direct 200 response. 14 67 $url = 'https://s.w.org/screenshots/3.9/dashboard.png'; 15 $response = $this->wp_remote_head( $url );68 $response = wp_remote_head( $url ); 16 69 17 70 $this->assertNotWPError( $response ); … … 29 82 */ 30 83 public function test_head_redirect() { 31 // This URL will 301 redirect. 84 $this->use_mock_transport(); 85 86 // This URL will 301 redirect. HEAD requests do not follow redirects by default. 32 87 $url = 'https://wp.org/screenshots/3.9/dashboard.png'; 33 $response = $this->wp_remote_head( $url );88 $response = wp_remote_head( $url ); 34 89 35 90 $this->assertNotWPError( $response ); … … 39 94 /** 40 95 * @covers ::wp_remote_head 96 * @covers ::wp_remote_retrieve_response_code 41 97 */ 42 98 public function test_head_404() { 99 $this->use_mock_transport(); 100 43 101 $url = 'https://wordpress.org/screenshots/3.9/awefasdfawef.jpg'; 44 $response = $this->wp_remote_head( $url );102 $response = wp_remote_head( $url ); 45 103 46 104 $this->assertNotWPError( $response ); … … 54 112 */ 55 113 public function test_get_request() { 114 $this->use_mock_transport(); 115 56 116 $url = 'https://s.w.org/screenshots/3.9/dashboard.png'; 57 117 58 $response = $this->wp_remote_get( $url );118 $response = wp_remote_get( $url ); 59 119 60 120 $this->assertNotWPError( $response ); … … 74 134 */ 75 135 public function test_get_redirect() { 136 $this->use_mock_transport(); 137 76 138 // This will redirect to wordpress.org. 77 139 $url = 'https://wp.org/screenshots/3.9/dashboard.png'; 78 140 79 $response = $this->wp_remote_get( $url );141 $response = wp_remote_get( $url ); 80 142 81 143 $this->assertNotWPError( $response ); … … 83 145 $headers = wp_remote_retrieve_headers( $response ); 84 146 85 // Should return the same headers as a HEAD request.147 // GET follows the redirect and returns the final image response. 86 148 $this->assertSame( 200, wp_remote_retrieve_response_code( $response ) ); 87 149 $this->assertSame( 'image/png', $headers['Content-Type'] ); … … 93 155 */ 94 156 public function test_get_redirect_limit_exceeded() { 157 $this->use_mock_transport(); 158 95 159 // This will redirect to wordpress.org. 96 160 $url = 'https://wp.org/screenshots/3.9/dashboard.png'; 97 161 98 162 // Pretend we've already redirected 5 times. 99 $response = $this->wp_remote_get( $url, array( 'redirection' => -1 ) );163 $response = wp_remote_get( $url, array( 'redirection' => -1 ) ); 100 164 101 165 $this->assertWPError( $response ); 166 $this->assertSame( 'http_request_failed', $response->get_error_code() ); 102 167 } 103 168 … … 109 174 * @covers ::wp_remote_retrieve_cookie 110 175 * @covers ::wp_remote_retrieve_cookie_value 176 * @covers WP_HTTP_Requests_Response::get_cookies 111 177 */ 112 178 public function test_get_response_cookies() { 179 $this->use_mock_transport(); 180 113 181 $url = 'https://login.wordpress.org/wp-login.php'; 114 182 115 $response = $this->wp_remote_head( $url );183 $response = wp_remote_head( $url ); 116 184 117 185 $this->assertNotWPError( $response ); … … 142 210 * @covers ::wp_remote_retrieve_cookies 143 211 * @covers ::wp_remote_retrieve_cookie 212 * @covers WP_Http::normalize_cookies 144 213 */ 145 214 public function test_get_response_cookies_with_wp_http_cookie_object() { 215 $this->use_mock_transport(); 216 146 217 $url = 'https://login.wordpress.org/wp-login.php'; 147 218 148 $response = $this->wp_remote_get(219 $response = wp_remote_get( 149 220 $url, 150 221 array( … … 178 249 * @covers ::wp_remote_retrieve_cookies 179 250 * @covers ::wp_remote_retrieve_cookie 251 * @covers WP_Http::normalize_cookies 180 252 */ 181 253 public function test_get_response_cookies_with_name_value_array() { 254 $this->use_mock_transport(); 255 182 256 $url = 'https://login.wordpress.org/wp-login.php'; 183 257 184 $response = $this->wp_remote_get(258 $response = wp_remote_get( 185 259 $url, 186 260 array(
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)