1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
<?php
namespace Twilio\Tests\Unit\Http;
use Twilio\Http\CurlClient;
use Twilio\Tests\Unit\UnitTest;
class CurlClientTest extends UnitTest {
public function testPreemptiveAuthorization() {
$client = new CurlClient();
$options = $client->options(
'GET',
'http://api.twilio.com',
array(),
array(),
array(),
'test-user',
'test-password'
);
$this->assertArrayHasKey(CURLOPT_HTTPHEADER, $options);
$headers = $options[CURLOPT_HTTPHEADER];
$authorization = null;
foreach ($headers as $header) {
$parse = explode(':', $header);
$headerKey = $parse[0];
if ($headerKey == 'Authorization') {
$authorization = $header;
break;
}
}
$this->assertNotNull($authorization);
$authorizationPayload = explode(' ', $authorization);
$encodedAuthorization = array_pop($authorizationPayload);
$decodedAuthorization = base64_decode($encodedAuthorization);
$this->assertEquals('test-user:test-password', $decodedAuthorization);
}
/**
* @param string $message Failure Message
* @param mixed[] $params Params with which to build the query
* @param string $expected Expected query string
* @dataProvider buildQueryProvider
*/
public function testBuildQuery($message, $params, $expected) {
$client = new CurlClient();
$actual = $client->buildQuery($params);
$this->assertEquals($expected, $actual, $message);
}
public function buildQueryProvider() {
return array(
array(
'Null Params',
null,
''
),
array(
'Empty Params',
array(),
'',
),
array(
'Single Scalar',
array('a' => 'z'),
'a=z',
),
array(
'Multiple Scalars',
array(
'a' => 'z',
'b' => 'y',
),
'a=z&b=y',
),
array(
'Type Coercion: Booleans',
array(
'a' => true,
'b' => false,
),
'a=1&b=',
),
array(
'Type Coercion: Integers',
array(
'a' => 7,
'b' => -14,
'c' => 0,
),
'a=7&b=-14&c=0',
),
array(
'Nested Arrays',
array(
'a' => array(1, 2, 3),
'b' => array('x', 'y', 'z'),
),
'a=1&a=2&a=3&b=x&b=y&b=z',
),
array(
'URL Safety',
array(
'a' => 'un$afe:// value!',
),
'a=un%24afe%3A%2F%2F+value%21',
),
array(
'Encoded Key',
array(
'StartTime>' => '2012-06-14',
),
'StartTime%3E=2012-06-14',
)
);
}
/**
* @param $method
* @param $params
* @param $expected
* @dataProvider queryStringProvider
* @throws \Twilio\Exceptions\EnvironmentException
*/
public function testQueryString($method, $params, $expected) {
$client = new CurlClient();
$actual = $client->options($method, 'url', $params);
$this->assertEquals($expected, $actual[CURLOPT_URL]);
}
public function queryStringProvider() {
$methods = array('GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'HEAD', 'CUSTOM');
$cases = array();
foreach ($methods as $method) {
$cases[] = array(
$method,
array(),
'url',
);
$cases[] = array(
$method,
array(
'a' => '$z',
'b' => 7,
'c' => array(1, 'x', 2),
),
'url?a=%24z&b=7&c=1&c=x&c=2',
);
}
return $cases;
}
/**
* @param mixed[] $params Parameters to post
* @param mixed[] $data Data to post
* @param string $expected Expected POSTFIELDS
* @dataProvider postFieldsProvider
* @throws \Twilio\Exceptions\EnvironmentException
*/
public function testPostFields($params, $data, $expected) {
$client = new CurlClient();
$actual = $client->options('POST', 'url', $params, $data);
$this->assertEquals($expected, $actual[CURLOPT_POSTFIELDS]);
}
public function postFieldsProvider() {
return array(
array(
array(),
array(),
'',
),
array(
array(
'a' => 'x',
),
array(
'a' => 'b',
),
'a=b'
),
array(
array(
'a' => 'x',
),
array(
'a' => 'x',
),
'a=x'
),
array(
array(
'a' => 'x',
),
array(
'a' => 'z',
'b' => 7,
'c' => array(1, 2, 3),
),
'a=z&b=7&c=1&c=2&c=3',
),
);
}
public function testPutFile() {
$client = new CurlClient();
$actual = $client->options('PUT', 'url', array(), array('a' => 1, 'b' => 2));
$this->assertNotNull($actual[CURLOPT_INFILE]);
$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'),
),
),
),
);
}
}
|