Skip to content

Commit

Permalink
Only use files.stripe.com for file creation requests
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Sep 22, 2018
1 parent 7144314 commit 97de5f4
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 97 deletions.
3 changes: 2 additions & 1 deletion lib/ApiOperations/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
22 changes: 17 additions & 5 deletions lib/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 13 additions & 4 deletions lib/Util/RequestOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}
Expand Down Expand Up @@ -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'];
}
Expand All @@ -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 '
Expand Down
65 changes: 65 additions & 0 deletions tests/Stripe/FileCreationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Stripe;

class FileCreationTest extends TestCase
{
/**
* @before
*/
public function setUpUploadBase()
{
Stripe::$apiUploadBase = Stripe::$apiBase;
Stripe::$apiBase = null;
}

/**
* @after
*/
public function tearDownUploadBase()
{
Stripe::$apiBase = Stripe::$apiUploadBase;
Stripe::$apiUploadBase = 'https://files.stripe.com';
}

public function testIsCreatableWithFileHandle()
{
$this->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);
}
}
39 changes: 0 additions & 39 deletions tests/Stripe/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
65 changes: 65 additions & 0 deletions tests/Stripe/FileUploadCreationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Stripe;

class FileUploadCreationTest extends TestCase
{
/**
* @before
*/
public function setUpUploadBase()
{
Stripe::$apiUploadBase = Stripe::$apiBase;
Stripe::$apiBase = null;
}

/**
* @after
*/
public function tearDownUploadBase()
{
Stripe::$apiBase = Stripe::$apiUploadBase;
Stripe::$apiUploadBase = 'https://files.stripe.com';
}

public function testIsCreatableWithFileHandle()
{
$this->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);
}
}
39 changes: 0 additions & 39 deletions tests/Stripe/FileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
12 changes: 4 additions & 8 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,15 +30,13 @@ 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();

// 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);
Expand All @@ -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);
Expand All @@ -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();
Expand Down

0 comments on commit 97de5f4

Please sign in to comment.