-
Notifications
You must be signed in to change notification settings - Fork 9
/
HttpServiceTest.php
524 lines (497 loc) · 16.2 KB
/
HttpServiceTest.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
<?php
/**
* Proxy service unit test.
*
* PHP version 7
*
* Copyright (C) Villanova University 2010.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Tests
* @author David Maus <maus@hab.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development
*/
namespace VuFindTest;
use VuFindHttp\HttpService as Service;
/**
* Proxy service unit test.
*
* PHP version 5
*
* Copyright (C) Villanova University 2010.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Tests
* @author David Maus <maus@hab.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development
*/
class ProxyServiceTest extends \PHPUnit\Framework\TestCase
{
/**
* Example representations of localhost.
*
* @var array
*/
protected $local = ['ipv4 localhost' => 'http://localhost',
'ipv4 loopback' => 'http://127.0.0.1',
'ipv6 loopback' => 'http://[::1]'];
/**
* Example custom regular expression for extended local server detection.
*
* @var string
*/
protected $localAddressesRegEx = '@^(localhost|127(\.\d+){3}|\[::1\]|([a-z])+\.internal)@';
/**
* Test GET request with associative array.
*
* @return void
*/
public function testGetWithAssocParams()
{
$service = new Service();
$adapter = $this->getMockBuilder('Laminas\Http\Client\Adapter\Test')
->setMethods(['write'])->getMock();
$adapter->expects($this->once())
->method('write')
->with(
$this->equalTo('GET'),
$this->equalTo(
new \Laminas\Uri\Http('http://example.tld?foo=bar&bar%5B0%5D=baz')
)
);
$service->setDefaultAdapter($adapter);
$service->get('http://example.tld', ['foo' => 'bar', 'bar' => ['baz']]);
}
/**
* Test GET request with parameter pairs.
*
* @return void
*/
public function testGetWithParamPairs()
{
$service = new Service();
$adapter = $this->getMockBuilder('Laminas\Http\Client\Adapter\Test')
->setMethods(['write'])->getMock();
$adapter->expects($this->once())
->method('write')
->with(
$this->equalTo('GET'),
$this->equalTo(
new \Laminas\Uri\Http('http://example.tld?foo=bar&bar=baz&bar=bar')
)
);
$service->setDefaultAdapter($adapter);
$service->get('http://example.tld', ['foo=bar', 'bar=baz', 'bar=bar']);
}
/**
* Test GET request appends query part.
*
* @return void
*/
public function testGetAppendsQueryPart()
{
$service = new Service();
$adapter = $this->getMockBuilder('Laminas\Http\Client\Adapter\Test')
->setMethods(['write'])->getMock();
$adapter->expects($this->once())
->method('write')
->with(
$this->equalTo('GET'),
$this->equalTo(
new \Laminas\Uri\Http('http://example.tld?foo=bar&bar=baz')
)
);
$service->setDefaultAdapter($adapter);
$service->get('http://example.tld?foo=bar', ['bar=baz']);
}
/**
* Test GET request appends headers.
*
* @return void
*/
public function testGetAppendsHeaders()
{
$service = new Service();
$adapter = $this->getMockBuilder('Laminas\Http\Client\Adapter\Test')
->setMethods(['write'])->getMock();
$adapter->expects($this->once())
->method('write')
->with(
$this->equalTo('GET'),
$this->equalTo(
new \Laminas\Uri\Http('http://example.tld?foo=bar')
),
$this->equalTo('1.1'),
$this->equalTo(
[
'Host' => 'example.tld', 'Connection' => 'close', 'Accept-Encoding' => 'gzip, deflate',
'User-Agent' => 'Laminas_Http_Client', 'Content-Type' => 'application/json',
'Accept' => 'application/json',
]
)
);
$service->setDefaultAdapter($adapter);
$service->get(
'http://example.tld',
['foo=bar'],
'test',
['Content-type: application/json', 'Accept: application/json']
);
}
/**
* Test POST request appends headers.
*
* @return void
*/
public function testPostAppendsHeaders()
{
$service = new Service();
$adapter = $this->getMockBuilder('Laminas\Http\Client\Adapter\Test')
->setMethods(['write'])->getMock();
$adapter->expects($this->once())
->method('write')
->with(
$this->equalTo('POST'),
$this->equalTo(
new \Laminas\Uri\Http('http://example.tld')
),
$this->equalTo('1.1'),
$this->equalTo(
[
'Host' => 'example.tld', 'Connection' => 'close', 'Accept-Encoding' => 'gzip, deflate',
'User-Agent' => 'Laminas_Http_Client', 'Content-Type' => 'application/json',
'Accept' => 'application/json', 'Content-Length' => '5',
]
)
);
$service->setDefaultAdapter($adapter);
$service->post('http://example.tld', 'dummy', 'application/json', null, ['Accept: application/json']);
}
/**
* Test POST request of form data.
*
* @return void
*/
public function testPostForm()
{
$service = new Service();
$adapter = $this->getMockBuilder('Laminas\Http\Client\Adapter\Test')
->setMethods(['write'])->getMock();
$adapter->expects($this->once())
->method('write')
->with(
$this->equalTo('POST'),
$this->equalTo(
new \Laminas\Uri\Http('http://example.tld', 'foo=bar&bar=baz')
)
);
$service->setDefaultAdapter($adapter);
$service->postForm('http://example.tld', ['foo=bar']);
}
/**
* Test POST request.with empty body
*
* @return void
*/
public function testSendPostRequestEmptyBody()
{
$service = new Service();
$adapter = $this->getMockBuilder('Laminas\Http\Client\Adapter\Test')
->setMethods(['write'])->getMock();
$adapter->expects($this->once())
->method('write')
->with(
$this->equalTo('POST'),
$this->equalTo(
new \Laminas\Uri\Http('http://example.tld')
)
);
$service->setDefaultAdapter($adapter);
$service->post('http://example.tld');
}
/**
* Test proxify.
*
* @return void
*/
public function testProxify()
{
$service = new Service(
[
'proxy_host' => 'localhost',
'proxy_port' => '666',
]
);
$client = new \Laminas\Http\Client('http://example.tld:8080');
$client = $service->proxify($client);
$adapter = $client->getAdapter();
$this->assertInstanceOf('Laminas\Http\Client\Adapter\Proxy', $adapter);
$config = $adapter->getConfig();
$this->assertEquals('localhost', $config['proxy_host']);
$this->assertEquals('666', $config['proxy_port']);
}
/**
* Test proxify with a Curl adapter.
*
* @return void
*/
public function testProxifyCurlAdapter()
{
$service = new Service(
[
'proxy_host' => 'localhost',
'proxy_port' => '666',
]
);
$service->setDefaultAdapter(new \Laminas\Http\Client\Adapter\Curl());
$client = new \Laminas\Http\Client('http://example.tld:8080');
$client = $service->proxify($client);
$adapter = $client->getAdapter();
$this->assertInstanceOf('Laminas\Http\Client\Adapter\Curl', $adapter);
$config = $adapter->getConfig();
$this->assertEquals('localhost', $config['curloptions'][CURLOPT_PROXY]);
$this->assertEquals('666', $config['curloptions'][CURLOPT_PROXYPORT]);
}
/**
* Test proxify w/SOCKS5 option.
*
* @return void
*/
public function testProxifySocks5()
{
$service = new Service(
[
'proxy_host' => 'localhost',
'proxy_port' => '666',
'proxy_type' => 'socks5',
]
);
$client = new \Laminas\Http\Client('http://example.tld:8080');
$client = $service->proxify($client);
$adapter = $client->getAdapter();
$this->assertInstanceOf('Laminas\Http\Client\Adapter\Curl', $adapter);
$config = $adapter->getConfig();
$this->assertEquals('localhost', $config['curloptions'][CURLOPT_PROXY]);
$this->assertEquals('666', $config['curloptions'][CURLOPT_PROXYPORT]);
$this->assertEquals(
CURLPROXY_SOCKS5,
$config['curloptions'][CURLOPT_PROXYTYPE]
);
$this->assertNotContains(CURLOPT_FOLLOWLOCATION, $config['curloptions']);
}
/**
* Test no proxify with local address.
*
* @return void
*/
public function testNoProxifyLocal()
{
$service = new Service(
[
'proxy_host' => 'localhost',
'proxy_port' => '666',
]
);
foreach ($this->local as $name => $address) {
$client = new \Laminas\Http\Client($address);
$client->setAdapter(new \Laminas\Http\Client\Adapter\Test());
$client = $service->proxify($client);
$this->assertInstanceOf(
'Laminas\Http\Client\Adapter\Test',
$client->getAdapter(),
sprintf('Failed to proxify %s: %s', $name, $address)
);
}
}
/**
* Test that a local custom address does not get proxified when correctly
* configured.
*
* @return void
*/
public function testNoProxifyLocalAddress()
{
$service = new Service(
[
'proxy_host' => 'localhost',
'proxy_port' => '666',
],
[],
[
'localAddressesRegEx' => $this->localAddressesRegEx,
]
);
$localAddress = 'http://solr.internal';
$client = new \Laminas\Http\Client($localAddress);
$client->setAdapter(new \Laminas\Http\Client\Adapter\Test());
$client = $service->proxify($client);
$this->assertInstanceOf(
'Laminas\Http\Client\Adapter\Test',
$client->getAdapter(),
sprintf('Failed to proxify %s', $localAddress)
);
}
/**
* Test that an external address still gets proxified when a custom local
* regular expression is configured.
*
* @return void
*/
public function testProxifyExternalAddress()
{
$service = new Service(
[
'proxy_host' => 'localhost',
'proxy_port' => '666',
],
[],
[
'localAddressesRegEx' => $this->localAddressesRegEx,
]
);
$externalAddress = 'http://solr.external';
$client = new \Laminas\Http\Client($externalAddress);
$client->setAdapter(new \Laminas\Http\Client\Adapter\Test());
$client = $service->proxify($client);
$this->assertInstanceOf(
'Laminas\Http\Client\Adapter\Proxy',
$client->getAdapter(),
sprintf('Failed to proxify %s', $externalAddress)
);
}
/**
* Test for runtime exception.
*
* @return void
*/
public function testRuntimeException()
{
$this->expectException(\VuFindHttp\Exception\RuntimeException::class);
$service = new Service();
$service->get('http://example.tld');
}
/**
* Test isAssocArray with mixed keys.
*
* @return void
*/
public function testIsAssocArrayWithMixedKeys()
{
$arr = [];
$arr[] = 'foo';
$arr['foo'] = 'bar';
$this->assertTrue(Service::isAssocParams($arr));
}
/**
* Test default settings.
*
* @return void
*/
public function testDefaults()
{
$service = new Service([], ['foo' => 'bar']);
$client = $service->createClient();
$clientConfig = $this->getProperty($client, 'config');
$this->assertEquals($clientConfig['foo'], 'bar');
}
/**
* Test timeout setting.
*
* @return void
*/
public function testTimeout()
{
$service = new Service();
$client = $service->createClient(null, \Laminas\Http\Request::METHOD_GET, 67);
$clientConfig = $this->getProperty($client, 'config');
$this->assertEquals($clientConfig['timeout'], 67);
}
/**
* Test defaults with Curl adapter.
*
* @return void
*/
public function testCurlAdapterDefaults()
{
$service = new Service(
[],
['adapter' => '\Laminas\Http\Client\Adapter\Curl']
);
$client = $service->createClient('http://example.tld:8080');
$adapter = $client->getAdapter();
$this->assertInstanceOf('Laminas\Http\Client\Adapter\Curl', $adapter);
$config = $adapter->getConfig();
$this->assertFalse(isset($config['curloptions']));
}
/**
* Test defaults with Curl adapter.
*
* @return void
*/
public function testCurlAdapterFollowLocation()
{
$service = new Service(
[],
[
'adapter' => '\Laminas\Http\Client\Adapter\Curl',
'curloptions' => [
52 => '1',
],
]
);
$client = $service->createClient('http://example.tld:8080');
$adapter = $client->getAdapter();
$this->assertInstanceOf('Laminas\Http\Client\Adapter\Curl', $adapter);
$config = $adapter->getConfig();
$this->assertEquals(
'1',
$config['curloptions'][CURLOPT_FOLLOWLOCATION] ?? null
);
}
/**
* Return protected or private property.
*
* Uses PHP's reflection API in order to modify property accessibility.
*
* @param object|string $object Object or class name
* @param string $property Property name
*
* @throws \ReflectionException Property does not exist
*
* @return mixed
*/
protected function getProperty($object, $property)
{
$reflectionProperty = new \ReflectionProperty($object, $property);
$reflectionProperty->setAccessible(true);
return $reflectionProperty->getValue($object);
}
}