Skip to content

Build/Test Tools: Mock the rejected requests in the wp_get_http_headers() tests. - #13603

Draft
adimoldovan wants to merge 2 commits into
WordPress:trunkfrom
adimoldovan:63914-http-wp-get-http-headers
Draft

adimoldovan wants to merge 2 commits into
WordPress:trunkfrom
adimoldovan:63914-http-wp-get-http-headers

Conversation

@adimoldovan

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/63914

Two tests in Tests_HTTP_wpGetHttpHeaders call wp_get_http_headers() with not_an_url and does_not_matter. wp_safe_remote_head() rejects both in wp_http_validate_url(), so no request left the machine. But pre_http_request runs before that check, and the mock in this class passed those URLs on. The guard from [63560] then fails both tests.

The mock now returns the WP_Error that WP_Http::request() returns for an invalid URL, and the tests leave the external-http group. wp_get_http_headers() gets the same error as before and returns false. Tests_HTTP_HTTP keeps the coverage of wp_http_validate_url().

The group goes from 80 tests to 78.

Testing Instructions

  1. Start the environment: npm run env:start && npm run env:install.
  2. Run npm run test:php -- --group external-http. Expect 78 tests and no failures.
  3. Run npm run test:php -- --exclude-group external-http tests/phpunit/tests/http/wpGetHttpHeaders.php. Expect 3 tests and no failures.
  4. Confirm the tests need the mock. Change the last line of mock_http_request() back to return $response;, then run step 3 again. Expect two failures: "This test made an external HTTP request but is not in the external-http group.", for not_an_url and does_not_matter. Restore the line.
  5. Check the coding standards: composer lint tests/phpunit/tests/http/wpGetHttpHeaders.php.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Fable 5.1
Used for: Tracing the request path, proving the tests made no network call, running the test suites.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

…ers()` tests.

Two tests call `wp_get_http_headers()` with a URL that
`wp_http_validate_url()` rejects. No request left the machine. But
`pre_http_request` runs before that check, and the mock in this class
passed those URLs on, so the guard against ungrouped external requests
fails both tests.

The mock now returns the `WP_Error` that `WP_Http::request()` returns
for an invalid URL. The tests leave the `external-http` group.
@adimoldovan adimoldovan self-assigned this Sep 18, 2026
}

return $response;
return new WP_Error( 'http_request_failed', 'A valid URL was not provided.' );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mock's @return false|array|WP_Error and its $response parameter are now dead, it can never return false and never reads $response.

Does that make sense?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants