diff options
author | matt <matt@twilio.com> | 2016-08-18 14:01:34 -0700 |
---|---|---|
committer | matt <matt@twilio.com> | 2016-08-18 14:01:34 -0700 |
commit | 327374679790f12de2f4f77b7e5733e08e72ab14 (patch) | |
tree | 312c68f85ba0d81258aa2125a470ede70659cdb7 | |
parent | f8a4589aa716deea0f1eaf017d667fd4af793cc1 (diff) | |
download | twilio-php-origin/curl-client-options.zip twilio-php-origin/curl-client-options.tar.gz twilio-php-origin/curl-client-options.tar.bz2 |
Add $options to the CurlClientorigin/curl-client-options
Updates tests for $options injection behavior
-rw-r--r-- | Twilio/Http/CurlClient.php | 7 | ||||
-rw-r--r-- | Twilio/Tests/Unit/Http/CurlClientTest.php | 85 |
2 files changed, 91 insertions, 1 deletions
diff --git a/Twilio/Http/CurlClient.php b/Twilio/Http/CurlClient.php index cbb4b0e..6fb2dda 100644 --- a/Twilio/Http/CurlClient.php +++ b/Twilio/Http/CurlClient.php @@ -8,6 +8,11 @@ use Twilio\Exceptions\EnvironmentException; class CurlClient implements Client { const DEFAULT_TIMEOUT = 60; + protected $curlOptions = array(); + + public function __construct(array $options = array()) { + $this->curlOptions = $options; + } public function request($method, $url, $params = array(), $data = array(), $headers = array(), $user = null, $password = null, @@ -71,7 +76,7 @@ class CurlClient implements Client { ? self::DEFAULT_TIMEOUT : $timeout; - $options = array( + $options = $this->curlOptions + array( CURLOPT_URL => $url, CURLOPT_HEADER => true, CURLOPT_RETURNTRANSFER => true, diff --git a/Twilio/Tests/Unit/Http/CurlClientTest.php b/Twilio/Tests/Unit/Http/CurlClientTest.php index b8725e0..1c41c10 100644 --- a/Twilio/Tests/Unit/Http/CurlClientTest.php +++ b/Twilio/Tests/Unit/Http/CurlClientTest.php @@ -229,4 +229,89 @@ class CurlClientTest extends UnitTest { $this->assertEquals('a=1&b=2', fread($actual[CURLOPT_INFILE], $actual[CURLOPT_INFILESIZE])); $this->assertEquals(7, $actual[CURLOPT_INFILESIZE]); } + + /** + * @param string $message Case message, displayed on assertion error + * @param mixed[] $options Options to inject + * @param mixed[] $expected Partial array to expect + * @dataProvider userInjectedOptionsProvider + */ + public function testUserInjectedOptions($message, $options, $expected) { + $client = new CurlClient($options); + $actual = $client->options( + 'GET', + 'url', + array('param-key' => 'param-value'), + array('data-key' => 'data-value'), + array('header-key' => 'header-value'), + 'user', + 'password', + 20 + ); + foreach ($expected as $key => $value) { + $this->assertEquals($value, $actual[$key], $message); + } + } + + public function userInjectedOptionsProvider() { + return array( + array( + 'No Options', + array(), + array(), + ), + array( + 'No Conflict Options', + array( + CURLOPT_VERBOSE => true, + ), + array( + CURLOPT_VERBOSE => true, + ), + ), + array( + 'Options preferred over Defaults', + array( + CURLOPT_TIMEOUT => 1000, + ), + array( + CURLOPT_TIMEOUT => 1000, + ), + ), + array( + 'Required Options can not be injected', + array( + CURLOPT_HTTPGET => false, + ), + array( + CURLOPT_HTTPGET => true, + ), + ), + array( + 'Injected URL decorated with Query String', + array( + CURLOPT_URL => 'user-provided-url', + ), + array( + CURLOPT_URL => 'user-provided-url?param-key=param-value', + ), + ), + array( + 'Injected Headers are additive', + array( + CURLOPT_HTTPHEADER => array( + 'injected-key: injected-value', + ), + ), + array( + CURLOPT_HTTPHEADER => array( + 'injected-key: injected-value', + 'header-key: header-value', + 'Authorization: Basic ' . base64_encode('user:password'), + ), + ), + ), + ); + } + } |