Passed
Push — master ( 89ae23...4acd79 )
by Gaetano
07:39
created

HTTPTest::testHttps()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 22
rs 9.7998
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__ . '/5ServerTest.php';
9
10
/**
11
 * Tests which stress http features of the library.
12
 * Each of these tests iterates over (almost) all the 'localhost' 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 testProxy($method)
106
    {
107
        if ($this->args['PROXYSERVER'] == '')
108
        {
109
            $this->markTestSkipped('PROXYSERVER definition missing: cannot test proxy');
110
            return;
111
        }
112
113
        $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']);
114
115
        $this->$method();
116
    }
117
118
    /**
119
     * @dataProvider getSingleHttpTestMethods
120
     * @param string $method
121
     */
122
    public function testHttp11($method)
123
    {
124
        if (!function_exists('curl_init'))
125
        {
126
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
127
            return;
128
        }
129
130
        $this->method = 'http11'; // not an error the double assignment!
131
        $this->client->method = 'http11';
132
        $this->client->keepalive = false;
133
134
        $this->$method();
135
    }
136
137
    /**
138
     * @dataProvider getSingleHttpTestMethods
139
     * @param string $method
140
     */
141
    public function testHttp10Curl($method)
142
    {
143
        if (!function_exists('curl_init'))
144
        {
145
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
146
            return;
147
        }
148
149
        $this->method = 'http10'; // not an error the double assignment!
150
        $this->client->method = 'http10';
151
        $this->client->keepalive = false;
152
        $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_ALWAYS);
153
154
        $this->$method();
155
    }
156
157
    /**
158
     * @dataProvider getSingleHttpTestMethods
159
     * @param string $method
160
     */
161
    public function testHttp11Gzip($method)
162
    {
163
        if (!function_exists('curl_init'))
164
        {
165
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
166
            return;
167
        }
168
        $this->method = 'http11'; // not an error the double assignment!
169
        $this->client->method = 'http11';
170
        $this->client->keepalive = false;
171
        $this->client->accepted_compression = array('gzip');
172
        $this->client->request_compression = 'gzip';
173
174
        $this->$method();
175
    }
176
177
    /**
178
     * @dataProvider getSingleHttpTestMethods
179
     * @param string $method
180
     */
181
    public function testHttp11Deflate($method)
182
    {
183
        if (!function_exists('curl_init'))
184
        {
185
            $this->markTestSkipped('CURL missing: cannot test http 1.1');
186
            return;
187
        }
188
        $this->method = 'http11'; // not an error the double assignment!
189
        $this->client->method = 'http11';
190
        $this->client->keepalive = false;
191
        $this->client->accepted_compression = array('deflate');
192
        $this->client->request_compression = 'deflate';
193
194
        $this->$method();
195
    }
196
197
    /**
198
     * @dataProvider getSingleHttpTestMethods
199
     * @param string $method
200
     */
201
    public function testHttp11Proxy($method)
202
    {
203
        if (!function_exists('curl_init'))
204
        {
205
            $this->markTestSkipped('CURL missing: cannot test http 1.1 w. proxy');
206
            return;
207
        }
208
        else if ($this->args['PROXYSERVER'] == '')
209
        {
210
            $this->markTestSkipped('PROXYSERVER definition missing: cannot test proxy w. http 1.1');
211
            return;
212
        }
213
214
        $this->method = 'http11'; // not an error the double assignment!
215
        $this->client->method = 'http11';
216
        $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']);
217
        $this->client->keepalive = false;
218
219
        $this->$method();
220
    }
221
222
    /**
223
     * @dataProvider getSingleHttpTestMethods
224
     * @param string $method
225
     */
226
    public function testHttps($method)
227
    {
228
        if (!function_exists('curl_init'))
229
        {
230
            $this->markTestSkipped('CURL missing: cannot test https functionality');
231
            return;
232
        }
233
        else if ($this->args['HTTPSSERVER'] == '')
234
        {
235
            $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https');
236
            return;
237
        }
238
239
        $this->client->server = $this->args['HTTPSSERVER'];
240
        $this->method = 'https';
241
        $this->client->method = 'https';
242
        $this->client->path = $this->args['HTTPSURI'];
243
        $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
244
        $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
245
        $this->client->setSSLVersion($this->args['SSLVERSION']);
246
247
        $this->$method();
248
    }
249
250
    /**
251
     * @dataProvider getSingleHttpTestMethods
252
     * @param string $method
253
     */
254
    public function testHttpsSocket($method)
255
    {
256
        if ($this->args['HTTPSSERVER'] == '')
257
        {
258
            $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https');
259
            return;
260
        }
261
262
        /// @todo investigate: can we make this work?
263
        if (version_compare(PHP_VERSION, '7.2', '<'))
264
        {
265
            if (is_readable('/etc/os-release')) {
266
                $output = file_get_contents('/etc/os-release');
267
                preg_match('/VERSION="?([0-9]+)/', $output, $matches);
268
                $ubuntuVersion = @$matches[1];
269
            } else {
270
                exec('uname -a', $output, $retval);
271
                preg_match('/ubunutu([0-9]+)/', $output[0], $matches);
272
                $ubuntuVersion = @$matches[1];
273
            }
274
            if ($ubuntuVersion >= 20) {
275
                $this->markTestSkipped('HTTPS via Socket known to fail on php less than 7.2 on Ubuntu 20 and higher');
276
                return;
277
            }
278
        }
279
280
        $this->client->server = $this->args['HTTPSSERVER'];
281
        $this->method = 'https';
282
        $this->client->method = 'https';
283
        $this->client->path = $this->args['HTTPSURI'];
284
        $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
285
        $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
286
        $this->client->setSSLVersion($this->args['SSLVERSION']);
287
        $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_NEVER);
288
289
        $this->$method();
290
    }
291
292
    /**
293
     * @dataProvider getSingleHttpTestMethods
294
     * @param string $method
295
     */
296
    public function testHttpsProxy($method)
297
    {
298
        if (!function_exists('curl_init'))
299
        {
300
            $this->markTestSkipped('CURL missing: cannot test https w. proxy');
301
            return;
302
        }
303
        else if ($this->args['PROXYSERVER'] == '')
304
        {
305
            $this->markTestSkipped('PROXYSERVER definition missing: cannot test proxy w. https');
306
            return;
307
        }
308
        else if ($this->args['HTTPSSERVER'] == '')
309
        {
310
            $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https w. proxy');
311
            return;
312
        }
313
314
        $this->client->server = $this->args['HTTPSSERVER'];
315
        $this->method = 'https';
316
        $this->client->method = 'https';
317
        $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']);
318
        $this->client->path = $this->args['HTTPSURI'];
319
        $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
320
        $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
321
        $this->client->setSSLVersion($this->args['SSLVERSION']);
322
323
        $this->$method();
324
    }
325
326
    /**
327
     * @dataProvider getSingleHttpTestMethods
328
     * @param string $method
329
     */
330
    public function testHttp2($method)
331
    {
332
        if (!function_exists('curl_init'))
333
        {
334
            $this->markTestSkipped('CURL missing: cannot test http/2');
335
            return;
336
        } else if (!defined('CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE'))
337
        {
338
            $this->markTestSkipped('CURL http/2 support missing: cannot test http/2');
339
            return;
340
        }
341
342
        $this->method = 'h2c'; // not an error the double assignment!
343
        $this->client->method = 'h2c';
344
        //$this->client->keepalive = false; // q: is this a good idea?
345
346
        $this->expectHttp2 = true;
347
        $this->$method();
348
        $this->expectHttp2 = false;
349
    }
350
351
    /**
352
     * @dataProvider getSingleHttpTestMethods
353
     * @param string $method
354
     */
355
    public function testHttp2tls($method)
356
    {
357
        if (!function_exists('curl_init'))
358
        {
359
            $this->markTestSkipped('CURL missing: cannot test http/2 tls');
360
            return;
361
        } else if ($this->args['HTTPSSERVER'] == '')
362
        {
363
            $this->markTestSkipped('HTTPS SERVER definition missing: cannot test http/2 tls');
364
            return;
365
        } else if (!defined('CURL_HTTP_VERSION_2_0'))
366
        {
367
            $this->markTestSkipped('CURL http/2 support missing: cannot test http/2 tls');
368
            return;
369
        }
370
371
        $this->client->server = $this->args['HTTPSSERVER'];
372
        $this->method = 'h2';
373
        $this->client->method = 'h2';
374
        $this->client->path = $this->args['HTTPSURI'];
375
        $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']);
376
        $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']);
377
        $this->client->setSSLVersion($this->args['SSLVERSION']);
378
379
        $this->expectHttp2 = true;
380
        $this->$method();
381
        $this->expectHttp2 = false;
382
    }
383
384
    /**
385
     * @dataProvider getSingleHttpTestMethods
386
     * @param string $method
387
     */
388
    public function testUTF8Responses($method)
389
    {
390
        $this->addQueryParams(array('RESPONSE_ENCODING' => 'UTF-8'));
391
392
        $this->$method();
393
    }
394
395
    /**
396
     * @dataProvider getSingleHttpTestMethods
397
     * @param string $method
398
     */
399
    public function testUTF8Requests($method)
400
    {
401
        $this->client->request_charset_encoding = 'UTF-8';
402
403
        $this->$method();
404
    }
405
406
    /**
407
     * @dataProvider getSingleHttpTestMethods
408
     * @param string $method
409
     */
410
    public function testISOResponses($method)
411
    {
412
        $this->addQueryParams(array('RESPONSE_ENCODING' => 'ISO-8859-1'));
413
414
        $this->$method();
415
    }
416
417
    /**
418
     * @dataProvider getSingleHttpTestMethods
419
     * @param string $method
420
     */
421
    public function testISORequests($method)
422
    {
423
        $this->client->request_charset_encoding = 'ISO-8859-1';
424
425
        $this->$method();
426
    }
427
428
    /**
429
     * @dataProvider getSingleHttpTestMethods
430
     * @param string $method
431
     */
432
    public function testBasicAuth($method)
433
    {
434
        $this->client->setCredentials('test', 'test');
435
        $this->addQueryParams(array('FORCE_AUTH' => 'Basic'));
436
437
        $this->$method();
438
    }
439
440
    /**
441
     * @dataProvider getSingleHttpTestMethods
442
     * @param string $method
443
     */
444
    public function testDigestAuth($method)
445
    {
446
        if (!function_exists('curl_init'))
447
        {
448
            $this->markTestSkipped('CURL missing: cannot test digest auth functionality');
449
            return;
450
        }
451
452
        $this->client->setCredentials('test', 'test', CURLAUTH_DIGEST);
453
        $this->addQueryParams(array('FORCE_AUTH' => 'Digest'));
454
        $this->method = 'http11';
455
        $this->client->method = 'http11';
456
457
        $this->$method();
458
    }
459
460
    /**
461
     * @param \PhpXmlRpc\Response $r
462
     * @return void
463
     */
464
    protected function validateResponse($r)
465
    {
466
        if ($this->expectHttp2) {
467
            $hr = $r->httpResponse();
468
            $this->assertEquals("2", $hr['protocol_version']);
469
        } else {
470
471
        }
472
    }
473
}
474