Skip to content

Commit

Permalink
Fix header persistence on Collection objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Feb 11, 2018
1 parent 78ef053 commit 58c092e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 13 deletions.
6 changes: 1 addition & 5 deletions lib/ApiOperations/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ protected static function _staticRequest($method, $url, $params, $options)
$opts = \Stripe\Util\RequestOptions::parse($options);
$requestor = new \Stripe\ApiRequestor($opts->apiKey, static::baseUrl());
list($response, $opts->apiKey) = $requestor->request($method, $url, $params, $opts->headers);
foreach ($opts->headers as $k => $v) {
if (!array_key_exists($k, self::$HEADERS_TO_PERSIST)) {
unset($opts->headers[$k]);
}
}
$opts->discardNonPersistentHeaders();
return [$response, $opts];
}
}
8 changes: 0 additions & 8 deletions lib/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ public static function getSavedNestedResources()
return $savedNestedResources;
}

/**
* @var array A list of headers that should be persisted across requests.
*/
private static $HEADERS_TO_PERSIST = [
'Stripe-Account' => true,
'Stripe-Version' => true
];

/**
* @var boolean A flag that can be set a behavior that will cause this
* resource to be encoded and sent up along with an update of its parent
Expand Down
20 changes: 20 additions & 0 deletions lib/Util/RequestOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

class RequestOptions
{
/**
* @var array A list of headers that should be persisted across requests.
*/
public static $HEADERS_TO_PERSIST = [
'Stripe-Account',
'Stripe-Version',
];

public $headers;
public $apiKey;

Expand All @@ -32,6 +40,18 @@ public function merge($options)
return $other_options;
}

/**
* Discards all headers that we don't want to persist across requests.
*/
public function discardNonPersistentHeaders()
{
foreach ($this->headers as $k => $v) {
if (!in_array($k, self::$HEADERS_TO_PERSIST)) {
unset($this->headers[$k]);
}
}
}

/**
* Unpacks an options array into an RequestOptions object
* @param array|string|null $options a key => value array
Expand Down
26 changes: 26 additions & 0 deletions tests/Stripe/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,30 @@ public function testSupportsIteratorToArray()

$this->assertSame([1, 2, 3], $seen);
}

public function testHeaders()
{
$this->stubRequest(
'POST',
'/things',
[
'foo' => 'bar',
],
[
'Stripe-Account: acct_foo',
'Idempotency-Key: qwertyuiop',
],
false,
[
'id' => 2,
]
);

$this->fixture->create([
'foo' => 'bar',
], [
'stripe_account' => 'acct_foo',
'idempotency_key' => 'qwertyuiop',
]);
}
}
12 changes: 12 additions & 0 deletions tests/Stripe/Util/RequestOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ public function testWrongType()
{
$opts = Util\RequestOptions::parse(5);
}

public function testDiscardNonPersistentHeaders()
{
$opts = Util\RequestOptions::parse(
[
'stripe_account' => 'foo',
'idempotency_key' => 'foo',
]
);
$opts->discardNonPersistentHeaders();
$this->assertSame(['Stripe-Account' => 'foo'], $opts->headers);
}
}

0 comments on commit 58c092e

Please sign in to comment.