From e32b1b7f143ed83de11fca0e0ec814acb68906d5 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Sat, 22 Sep 2018 22:07:57 +0200 Subject: [PATCH] Only use files.stripe.com for file creation requests --- lib/ApiOperations/Request.php | 3 +- lib/File.php | 22 ++++++-- lib/Stripe.php | 2 +- lib/Util/RequestOptions.php | 17 ++++-- tests/Stripe/FileCreationTest.php | 69 +++++++++++++++++++++++++ tests/Stripe/FileTest.php | 39 -------------- tests/Stripe/FileUploadCreationTest.php | 69 +++++++++++++++++++++++++ tests/Stripe/FileUploadTest.php | 39 -------------- tests/TestCase.php | 12 ++--- 9 files changed, 175 insertions(+), 97 deletions(-) create mode 100644 tests/Stripe/FileCreationTest.php create mode 100644 tests/Stripe/FileUploadCreationTest.php diff --git a/lib/ApiOperations/Request.php b/lib/ApiOperations/Request.php index c6b06585a..dd048dc5f 100644 --- a/lib/ApiOperations/Request.php +++ b/lib/ApiOperations/Request.php @@ -52,7 +52,8 @@ protected function _request($method, $url, $params = [], $options = null) protected static function _staticRequest($method, $url, $params, $options) { $opts = \Stripe\Util\RequestOptions::parse($options); - $requestor = new \Stripe\ApiRequestor($opts->apiKey, static::baseUrl()); + $baseUrl = isset($opts->apiBase) ? $opts->apiBase : static::baseUrl(); + $requestor = new \Stripe\ApiRequestor($opts->apiKey, $baseUrl); list($response, $opts->apiKey) = $requestor->request($method, $url, $params, $opts->headers); $opts->discardNonPersistentHeaders(); return [$response, $opts]; diff --git a/lib/File.php b/lib/File.php index 21413cc12..dd1c55993 100644 --- a/lib/File.php +++ b/lib/File.php @@ -26,17 +26,29 @@ class File extends ApiResource const OBJECT_NAME_ALT = "file_upload"; use ApiOperations\All; - use ApiOperations\Create; + use ApiOperations\Create { + create as protected _create; + } use ApiOperations\Retrieve; - public static function baseUrl() + public static function classUrl() { - return Stripe::$apiUploadBase; + return '/v1/files'; } - public static function classUrl() + /** + * @param array|null $params + * @param array|string|null $options + * + * @return \Stripe\File The created resource. + */ + public static function create($params = null, $options = null) { - return '/v1/files'; + $opts = \Stripe\Util\RequestOptions::parse($options); + if (is_null($opts->apiBase)) { + $opts->apiBase = Stripe::$apiUploadBase; + } + return static::_create($params, $opts); } } diff --git a/lib/Stripe.php b/lib/Stripe.php index d861106af..2005143d6 100644 --- a/lib/Stripe.php +++ b/lib/Stripe.php @@ -22,7 +22,7 @@ class Stripe public static $connectBase = 'https://connect.stripe.com'; // @var string The base URL for the Stripe API uploads endpoint. - public static $apiUploadBase = 'https://uploads.stripe.com'; + public static $apiUploadBase = 'https://files.stripe.com'; // @var string|null The version of the Stripe API to use for requests. public static $apiVersion = null; diff --git a/lib/Util/RequestOptions.php b/lib/Util/RequestOptions.php index a31f4d322..495236224 100644 --- a/lib/Util/RequestOptions.php +++ b/lib/Util/RequestOptions.php @@ -16,11 +16,13 @@ class RequestOptions public $headers; public $apiKey; + public $apiBase; - public function __construct($key = null, $headers = []) + public function __construct($key = null, $headers = [], $base = null) { $this->apiKey = $key; $this->headers = $headers; + $this->apiBase = $base; } /** @@ -36,6 +38,9 @@ public function merge($options) if ($other_options->apiKey === null) { $other_options->apiKey = $this->apiKey; } + if ($other_options->apiBase === null) { + $other_options->apiBase = $this->apiBase; + } $other_options->headers = array_merge($this->headers, $other_options->headers); return $other_options; } @@ -65,16 +70,17 @@ public static function parse($options) } if (is_null($options)) { - return new RequestOptions(null, []); + return new RequestOptions(null, [], null); } if (is_string($options)) { - return new RequestOptions($options, []); + return new RequestOptions($options, [], null); } if (is_array($options)) { $headers = []; $key = null; + $base = null; if (array_key_exists('api_key', $options)) { $key = $options['api_key']; } @@ -87,7 +93,10 @@ public static function parse($options) if (array_key_exists('stripe_version', $options)) { $headers['Stripe-Version'] = $options['stripe_version']; } - return new RequestOptions($key, $headers); + if (array_key_exists('api_base', $options)) { + $base = $options['api_base']; + } + return new RequestOptions($key, $headers, $base); } $message = 'The second argument to Stripe API method calls is an ' diff --git a/tests/Stripe/FileCreationTest.php b/tests/Stripe/FileCreationTest.php new file mode 100644 index 000000000..9a65452c6 --- /dev/null +++ b/tests/Stripe/FileCreationTest.php @@ -0,0 +1,69 @@ +expectsRequest( + 'post', + '/v1/files', + null, + ['Content-Type: multipart/form-data'], + true, + Stripe::$apiUploadBase + ); + $fp = fopen(dirname(__FILE__) . '/../data/test.png', 'r'); + $resource = File::create([ + "purpose" => "dispute_evidence", + "file" => $fp, + ]); + $this->assertInstanceOf("Stripe\\File", $resource); + } + + public function testIsCreatableWithCurlFile() + { + if (!class_exists('\CurlFile', false)) { + // Older PHP versions don't support this + return; + } + + $this->expectsRequest( + 'post', + '/v1/files', + null, + ['Content-Type: multipart/form-data'], + true, + Stripe::$apiUploadBase + ); + $curlFile = new \CurlFile(dirname(__FILE__) . '/../data/test.png'); + $resource = File::create([ + "purpose" => "dispute_evidence", + "file" => $curlFile, + ]); + $this->assertInstanceOf("Stripe\\File", $resource); + } +} diff --git a/tests/Stripe/FileTest.php b/tests/Stripe/FileTest.php index b17936206..5ea89184c 100644 --- a/tests/Stripe/FileTest.php +++ b/tests/Stripe/FileTest.php @@ -27,45 +27,6 @@ public function testIsRetrievable() $this->assertInstanceOf("Stripe\\File", $resource); } - public function testIsCreatableWithFileHandle() - { - $this->expectsRequest( - 'post', - '/v1/files', - null, - ['Content-Type: multipart/form-data'], - true - ); - $fp = fopen(dirname(__FILE__) . '/../data/test.png', 'r'); - $resource = File::create([ - "purpose" => "dispute_evidence", - "file" => $fp, - ]); - $this->assertInstanceOf("Stripe\\File", $resource); - } - - public function testIsCreatableWithCurlFile() - { - if (!class_exists('\CurlFile', false)) { - // Older PHP versions don't support this - return; - } - - $this->expectsRequest( - 'post', - '/v1/files', - null, - ['Content-Type: multipart/form-data'], - true - ); - $curlFile = new \CurlFile(dirname(__FILE__) . '/../data/test.png'); - $resource = File::create([ - "purpose" => "dispute_evidence", - "file" => $curlFile, - ]); - $this->assertInstanceOf("Stripe\\File", $resource); - } - public function testDeserializesFromFile() { $obj = Util\Util::convertToStripeObject([ diff --git a/tests/Stripe/FileUploadCreationTest.php b/tests/Stripe/FileUploadCreationTest.php new file mode 100644 index 000000000..8d5bd870e --- /dev/null +++ b/tests/Stripe/FileUploadCreationTest.php @@ -0,0 +1,69 @@ +expectsRequest( + 'post', + '/v1/files', + null, + ['Content-Type: multipart/form-data'], + true, + Stripe::$apiUploadBase + ); + $fp = fopen(dirname(__FILE__) . '/../data/test.png', 'r'); + $resource = FileUpload::create([ + "purpose" => "dispute_evidence", + "file" => $fp, + ]); + $this->assertInstanceOf("Stripe\\FileUpload", $resource); + } + + public function testIsCreatableWithCurlFile() + { + if (!class_exists('\CurlFile', false)) { + // Older PHP versions don't support this + return; + } + + $this->expectsRequest( + 'post', + '/v1/files', + null, + ['Content-Type: multipart/form-data'], + true, + Stripe::$apiUploadBase + ); + $curlFile = new \CurlFile(dirname(__FILE__) . '/../data/test.png'); + $resource = FileUpload::create([ + "purpose" => "dispute_evidence", + "file" => $curlFile, + ]); + $this->assertInstanceOf("Stripe\\FileUpload", $resource); + } +} diff --git a/tests/Stripe/FileUploadTest.php b/tests/Stripe/FileUploadTest.php index 889fa1048..4b23de67f 100644 --- a/tests/Stripe/FileUploadTest.php +++ b/tests/Stripe/FileUploadTest.php @@ -27,45 +27,6 @@ public function testIsRetrievable() $this->assertInstanceOf("Stripe\\FileUpload", $resource); } - public function testIsCreatableWithFileHandle() - { - $this->expectsRequest( - 'post', - '/v1/files', - null, - ['Content-Type: multipart/form-data'], - true - ); - $fp = fopen(dirname(__FILE__) . '/../data/test.png', 'r'); - $resource = FileUpload::create([ - "purpose" => "dispute_evidence", - "file" => $fp, - ]); - $this->assertInstanceOf("Stripe\\FileUpload", $resource); - } - - public function testIsCreatableWithCurlFile() - { - if (!class_exists('\CurlFile', false)) { - // Older PHP versions don't support this - return; - } - - $this->expectsRequest( - 'post', - '/v1/files', - null, - ['Content-Type: multipart/form-data'], - true - ); - $curlFile = new \CurlFile(dirname(__FILE__) . '/../data/test.png'); - $resource = FileUpload::create([ - "purpose" => "dispute_evidence", - "file" => $curlFile, - ]); - $this->assertInstanceOf("Stripe\\FileUpload", $resource); - } - public function testDeserializesFromFile() { $obj = Util\Util::convertToStripeObject([ diff --git a/tests/TestCase.php b/tests/TestCase.php index b013003f0..93be3d128 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -13,9 +13,6 @@ class TestCase extends \PHPUnit_Framework_TestCase /** @var string original API key */ protected $origApiKey; - /** @var string original upload base URL */ - protected $origApiUploadBase; - /** @var string original client ID */ protected $origClientId; @@ -33,7 +30,6 @@ protected function setUp() // Save original values so that we can restore them after running tests $this->origApiBase = Stripe::$apiBase; $this->origApiKey = Stripe::getApiKey(); - $this->origApiUploadBase = Stripe::$apiUploadBase; $this->origClientId = Stripe::getClientId(); $this->origApiVersion = Stripe::getApiVersion(); $this->origAccountId = Stripe::getAccountId(); @@ -41,7 +37,6 @@ protected function setUp() // Set up host and credentials for stripe-mock Stripe::$apiBase = "http://localhost:" . MOCK_PORT; Stripe::setApiKey("sk_test_123"); - Stripe::$apiUploadBase = "http://localhost:" . MOCK_PORT; Stripe::setClientId("ca_123"); Stripe::setApiVersion(null); Stripe::setAccountId(null); @@ -58,7 +53,6 @@ protected function tearDown() // Restore original values Stripe::$apiBase = $this->origApiBase; Stripe::setApiKey($this->origApiKey); - Stripe::$apiUploadBase = $this->origApiUploadBase; Stripe::setClientId($this->origClientId); Stripe::setApiVersion($this->origApiVersion); Stripe::setAccountId($this->origAccountId); @@ -76,15 +70,17 @@ protected function tearDown() * exhaustive. If null, headers are not checked. * @param bool $hasFile Whether the request parameters contains a file. * Defaults to false. + * @param string|null $base base URL (e.g. 'https://api.stripe.com') */ protected function expectsRequest( $method, $path, $params = null, $headers = null, - $hasFile = false + $hasFile = false, + $base = null ) { - $this->prepareRequestMock($method, $path, $params, $headers, $hasFile) + $this->prepareRequestMock($method, $path, $params, $headers, $hasFile, $base) ->will($this->returnCallback( function ($method, $absUrl, $headers, $params, $hasFile) { $curlClient = HttpClient\CurlClient::instance();