From 58c092e0a21716bee7a03924519e0462a44ef90f Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Sun, 11 Feb 2018 11:49:09 -0800 Subject: [PATCH] Fix header persistence on Collection objects --- lib/ApiOperations/Request.php | 6 +----- lib/ApiResource.php | 8 -------- lib/Util/RequestOptions.php | 20 ++++++++++++++++++ tests/Stripe/CollectionTest.php | 26 ++++++++++++++++++++++++ tests/Stripe/Util/RequestOptionsTest.php | 12 +++++++++++ 5 files changed, 59 insertions(+), 13 deletions(-) diff --git a/lib/ApiOperations/Request.php b/lib/ApiOperations/Request.php index c83413b020..4339a83744 100644 --- a/lib/ApiOperations/Request.php +++ b/lib/ApiOperations/Request.php @@ -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]; } } diff --git a/lib/ApiResource.php b/lib/ApiResource.php index 9465f1aad5..3e0156e5f7 100644 --- a/lib/ApiResource.php +++ b/lib/ApiResource.php @@ -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 diff --git a/lib/Util/RequestOptions.php b/lib/Util/RequestOptions.php index adfd2763e1..6ac436a4fc 100644 --- a/lib/Util/RequestOptions.php +++ b/lib/Util/RequestOptions.php @@ -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; @@ -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 diff --git a/tests/Stripe/CollectionTest.php b/tests/Stripe/CollectionTest.php index bc07cfa20e..560085aede 100644 --- a/tests/Stripe/CollectionTest.php +++ b/tests/Stripe/CollectionTest.php @@ -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', + ]); + } } diff --git a/tests/Stripe/Util/RequestOptionsTest.php b/tests/Stripe/Util/RequestOptionsTest.php index 5151e3e064..558f73982e 100644 --- a/tests/Stripe/Util/RequestOptionsTest.php +++ b/tests/Stripe/Util/RequestOptionsTest.php @@ -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); + } }