Passed
Push — master ( 5987e0...fe1d88 )
by Gaetano
09:19
created

HTTPTest::testISOResponses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
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 '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
    public function testAcceptCharset()
102
    {
103
        if (!function_exists('mb_list_encodings'))
104
        {
105
            $this->markTestSkipped('mbstring missing: cannot test accept-charset');
106
            return;
107
        }
108
109
        $r = new \PhpXmlRpc\Request('examples.stringecho', array(new \PhpXmlRpc\Value('€')));
110
        //chr(164)
111
112
        $originalEncoding = \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding;
113
        \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
114
115
        $this->addQueryParams(array('RESPONSE_ENCODING' => 'auto'));
116
        $this->client->accepted_charset_encodings = array(
117
            'utf-1234;q=0.1',
118
            'windows-1252;q=0.8'
119
        );
120
        $v = $this->send($r, 0, true);
121
        $h = $v->httpResponse();
122
        $this->assertEquals('text/xml; charset=Windows-1252', $h['headers']['content-type']);
123
        if ($v) {
124
            $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

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