Passed
Push — master ( ebcf65...c803c8 )
by Gaetano
06:00
created

HTTPTest::testHttpsSocket()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 6
nop 1
dl 0
loc 36
rs 9.2248
c 0
b 0
f 0
1
<?php
2
3
include_once __DIR__ . '/../lib/xmlrpc.inc';
4
include_once __DIR__ . '/../lib/xmlrpc_wrappers.inc';
5
6
include_once __DIR__ . '/parse_args.php';
7
8
include_once __DIR__ . '/08ServerTest.php';
9
10
/**
11
 * Tests which stress http features of the library.
12
 * Each of these tests iterates over (almost) all the 'Server' tests
13
 */
14
class HTTPTest extends ServerTest
15
{
16
    protected $expectHttp2 = false;
17
18
    /**
19
     * Returns all test methods from the base class, except the ones which failed already
20
     *
21
     * @todo (re)introduce skipping of tests which failed when executed individually even if test runs happen as separate processes
22
     * @todo reintroduce skipping of tests within the loop
23
     */
24
    public function getSingleHttpTestMethods()
25
    {
26
        $unsafeMethods = array(
27
            'testCatchExceptions', 'testUtf8Method', 'testServerComments',
28
            'testExoticCharsetsRequests', 'testExoticCharsetsRequests2', 'testExoticCharsetsRequests3',
29
            'testWrapInexistentUrl',
30
        );
31
32
        $methods = array();
33
        foreach(get_class_methods('ServerTest') as $method)
34
        {
35
            if (strpos($method, 'test') === 0 && !in_array($method, $unsafeMethods))
36
            {
37
                if (!isset(self::$failed_tests[$method])) {
38
                    $methods[$method] = array($method);
39
                }
40
            }
41
        }
42
43
        return $methods;
44
    }
45
46
    /**
47
     * @dataProvider getSingleHttpTestMethods
48
     * @param string $method
49
     */
50
    public function testDeflate($method)
51
    {
52
        if (!function_exists('gzdeflate'))
53
        {
54
            $this->markTestSkipped('Zlib missing: cannot test deflate functionality');
55
            return;
56
        }
57
58
        $this->client->accepted_compression = array('deflate');
59
        $this->client->request_compression = 'deflate';
60
61
        $this->$method();
62
    }
63
64
    /**
65
     * @dataProvider getSingleHttpTestMethods
66
     * @param string $method
67
     */
68
    public function testGzip($method)
69
    {
70
        if (!function_exists('gzdeflate'))
71
        {
72
            $this->markTestSkipped('Zlib missing: cannot test gzip functionality');
73
            return;
74
        }
75
76
        $this->client->accepted_compression = array('gzip');
77
        $this->client->request_compression = 'gzip';
78
79
        $this->$method();
80
    }
81
82
    public function testKeepAlives()
83
    {
84
        if (!function_exists('curl_init'))
85
        {
86
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
87
            return;
88
        }
89
90
        $this->method = 'http11';
91
        $this->client->method = 'http11';
92
        $this->client->keepalive = true;
93
94
        // to successfully test keepalive, we have to reuse the same client for all tests, we can not recreate one on setup/teardown...
95
        foreach ($this->getSingleHttpTestMethods() as $methods) {
96
            $method = $methods[0];
97
            $this->$method();
98
        }
99
    }
100
101
    /**
102
     * @dataProvider getSingleHttpTestMethods
103
     * @param string $method
104
     */
105
    public function testRedirects($method)
106
    {
107
        if (!function_exists('curl_init'))
108
        {
109
            $this->markTestSkipped('CURL missing: cannot test redirects');
110
            return;
111
        }
112
113
        $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_ALWAYS);
114
        $this->client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => true, CURLOPT_POSTREDIR => 3));
115
116
        $this->$method();
117
    }
118
119
    public function testAcceptCharset()
120
    {
121
        if (version_compare(PHP_VERSION, '5.6.0', '<'))
122
        {
123
            $this->markTestSkipped('cannot test accept-charset on php < 5.6');
124
            return;
125
        }
126
        if (!function_exists('mb_list_encodings'))
127
        {
128
            $this->markTestSkipped('mbstring missing: cannot test accept-charset');
129
            return;
130
        }
131
132
        $r = new \PhpXmlRpc\Request('examples.stringecho', array(new \PhpXmlRpc\Value('€')));
133
        //chr(164)
134
135
        $originalEncoding = \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding;
136
        \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
137
138
        $this->addQueryParams(array('RESPONSE_ENCODING' => 'auto'));
139
        $this->client->accepted_charset_encodings = array(
140
            'utf-1234;q=0.1',
141
            'windows-1252;q=0.8'
142
        );
143
        $v = $this->send($r, 0, true);
144
        $h = $v->httpResponse();
145
        $this->assertEquals('text/xml; charset=Windows-1252', $h['headers']['content-type']);
146
        if ($v) {
147
            $this->assertEquals('€', $v->value()->scalarval());
0 ignored issues
show
Bug introduced by
The method scalarval() does not exist on integer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

147
            $this->assertEquals('€', $v->value()->/** @scrutinizer ignore-call */ scalarval());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
148
        }
149
        \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = $originalEncoding;
150
    }
151
152
    /**
153
     * @dataProvider getSingleHttpTestMethods
154
     * @param string $method
155
     */
156
    public function testProxy($method)
157
    {
158
        if ($this->args['PROXYSERVER'] == '')
159
        {
160
            $this->markTestSkipped('PROXYSERVER definition missing: cannot test proxy');
161
            return;
162
        }
163
164
        $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']);
165
166
        $this->$method();
167
    }
168
169
    /**
170
     * @dataProvider getSingleHttpTestMethods
171
     * @param string $method
172
     */
173
    public function testHttp11($method)
174
    {
175
        if (!function_exists('curl_init'))
176
        {
177
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
178
            return;
179
        }
180
181
        $this->method = 'http11'; // not an error the double assignment!
182
        $this->client->method = 'http11';
183
        $this->client->keepalive = false;
184
185
        $this->$method();
186
    }
187
188
    /**
189
     * @dataProvider getSingleHttpTestMethods
190
     * @param string $method
191
     */
192
    public function testHttp10Curl($method)
193
    {
194
        if (!function_exists('curl_init'))
195
        {
196
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
197
            return;
198
        }
199
200
        $this->method = 'http10'; // not an error the double assignment!
201
        $this->client->method = 'http10';
202
        $this->client->keepalive = false;
203
        $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_ALWAYS);
204
205
        $this->$method();
206
    }
207
208
    /**
209
     * @dataProvider getSingleHttpTestMethods
210
     * @param string $method
211
     */
212
    public function testHttp11Gzip($method)
213
    {
214
        if (!function_exists('curl_init'))
215
        {
216
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
217
            return;
218
        }
219
        $this->method = 'http11'; // not an error the double assignment!
220
        $this->client->method = 'http11';
221
        $this->client->keepalive = false;
222
        $this->client->accepted_compression = array('gzip');
223
        $this->client->request_compression = 'gzip';
224
225
        $this->$method();
226
    }
227
228
    /**
229
     * @dataProvider getSingleHttpTestMethods
230
     * @param string $method
231
     */
232
    public function testHttp11Deflate($method)
233
    {
234
        if (!function_exists('curl_init'))
235
        {
236
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
237
            return;
238
        }
239
        $this->method = 'http11'; // not an error the double assignment!
240
        $this->client->method = 'http11';
241
        $this->client->keepalive = false;
242
        $this->client->accepted_compression = array('deflate');
243
        $this->client->request_compression = 'deflate';
244
245
        $this->$method();
246
    }
247
248
    /**
249
     * @dataProvider getSingleHttpTestMethods
250
     * @param string $method
251
     */
252
    public function testHttp11Proxy($method)
253
    {
254
        if (!function_exists('curl_init'))
255
        {
256
            $this->markTestSkipped('CURL missing: cannot test http 1.1 w. proxy');
257
            return;
258
        }
259
        else if ($this->args['PROXYSERVER'] == '')
260
        {
261
            $this->markTestSkipped('PROXYSERVER definition missing: cannot test proxy w. http 1.1');
262
            return;
263
        }
264
265
        $this->method = 'http11'; // not an error the double assignment!
266
        $this->client->method = 'http11';
267
        $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']);
268
        $this->client->keepalive = false;
269
270
        $this->$method();
271
    }
272
273
    /**
274
     * @dataProvider getSingleHttpTestMethods
275
     * @param string $method
276
     */
277
    public function testHttps($method)
278
    {
279
        if (!function_exists('curl_init'))
280
        {
281
            $this->markTestSkipped('CURL missing: cannot test https functionality');
282
            return;
283
        }
284
        else if ($this->args['HTTPSSERVER'] == '')
285
        {
286
            $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https');
287
            return;
288
        }
289
290
        $this->client->server = $this->args['HTTPSSERVER'];
291
        $this->method = 'https';
292
        $this->client->method = 'https';
293
        $this->client->path = $this->args['HTTPSURI'];
294
        $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
295
        $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
296
        $this->client->setSSLVersion($this->args['SSLVERSION']);
297
298
        $this->$method();
299
    }
300
301
    /**
302
     * @dataProvider getSingleHttpTestMethods
303
     * @param string $method
304
     */
305
    public function testHttpsSocket($method)
306
    {
307
        if ($this->args['HTTPSSERVER'] == '')
308
        {
309
            $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https');
310
            return;
311
        }
312
313
        /// @todo investigate: can we make this work?
314
        if (version_compare(PHP_VERSION, '7.2', '<'))
315
        {
316
            if (is_readable('/etc/os-release')) {
317
                $output = file_get_contents('/etc/os-release');
318
                preg_match('/VERSION="?([0-9]+)/', $output, $matches);
319
                $ubuntuVersion = @$matches[1];
320
            } else {
321
                exec('uname -a', $output, $retval);
322
                preg_match('/ubunutu([0-9]+)/', $output[0], $matches);
323
                $ubuntuVersion = @$matches[1];
324
            }
325
            if ($ubuntuVersion >= 20) {
326
                $this->markTestSkipped('HTTPS via Socket known to fail on php less than 7.2 on Ubuntu 20 and higher');
327
                return;
328
            }
329
        }
330
331
        $this->client->server = $this->args['HTTPSSERVER'];
332
        $this->method = 'https';
333
        $this->client->method = 'https';
334
        $this->client->path = $this->args['HTTPSURI'];
335
        $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
336
        $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
337
        $this->client->setSSLVersion($this->args['SSLVERSION']);
338
        $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_NEVER);
339
340
        $this->$method();
341
    }
342
343
    /**
344
     * @dataProvider getSingleHttpTestMethods
345
     * @param string $method
346
     */
347
    public function testHttpsProxy($method)
348
    {
349
        if (!function_exists('curl_init'))
350
        {
351
            $this->markTestSkipped('CURL missing: cannot test https w. proxy');
352
            return;
353
        }
354
        else if ($this->args['PROXYSERVER'] == '')
355
        {
356
            $this->markTestSkipped('PROXYSERVER definition missing: cannot test proxy w. https');
357
            return;
358
        }
359
        else if ($this->args['HTTPSSERVER'] == '')
360
        {
361
            $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https w. proxy');
362
            return;
363
        }
364
365
        $this->client->server = $this->args['HTTPSSERVER'];
366
        $this->method = 'https';
367
        $this->client->method = 'https';
368
        $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']);
369
        $this->client->path = $this->args['HTTPSURI'];
370
        $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
371
        $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
372
        $this->client->setSSLVersion($this->args['SSLVERSION']);
373
374
        $this->$method();
375
    }
376
377
    /**
378
     * @dataProvider getSingleHttpTestMethods
379
     * @param string $method
380
     */
381
    public function testHttp2($method)
382
    {
383
        if (!function_exists('curl_init'))
384
        {
385
            $this->markTestSkipped('CURL missing: cannot test http/2');
386
            return;
387
        } else if (!defined('CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE'))
388
        {
389
            $this->markTestSkipped('CURL http/2 support missing: cannot test http/2');
390
            return;
391
        }
392
393
        $this->method = 'h2c'; // not an error the double assignment!
394
        $this->client->method = 'h2c';
395
        //$this->client->keepalive = false; // q: is this a good idea?
396
397
        $this->expectHttp2 = true;
398
        $this->$method();
399
        $this->expectHttp2 = false;
400
    }
401
402
    /**
403
     * @dataProvider getSingleHttpTestMethods
404
     * @param string $method
405
     */
406
    public function testHttp2tls($method)
407
    {
408
        if (!function_exists('curl_init'))
409
        {
410
            $this->markTestSkipped('CURL missing: cannot test http/2 tls');
411
            return;
412
        } else if ($this->args['HTTPSSERVER'] == '')
413
        {
414
            $this->markTestSkipped('HTTPS SERVER definition missing: cannot test http/2 tls');
415
            return;
416
        } else if (!defined('CURL_HTTP_VERSION_2_0'))
417
        {
418
            $this->markTestSkipped('CURL http/2 support missing: cannot test http/2 tls');
419
            return;
420
        }
421
422
        $this->client->server = $this->args['HTTPSSERVER'];
423
        $this->method = 'h2';
424
        $this->client->method = 'h2';
425
        $this->client->path = $this->args['HTTPSURI'];
426
        $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
427
        $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
428
        $this->client->setSSLVersion($this->args['SSLVERSION']);
429
430
        $this->expectHttp2 = true;
431
        $this->$method();
432
        $this->expectHttp2 = false;
433
    }
434
435
    /**
436
     * @dataProvider getSingleHttpTestMethods
437
     * @param string $method
438
     */
439
    public function testUTF8Responses($method)
440
    {
441
        $this->addQueryParams(array('RESPONSE_ENCODING' => 'UTF-8'));
442
443
        $this->$method();
444
    }
445
446
    /**
447
     * @dataProvider getSingleHttpTestMethods
448
     * @param string $method
449
     */
450
    public function testUTF8Requests($method)
451
    {
452
        $this->client->request_charset_encoding = 'UTF-8';
453
454
        $this->$method();
455
    }
456
457
    /**
458
     * @dataProvider getSingleHttpTestMethods
459
     * @param string $method
460
     */
461
    public function testISOResponses($method)
462
    {
463
        $this->addQueryParams(array('RESPONSE_ENCODING' => 'ISO-8859-1'));
464
465
        $this->$method();
466
    }
467
468
    /**
469
     * @dataProvider getSingleHttpTestMethods
470
     * @param string $method
471
     */
472
    public function testISORequests($method)
473
    {
474
        $this->client->request_charset_encoding = 'ISO-8859-1';
475
476
        $this->$method();
477
    }
478
479
    /**
480
     * @dataProvider getSingleHttpTestMethods
481
     * @param string $method
482
     */
483
    public function testBasicAuth($method)
484
    {
485
        $this->client->setCredentials('test', 'test');
486
        $this->addQueryParams(array('FORCE_AUTH' => 'Basic'));
487
488
        $this->$method();
489
    }
490
491
    /**
492
     * @dataProvider getSingleHttpTestMethods
493
     * @param string $method
494
     */
495
    public function testDigestAuth($method)
496
    {
497
        if (!function_exists('curl_init'))
498
        {
499
            $this->markTestSkipped('CURL missing: cannot test digest auth functionality');
500
            return;
501
        }
502
503
        $this->client->setCredentials('test', 'test', CURLAUTH_DIGEST);
504
        $this->addQueryParams(array('FORCE_AUTH' => 'Digest'));
505
        $this->method = 'http11';
506
        $this->client->method = 'http11';
507
508
        $this->$method();
509
    }
510
511
    /**
512
     * @param \PhpXmlRpc\Response $r
513
     * @return void
514
     */
515
    protected function validateResponse($r)
516
    {
517
        if ($this->expectHttp2) {
518
            $hr = $r->httpResponse();
519
            $this->assertEquals("2", @$hr['protocol_version']);
520
        } else {
521
522
        }
523
    }
524
}
525