Skip to content

Commit

Permalink
Merge pull request #1632 from stripe/richardm-rawrequest-usage
Browse files Browse the repository at this point in the history
Beta: report usage of `rawRequest`
  • Loading branch information
richardm-stripe authored Jan 16, 2024
2 parents 42c2288 + 889f459 commit 84dd136
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/BaseStripeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function rawRequest($method, $path, $params = null, $opts = [])
$opts->headers = \array_merge($opts->headers, $headers);
$baseUrl = $opts->apiBase ?: $this->getApiBase();
$requestor = new \Stripe\ApiRequestor($this->apiKeyForRequest($opts), $baseUrl);
list($response) = $requestor->request($method, $path, $params, $opts->headers, $apiMode);
list($response) = $requestor->request($method, $path, $params, $opts->headers, $apiMode, ['raw_request']);

return $response;
}
Expand Down
37 changes: 37 additions & 0 deletions tests/Stripe/BaseStripeClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ final class BaseStripeClientTest extends \Stripe\TestCase
/** @var \ReflectionProperty */
private $optsReflector;

/** @var \ReflectionClass */
private $apiRequestorReflector;

protected function headerStartsWith($header, $name)
{
return substr($header, 0, \strlen($name)) === $name;
Expand All @@ -26,6 +29,12 @@ protected function setUpOptsReflector()
$this->optsReflector->setAccessible(true);
}

/** @before */
protected function setUpApiRequestorReflector()
{
$this->apiRequestorReflector = new \ReflectionClass(\Stripe\ApiRequestor::class);
}

public function testCtorDoesNotThrowWhenNoParams()
{
$client = new BaseStripeClient();
Expand Down Expand Up @@ -273,6 +282,34 @@ public function testJsonRawRequestGetWithURLParams()
static::assertSame('Stripe-Version: ' . ApiVersion::CURRENT, $stripe_version);
}

public function testRawRequestUsageTelemetry()
{
$curlClientStub = $this->getMockBuilder(\Stripe\HttpClient\CurlClient::class)
->setMethods(['executeRequestWithRetries'])
->getMock()
;
$curlClientStub->method('executeRequestWithRetries')
->willReturn(['{}', 200, ['request-id' => 'req_123']])
;

$curlClientStub->expects(static::once())
->method('executeRequestWithRetries')
->with(static::callback(function ($opts) {
return true;
}), MOCK_URL . '/v1/xyz')
;
ApiRequestor::setHttpClient($curlClientStub);
$client = new BaseStripeClient([
'api_key' => 'sk_test_client',
'api_base' => MOCK_URL,
]);
$client->rawRequest('post', '/v1/xyz', [], [
'api_mode' => 'standard',
]);
// Can't use ->getStaticPropertyValue because this has a bug until PHP 7.4.9: https://bugs.php.net/bug.php?id=69804
static::assertSame(['raw_request'], $this->apiRequestorReflector->getStaticProperties()['requestTelemetry']->usage);
}

public function testJsonRawRequestPost()
{
$curlClientStub = $this->getMockBuilder(\Stripe\HttpClient\CurlClient::class)
Expand Down

0 comments on commit 84dd136

Please sign in to comment.