Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix header persistence on Collection objects #435

Merged
merged 1 commit into from
Feb 12, 2018
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
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);
}
}