diff --git a/lib/ApiOperations/Request.php b/lib/ApiOperations/Request.php index c83413b02..4339a8374 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 9465f1aad..3e0156e5f 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 adfd2763e..a31f4d322 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 bc07cfa20..560085aed 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 5151e3e06..558f73982 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); + } }