Skip to content

Commit

Permalink
Pass ID to curl.after_request hook for multiple requests
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Apr 24, 2024
1 parent 7f12bd4 commit 8ce4f3f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Available Hooks

Alter the raw HTTP response before returning for parsing.

Parameters: `string &$headers`, `[array &$info]`
Parameters: `string &$headers`, `[array &$info]`, `[int|string $id]`

The optional `$info` parameter contains the associated array as defined in
the return value for [curl_getinfo()](https://www.php.net/curl-getinfo#refsect1-function.curl-getinfo-returnvalues).
Expand Down
9 changes: 5 additions & 4 deletions src/Transport/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public function request_multiple($requests, $options) {
$responses[$key] = $exception;
$options['hooks']->dispatch('transport.internal.parse_error', [&$responses[$key], $requests[$key]]);
} else {
$responses[$key] = $subrequests[$key]->process_response($subrequests[$key]->response_data, $options);
$responses[$key] = $subrequests[$key]->process_response($subrequests[$key]->response_data, $options, $key);

$options['hooks']->dispatch('transport.internal.parse_response', [&$responses[$key], $requests[$key]]);
}
Expand Down Expand Up @@ -464,13 +464,14 @@ private function setup_handle($url, $headers, $data, $options) {
*
* @param string $response Response data from the body
* @param array $options Request options
* @param int|string|null $id ID of a multi-request
* @return string|false HTTP response data including headers. False if non-blocking.
* @throws \WpOrg\Requests\Exception If the request resulted in a cURL error.
*/
public function process_response($response, $options) {
public function process_response($response, $options, $id = NULL) {

Check failure on line 471 in src/Transport/Curl.php

View workflow job for this annotation

GitHub Actions / PHPCS

TRUE, FALSE and NULL must be lowercase; expected "null" but found "NULL"
if ($options['blocking'] === false) {
$fake_headers = '';
$options['hooks']->dispatch('curl.after_request', [&$fake_headers]);
$options['hooks']->dispatch('curl.after_request', [&$fake_headers, NULL, $id]);

Check failure on line 474 in src/Transport/Curl.php

View workflow job for this annotation

GitHub Actions / PHPCS

TRUE, FALSE and NULL must be lowercase; expected "null" but found "NULL"
return false;
}

Expand All @@ -492,7 +493,7 @@ public function process_response($response, $options) {

$this->info = curl_getinfo($this->handle);

$options['hooks']->dispatch('curl.after_request', [&$this->headers, &$this->info]);
$options['hooks']->dispatch('curl.after_request', [&$this->headers, &$this->info, $id]);
return $this->headers;
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Transport/CurlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WpOrg\Requests\Tests\Transport;

use WpOrg\Requests\Exception;
use WpOrg\Requests\Hooks;
use WpOrg\Requests\Requests;
use WpOrg\Requests\Tests\Transport\BaseTestCase;
use WpOrg\Requests\Transport\Curl;
Expand Down Expand Up @@ -136,4 +137,38 @@ public function testSetsEmptyExpectHeaderIfBodySmallerThan1Mb() {

$this->assertFalse(isset($result['headers']['Expect']));
}

public function testMultipleTriggersCurlAfterRequestHookWithId() {
$requests = [

Check warning on line 142 in tests/Transport/CurlTest.php

View workflow job for this annotation

GitHub Actions / PHPCS

Equals sign not aligned correctly; expected 1 space but found 8 spaces
'get' => [
'url' => $this->httpbin('/get', true),
],
'post' => [
'url' => $this->httpbin('/post', true),
'type' => Requests::POST,
'data' => 'test',
],
];

$mock = $this->getMockBuilder(stdClass::class)
->setMethods(['after_request'])
->getMock();

$mock->expects($this->atLeast(2))
->method('after_request')
->with(
$this->isType('string'),
$this->logicalAnd($this->isType('array'), $this->logicalNot($this->isEmpty())),
$this->isType('string')
);
$hooks = new Hooks();
$hooks->register('curl.after_request', [$mock, 'after_request']);

$options = [
'hooks' => $hooks,
];

Requests::request_multiple($requests, $this->getOptions($options));
}

}

0 comments on commit 8ce4f3f

Please sign in to comment.