Fix #707: Call MultiCurl::beforeSend() before each request is made

This commit is contained in:
Zach Borboa
2022-07-01 11:17:22 -04:00
parent d453257b12
commit a33b7bde4e
3 changed files with 46 additions and 0 deletions
+2
View File
@@ -1016,6 +1016,8 @@ class MultiCurl
'cURL multi add handle error: ' . curl_multi_strerror($curlm_error_code)
);
}
$curl->call($curl->beforeSendCallback);
} else {
$curl->execDone();
+21
View File
@@ -4293,4 +4293,25 @@ class CurlTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(Test::TEST_URL, $curl->url);
$this->assertEquals(Test::TEST_URL, $curl->effectiveUrl);
}
public function testBeforeSendEachRequest()
{
// Ensure Curl::beforeSend() is called before each request including retries.
$test = new Test();
$test->curl->setOpt(CURLOPT_COOKIEJAR, '/dev/null');
$test->curl->setRetry(5);
$before_send_call_count = 0;
$test->curl->beforeSend(function ($instance) use (&$before_send_call_count) {
$before_send_call_count += 1;
});
$test->server('retry', 'GET', ['failures' => 5]);
$this->assertEquals(6, $before_send_call_count);
$this->assertEquals(6, $test->curl->attempts);
$this->assertEquals(5, $test->curl->retries);
$this->assertFalse($test->curl->error);
}
}
@@ -5015,4 +5015,27 @@ class MultiCurlTest extends \PHPUnit\Framework\TestCase
$this->assertEquals(3, $request_count);
}
public function testBeforeSendEachRequest()
{
// Ensure MultiCurl::beforeSend() is called before each request including retries.
$multi_curl = new MultiCurl();
$multi_curl->setOpt(CURLOPT_COOKIEJAR, '/dev/null');
$multi_curl->setHeader('X-DEBUG-TEST', 'retry');
$multi_curl->setRetry(5);
$before_send_call_count = 0;
$multi_curl->beforeSend(function ($instance) use (&$before_send_call_count) {
$before_send_call_count += 1;
});
$instance = $multi_curl->addGet(Test::TEST_URL, ['failures' => 5]);
$multi_curl->start();
$this->assertEquals(6, $before_send_call_count);
$this->assertEquals(6, $instance->attempts);
$this->assertEquals(5, $instance->retries);
$this->assertFalse($instance->error);
}
}