Skip to content

Tests: Add unit tests for the HTTP response and request host helper functions - #15611

Open
hmbashar wants to merge 1 commit into
WordPress:trunkfrom
hmbashar:66501-http-retrieve-hosts-tests
Open

hmbashar wants to merge 1 commit into
WordPress:trunkfrom
hmbashar:66501-http-retrieve-hosts-tests

Conversation

@hmbashar

@hmbashar hmbashar commented Nov 6, 2026

Copy link
Copy Markdown

Adds unit tests for four functions in src/wp-includes/http.php that were not referenced by any test. No behavior is changed.

Functions covered, each in its own test class in tests/phpunit/tests/http/:

  • wp_remote_retrieve_header()
    • Returns the value of a header, including repeated headers and values that are '0' or empty, and an empty string when the header is not set.
    • Looks up the headers of a WpOrg\Requests\Utility\CaseInsensitiveDictionary in any case, while the name of a header in a plain array is case sensitive.
    • Returns an empty string for a WP_Error and for a response without headers.
  • wp_remote_retrieve_response_message()
    • Returns the message, and not the code, of the response.
    • Returns an empty string for a WP_Error, for a response without the response data and when it is not an array.
  • allowed_http_request_hosts()
    • Keeps a host that is already external, allows the host of the site and the hosts that are added with the allowed_redirect_hosts filter, and does not allow other hosts.
    • Is hooked to http_request_host_is_external with the expected priority.
  • ms_allowed_http_request_hosts() (@group ms-required)
    • Keeps a host that is already external and allows the domain of the network, both without a query.
    • Allows the domain of a site of the network and does not allow other hosts.
    • Queries the database only once for the same host, and is hooked with the expected priority.

send_origin_headers() is not covered, because it calls exit.

Testing:

  • vendor/bin/phpunit --filter 'Tests_HTTP_(WpRemoteRetrieveHeader|WpRemoteRetrieveResponseMessage|AllowedHttpRequestHosts|MsAllowedHttpRequestHosts)'
  • vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter 'Tests_HTTP_(WpRemoteRetrieveHeader|WpRemoteRetrieveResponseMessage|AllowedHttpRequestHosts|MsAllowedHttpRequestHosts)'
  • vendor/bin/phpcs tests/phpunit/tests/http/

I also temporarily broke these functions in src/wp-includes/http.php (lowercasing the header name, dropping falsy values, removing the checks for a WP_Error and for a response that is not an array, returning the code instead of the message, each of the branches of the two host functions, and the cache of the queried hosts) and confirmed that a new test fails for each one. Replacing isset() and is_array() with is_array( ... ?? null ) in wp_remote_retrieve_response_message() is equivalent, so no test can fail for it.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Written with AI and manually reviewed by me.


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.

…unctions.

Covers `wp_remote_retrieve_header()`, `wp_remote_retrieve_response_message()`, `allowed_http_request_hosts()` and `ms_allowed_http_request_hosts()`, which had no tests.

See #66501.
@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props hmbashar.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the added HTTP helper test coverage.

No open issues 🎉

What changed in this PR

Adds PHPUnit coverage for four HTTP helper functions in src/wp-includes/http.php, supporting Trac ticket #66501 without changing production behavior.

Changes:

  • Tests response header and message retrieval, including error handling and edge cases.
  • Tests single-site and multisite host allowlisting and default filter priorities.
  • Verifies multisite query avoidance and per-host query caching.
File Description
tests/​phpunit/​tests/​http/​wpRemoteRetrieveResponseMessage.php Covers message retrieval and invalid response data.
tests/​phpunit/​tests/​http/​wpRemoteRetrieveHeader.php Covers header values, case sensitivity, missing headers, and errors.
tests/​phpunit/​tests/​http/​msAllowedHttpRequestHosts.php Covers multisite host rules, query caching, and filter registration.
tests/​phpunit/​tests/​http/​allowedHttpRequestHosts.php Covers site and filtered host allowlisting and filter registration.

Review effort: Balanced

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