-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathClientTest.php
268 lines (222 loc) · 8.63 KB
/
ClientTest.php
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
<?php
namespace SendGrid\Test;
use PHPUnit\Framework\TestCase;
use SendGrid\Client;
use SendGrid\Exception\InvalidRequest;
class ClientTest extends TestCase
{
/** @var MockClient */
private $client;
/** @var string */
private $host;
/** @var array */
private $headers;
protected function setUp(): void
{
$this->host = 'https://localhost:4010';
$this->headers = [
'Content-Type: application/json',
'Authorization: Bearer SG.XXXX'
];
$this->client = new MockClient($this->host, $this->headers, '/v3');
}
public function testConstructor()
{
$this->assertEquals($this->host, $this->client->getHost());
$this->assertEquals($this->headers, $this->client->getHeaders());
$this->assertEquals('/v3', $this->client->getVersion());
$this->assertEquals([], $this->client->getPath());
$this->assertEquals([], $this->client->getCurlOptions());
}
public function test_()
{
$client = new MockClient($this->host, $this->headers, '/v3');
$client->setCurlOptions(['foo' => 'bar']);
$client = $client->_('test');
$this->assertEquals(['test'], $client->getPath());
$this->assertEquals(['foo' => 'bar'], $client->getCurlOptions());
}
public function test__call()
{
$client = $this->client->get();
$this->assertEquals('https://localhost:4010/v3/', $client->url);
$queryParams = ['limit' => 100, 'offset' => 0];
$client = $this->client->get(null, $queryParams);
$this->assertEquals('https://localhost:4010/v3/?limit=100&offset=0', $client->url);
$requestBody = ['name' => 'A New Hope'];
$client = $this->client->get($requestBody);
$this->assertEquals($requestBody, $client->requestBody);
$requestHeaders = ['X-Mock: 200'];
$client = $this->client->get(null, null, $requestHeaders);
$this->assertEquals($requestHeaders, $client->requestHeaders);
$client = $this->client->version('/v4');
$this->assertEquals('/v4', $client->getVersion());
$client = $this->client->path_to_endpoint();
$this->assertEquals(['path_to_endpoint'], $client->getPath());
$client = $client->one_more_segment();
$this->assertEquals(['path_to_endpoint', 'one_more_segment'], $client->getPath());
}
public function test__callWithEndpointVersion()
{
$endpoint_version = '2.0';
$object_id = '00000000-0000-0000-0000-000000000000';
$response = $this->client->segments()->_version($endpoint_version)->_($object_id)->patch();
$this->assertEquals("https://localhost:4010/v3/segments/$endpoint_version/$object_id", $response->url);
}
public function testGetHost()
{
$client = new Client('https://localhost:4010');
$this->assertSame('https://localhost:4010', $client->getHost());
}
public function testSetHost()
{
$client = new Client('https://localhost:4010');
$client->setHost("https://api.test.com");
$this->assertSame('https://api.test.com', $client->getHost());
}
public function testGetHeaders()
{
$client = new Client(
'https://localhost:4010',
['Content-Type: application/json', 'Authorization: Bearer SG.XXXX']
);
$this->assertSame(['Content-Type: application/json', 'Authorization: Bearer SG.XXXX'], $client->getHeaders());
$client2 = new Client('https://localhost:4010');
$this->assertSame([], $client2->getHeaders());
}
public function testGetVersion()
{
$client = new Client('https://localhost:4010', [], '/v3');
$this->assertSame('/v3', $client->getVersion());
$client = new Client('https://localhost:4010');
$this->assertNull($client->getVersion());
}
public function testGetPath()
{
$client = new Client('https://localhost:4010', [], null, ['/foo/bar']);
$this->assertSame(['/foo/bar'], $client->getPath());
$client = new Client('https://localhost:4010');
$this->assertSame([], $client->getPath());
}
public function testGetCurlOptions()
{
$client = new Client('https://localhost:4010');
$client->setCurlOptions([CURLOPT_PROXY => '127.0.0.1:8080']);
$this->assertSame([CURLOPT_PROXY => '127.0.0.1:8080'], $client->getCurlOptions());
$client = new Client('https://localhost:4010');
$this->assertSame([], $client->getCurlOptions());
}
public function testCurlMulti()
{
$client = new Client('https://localhost:4010');
$client->setIsConcurrentRequest(true);
$client->get(['name' => 'A New Hope']);
$client->get(null, null, ['X-Mock: 200']);
$client->get(null, ['limit' => 100, 'offset' => 0]);
// returns 3 response object
$this->assertCount(3, $client->send());
}
public function testCreateCurlOptionsWithMethodOnly()
{
$client = new Client('https://localhost:4010');
$result = $this->callMethod($client, 'createCurlOptions', ['get']);
$this->assertEquals([
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_FAILONERROR => false,
CURLOPT_HTTPHEADER => []
], $result);
}
public function testCreateCurlOptionsWithBody()
{
$client = new Client('https://localhost:4010', ['User-Agent: Custom-Client 1.0']);
$client->setCurlOptions([
CURLOPT_ENCODING => 'utf-8'
]);
$body = ['foo' => 'bar'];
$result = $this->callMethod($client, 'createCurlOptions', ['post', $body]);
$this->assertEquals([
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_FAILONERROR => false,
CURLOPT_ENCODING => 'utf-8',
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => [
'User-Agent: Custom-Client 1.0',
'Content-Type: application/json'
]
], $result);
}
public function testCreateCurlOptionsWithBodyAndHeaders()
{
$client = new Client('https://localhost:4010', ['User-Agent: Custom-Client 1.0']);
$client->setCurlOptions([
CURLOPT_ENCODING => 'utf-8'
]);
$body = ['foo' => 'bar'];
$headers = ['Accept-Encoding: gzip'];
$result = $this->callMethod($client, 'createCurlOptions', ['post', $body, $headers]);
$this->assertEquals([
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_FAILONERROR => false,
CURLOPT_ENCODING => 'utf-8',
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => [
'User-Agent: Custom-Client 1.0',
'Accept-Encoding: gzip',
'Content-Type: application/json'
]
], $result);
}
public function testThrowExceptionOnInvalidCall()
{
$this->expectException(InvalidRequest::class);
$client = new Client('invalid://url', ['User-Agent: Custom-Client 1.0']);
$client->get();
}
public function testMakeRequestWithUntrustedRootCert()
{
$this->expectException(InvalidRequest::class);
$this->expectExceptionMessageMatches('/certificate/i');
$client = new Client('https://untrusted-root.badssl.com/');
$client->makeRequest('GET', 'https://untrusted-root.badssl.com/');
}
public function testFormRepeatUrlArgs()
{
$client = new Client('https://localhost:4010');
$testParams = [
'thing' => 'stuff',
'foo' => [
'bar',
'bat',
'baz',
],
];
$result = $this->callMethod($client, 'buildUrl', [$testParams]);
$this->assertEquals('https://localhost:4010/?thing=stuff&foo=bar&foo=bat&foo=baz', $result);
}
/**
* @param object $obj
* @param string $name
* @param array $args
* @return mixed
*/
private function callMethod($obj, $name, $args = [])
{
try {
$class = new \ReflectionClass($obj);
} catch (\ReflectionException $e) {
return null;
}
$method = $class->getMethod($name);
$method->setAccessible(true);
return $method->invokeArgs($obj, $args);
}
}