-
Notifications
You must be signed in to change notification settings - Fork 853
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
77eb5bb
commit b03f398
Showing
7 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
/** | ||
* Class RequestTelemetry | ||
* | ||
* Tracks client request telemetry | ||
* @package Stripe | ||
*/ | ||
class RequestTelemetry | ||
{ | ||
public $requestId; | ||
public $requestDuration; | ||
|
||
public function __construct($requestId, $requestDuration) | ||
{ | ||
$this->requestId = $requestId; | ||
$this->requestDuration = $requestDuration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
class StripeTelemetryTest extends TestCase | ||
{ | ||
const TEST_RESOURCE_ID = 'acct_123'; | ||
const TEST_EXTERNALACCOUNT_ID = 'ba_123'; | ||
const TEST_PERSON_ID = 'person_123'; | ||
|
||
const FAKE_VALID_RESPONSE = '{ | ||
"data": [], | ||
"has_more": false, | ||
"object": "list", | ||
"url": "/v1/accounts" | ||
}'; | ||
|
||
protected function setUp() | ||
{ | ||
parent::setUp(); | ||
|
||
// clear static telemetry data | ||
ApiRequestor::resetTelemetry(); | ||
} | ||
|
||
|
||
public function testNoTelemetrySentIfNotEnabled() | ||
{ | ||
$requestheaders = null; | ||
|
||
$stub = $this | ||
->getMockBuilder("HttpClient\ClientInterface") | ||
->setMethods(array('request')) | ||
->getMock(); | ||
|
||
$stub->expects($this->any()) | ||
->method("request") | ||
->with( | ||
$this->anything(), | ||
$this->anything(), | ||
$this->callback(function ($headers) use (&$requestheaders) { | ||
foreach ($headers as $index => $header) { | ||
// capture the requested headers and format back to into an assoc array | ||
$components = explode(": ", $header, 2); | ||
$requestheaders[$components[0]] = $components[1]; | ||
} | ||
|
||
return true; | ||
}), | ||
$this->anything(), | ||
$this->anything() | ||
)->willReturn(array(self::FAKE_VALID_RESPONSE, 200, ["request-id" => "123"])); | ||
|
||
ApiRequestor::setHttpClient($stub); | ||
|
||
// make one request to capture its result | ||
Charge::all(); | ||
$this->assertArrayNotHasKey('X-Stripe-Client-Telemetry', $requestheaders); | ||
|
||
// make another request and verify telemetry isn't sent | ||
Charge::all(); | ||
$this->assertArrayNotHasKey('X-Stripe-Client-Telemetry', $requestheaders); | ||
|
||
ApiRequestor::setHttpClient(null); | ||
} | ||
|
||
public function testTelemetrySetIfEnabled() | ||
{ | ||
Stripe::setEnableClientTelemetry(true); | ||
|
||
$requestheaders = null; | ||
|
||
$stub = $this | ||
->getMockBuilder("HttpClient\ClientInterface") | ||
->setMethods(array('request')) | ||
->getMock(); | ||
|
||
$stub->expects($this->any()) | ||
->method("request") | ||
->with( | ||
$this->anything(), | ||
$this->anything(), | ||
$this->callback(function ($headers) use (&$requestheaders) { | ||
// capture the requested headers and format back to into an assoc array | ||
foreach ($headers as $index => $header) { | ||
$components = explode(": ", $header, 2); | ||
$requestheaders[$components[0]] = $components[1]; | ||
} | ||
|
||
return true; | ||
}), | ||
$this->anything(), | ||
$this->anything() | ||
)->willReturn(array(self::FAKE_VALID_RESPONSE, 200, ["request-id" => "123"])); | ||
|
||
ApiRequestor::setHttpClient($stub); | ||
|
||
// make one request to capture its result | ||
Charge::all(); | ||
$this->assertArrayNotHasKey('X-Stripe-Client-Telemetry', $requestheaders); | ||
|
||
// make another request to send the previous | ||
Charge::all(); | ||
$this->assertArrayHasKey('X-Stripe-Client-Telemetry', $requestheaders); | ||
|
||
$data = json_decode($requestheaders['X-Stripe-Client-Telemetry'], true); | ||
$this->assertEquals('123', $data['last_request_metrics']['request_id']); | ||
$this->assertNotNull($data['last_request_metrics']['request_duration_ms']); | ||
|
||
ApiRequestor::setHttpClient(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters