Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass ID to curl.after_request hook for multiple requests #4

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,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]`
pprkut marked this conversation as resolved.
Show resolved Hide resolved

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) {
if ($options['blocking'] === false) {
$fake_headers = '';
$options['hooks']->dispatch('curl.after_request', [&$fake_headers]);
$options['hooks']->dispatch('curl.after_request', [&$fake_headers, null, $id]);
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 = [
'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));
}

}
Loading