Repository navigation
Description
Is your feature request related to a problem? Please describe.
HttpRequestTrait sees every outbound request, but tests cannot read what it saw. When a test needs to assert on the request itself (method, headers, body, how many calls were made), it adds its own pre_http_request filter at a lower priority that records the request and passes $preempt through, plus the matching add_filter / remove_filter pair and a property to store the result.
WP Rocket has 9 of these spies:
- Varnish
cleanHome,cleanFile,cleanDomain: URL, method, blocking, redirection, headers. - Kinsta
cleanKinstaCacheHome,cleanKinstaCacheUrl: URL, blocking, timeout. - Cloudflare
purgeUrl: JSON body of thepurge_cacherequest. - RocketCDN
WebsiteSearch::find,APIClient::getSubscriptionData: number of calls to one endpoint. - RocketCDN
DataManagerSubscriber::maybeRetryActivation: whether any request was made.
Describe the solution you'd like
Have the trait log the requests it receives and expose them:
/**
* Requests received during the test, in order.
*
* @var array<int, array{url: string, args: array}>
*/
private $http_request_log = [];
public function http_callback( $response, $args, $url ) {
$this->http_request_log[] = [ 'url' => $url, 'args' => $args ];
// ... existing lookup unchanged
}
/**
* Returns the requests received so far, optionally only those to one URL.
*
* @param string|null $url Exact URL to filter on.
*
* @return array<int, array{url: string, args: array}>
*/
protected function get_http_requests( $url = null ) { ... }- Log every request, mocked or blocked (blocked ones still fail the test as today).
reset_http()clears the log.argsholds the parsed request arguments: core firespre_http_requestafter merging defaults, somethod,blocking,timeout,headersandbodyare always present.- Do not reuse or come close to core's property names (see HttpRequestTrait fatals on WordPress 7.2: $blocked_http_requests clashes with WP_UnitTestCase_Base #62);
$http_request_logavoidsWP_UnitTestCase_Base::$blocked_http_requests. - Purely additive, so it can ship as a minor release (v3.4).
Consumer side, the spies become plain assertions:
$this->assertSame( 'PURGE', $this->get_http_requests()[0]['args']['method'] );
$this->assertCount( 1, $this->get_http_requests( self::SEARCH_ENDPOINT ) );
$this->assertSame( [], $this->get_http_requests() );Describe alternatives you've considered
- Keep per-test spy filters: works, but repeats the same filter, cleanup and storage boilerplate in every test, and AGENTS.md in WP Rocket already discourages bespoke
pre_http_requestfilters in integration tests. - Expose
$http_request_counts: only covers the counting cases, not method, headers or body.
Additional context
The log only sees requests that reach the trait's priority-10 filter. A test that short-circuits a request at a lower priority hides it from the log. None of the 9 WP Rocket spies does that: they all return $preempt unchanged.
Acceptance criteria
-
get_http_requests()returns every request received during the current test, in order, withurlandargs. -
get_http_requests( $url )returns only the requests to that exact URL. - Blocked (unmocked) requests are logged too.
- The log is empty at the start of each test.
- Covered in
Tests/Integration/testHttpRequestTrait.php. - PHPCS and PHPStan pass.
Activity
remyperona commentedon Oct 28, 2026
Note
Generated by the AI delivery pipeline (grooming-agent · Claude Opus 5.5).
Grooming Plan — Issue #66
Approach: Log every request in
HttpRequestTrait::http_callback()(first statement, before the fixture lookup, so mocked, list-exhausted and blocked requests are all recorded), store it in a newprivate $http_request_log, expose it viaprotected get_http_requests( $url = null ), and clear it inreset_http().Effort: S · Risk: LOW · Complexity: LOW
Key decisions
src/Integration/HttpRequestTrait.phponly (already in thephpcs.xml.distallow-list; PHPStan covers it). Purely additive, PHP 7.4 compatible, untyped property like its siblings.$http_request_log/get_http_requestsdo not exist in the installed WP test suite and are not close to core's$blocked_http_requests, so no HttpRequestTrait fatals on WordPress 7.2: $blocked_http_requests clashes with WP_UnitTestCase_Base #62-style clash.array_filterinarray_values, otherwiseget_http_requests( $url )[0]breaks (keys preserved).argsare what core passes topre_http_request(verified inWP_Http::request()): defaults merged andhttp_request_argsapplied, but headers are not yet normalised, soheadersis whatever the caller passed (array or raw string). Document this; do not normalise.reset_http()(setup and teardown), so tests must read it beforetear_down_http(). Blocked requests are still reported at teardown as today.argsshape, the priority-10 limitation (lower-priority short-circuit filters hide requests), log unreadable after teardown.Tests (all in
Tests/Integration/testHttpRequestTrait.php): empty log; order with url+args; parsed args (methodPURGE, timeout, blocking, headers, body, plus core defaults); exact-URL filter incl. re-indexing, trailing-slash/query mismatch returning empty; list-mocked URL beyond its list still logged; blocked request logged and teardown still fails; log cleared on teardown and on setup. Do not add a newtry/catchto that file (the PHPStan baseline has acatch.internalClassentry with count 1 there); reusecaptureTearDownFailure(). Existing composition tests must stay green.Verify:
npm run test:php:integration,composer phpcs,composer phpstan.Smoke test: in a consumer-style test using the trait, call
wp_remote_request( $mocked_url, [ 'method' => 'PURGE' ] )and assertget_http_requests()[0]['args']['method'] === 'PURGE'andcount( get_http_requests( $mocked_url ) ) === 1; then request an unmocked URL and confirm it is logged and the test still fails at teardown.Out of scope: logging requests short-circuited at priority < 10, request counters/pattern filtering, normalising args, v3.4 release/tagging, CI or phpcs scope changes.
PR splitting: not needed (effort S, one source file plus one test file).
Full spec:
.TemporaryItems/Issues/phpunit/issues/66-spec.mdtest(HttpRequestTrait): assert setup resets the request log on its own
remyperona commentedon Oct 28, 2026
Note
Generated by the AI delivery pipeline (orchestrator · Claude Opus 5.5).
PR: #67 | Status: READY FOR REVIEW
Closes #66: expose the requests HttpRequestTrait received (#67)