Skip to content
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
7 changes: 6 additions & 1 deletion src/Senders/CurlSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class CurlSender implements SenderInterface
private $maxBatchRequests = 75;
private $batchRequests = array();
private $inflightRequests = array();
private $ipResolve = CURL_IPRESOLVE_V4;

public function __construct($opts)
{
Expand Down Expand Up @@ -54,6 +55,10 @@ public function __construct($opts)
if (array_key_exists('ca_cert_path', $opts)) {
$this->caCertPath = $opts['ca_cert_path'];
}
if (array_key_exists('ip_resolve', $opts)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can we add testing for this scenario?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I added some tests to ensure that the value is set. I usually try to avoid testing private properties since they are generally assumed to be safe to refactor. So, I added a comment to the test in case it fails in the future because of some sort of refactoring.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I approved the PR but it looks like tests are failing.

&& in_array($opts['ip_resolve'], [CURL_IPRESOLVE_V4, CURL_IPRESOLVE_V6, CURL_IPRESOLVE_WHATEVER])) {
$this->ipResolve = $opts['ip_resolve'];
}
}

public function getEndpoint()
Expand Down Expand Up @@ -151,7 +156,7 @@ public function setCurlOptions(CurlHandle $handle, EncodedPayload $payload, stri
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($handle, CURLOPT_HTTPHEADER, array('X-Rollbar-Access-Token: ' . $accessToken));
curl_setopt($handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($handle, CURLOPT_IPRESOLVE, $this->ipResolve);

if (!is_null($this->caCertPath)) {
curl_setopt($handle, CURLOPT_CAINFO, $this->caCertPath);
Expand Down
43 changes: 43 additions & 0 deletions tests/CurlSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Rollbar;

use ReflectionClass;
use ReflectionException;
use Rollbar\Payload\Level;
use Rollbar\Senders\CurlSender;

class CurlSenderTest extends BaseRollbarTest
{
Expand All @@ -26,4 +29,44 @@ public function testCurlError(): void
)
);
}

/**
* This test will fail if the {@see CurlSender::$ipResolve} property is renamed or removed.
*/
public function testIPResolve(): void
{
$sender = new CurlSender([
'ip_resolve' => CURL_IPRESOLVE_V4,
]);
self::assertSame(CURL_IPRESOLVE_V4, self::getPrivateProperty($sender, 'ipResolve'));

$sender = new CurlSender([]);
self::assertSame(CURL_IPRESOLVE_V4, self::getPrivateProperty($sender, 'ipResolve'));

$sender = new CurlSender([
'ip_resolve' => CURL_IPRESOLVE_V6,
]);
self::assertSame(CURL_IPRESOLVE_V6, self::getPrivateProperty($sender, 'ipResolve'));

$sender = new CurlSender([
'ip_resolve' => CURL_IPRESOLVE_WHATEVER,
]);
self::assertSame(CURL_IPRESOLVE_WHATEVER, self::getPrivateProperty($sender, 'ipResolve'));
}

/**
* Returns the value of a private property of an object.
*
* @param object $object The object from which to get the private property.
* @param string $property The name of the private property to get.
* @return mixed
* @throws ReflectionException If the object or property does not exist.
*/
private static function getPrivateProperty(object $object, string $property): mixed
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($property);
$property->setAccessible(true);
return $property->getValue($object);
}
}