Total Complexity | 65 |
Total Lines | 532 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 2 |
Complex classes like HTTPTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use HTTPTest, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class HTTPTest extends ServerTest |
||
10 | { |
||
11 | protected $expectHttp2 = false; |
||
12 | |||
13 | /** |
||
14 | * Returns all test methods from the base class, except the ones which failed already |
||
15 | * |
||
16 | * @todo (re)introduce skipping of tests which failed when executed individually even if test runs happen as separate processes |
||
17 | * @todo reintroduce skipping of tests within the loop |
||
18 | */ |
||
19 | public function getSingleHttpTestMethods() |
||
20 | { |
||
21 | $unsafeMethods = array( |
||
22 | 'testCatchExceptions', 'testCatchErrors', 'testUtf8Method', 'testServerComments', |
||
23 | 'testExoticCharsetsRequests', 'testExoticCharsetsRequests2', 'testExoticCharsetsRequests3', |
||
24 | 'testWrapInexistentUrl', 'testNegativeDebug' |
||
25 | ); |
||
26 | |||
27 | $methods = array(); |
||
28 | foreach(get_class_methods('ServerTest') as $method) |
||
29 | { |
||
30 | if (strpos($method, 'test') === 0 && !in_array($method, $unsafeMethods)) |
||
31 | { |
||
32 | if (!isset(self::$failed_tests[$method])) { |
||
33 | $methods[$method] = array($method); |
||
34 | } |
||
35 | } |
||
36 | } |
||
37 | |||
38 | return $methods; |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @dataProvider getSingleHttpTestMethods |
||
43 | * @param string $method |
||
44 | */ |
||
45 | public function testDeflate($method) |
||
46 | { |
||
47 | if (!function_exists('gzdeflate')) |
||
48 | { |
||
49 | $this->markTestSkipped('Zlib missing: cannot test deflate functionality'); |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | $this->client->accepted_compression = array('deflate'); |
||
|
|||
54 | $this->client->request_compression = 'deflate'; |
||
55 | |||
56 | $this->$method(); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @dataProvider getSingleHttpTestMethods |
||
61 | * @param string $method |
||
62 | */ |
||
63 | public function testGzip($method) |
||
64 | { |
||
65 | if (!function_exists('gzdeflate')) |
||
66 | { |
||
67 | $this->markTestSkipped('Zlib missing: cannot test gzip functionality'); |
||
68 | return; |
||
69 | } |
||
70 | |||
71 | $this->client->accepted_compression = array('gzip'); |
||
72 | $this->client->request_compression = 'gzip'; |
||
73 | |||
74 | $this->$method(); |
||
75 | } |
||
76 | |||
77 | public function testKeepAlives() |
||
93 | } |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @dataProvider getSingleHttpTestMethods |
||
98 | * @param string $method |
||
99 | */ |
||
100 | public function testRedirects($method) |
||
101 | { |
||
102 | if (!function_exists('curl_init')) |
||
103 | { |
||
104 | $this->markTestSkipped('CURL missing: cannot test redirects'); |
||
105 | return; |
||
106 | } |
||
107 | |||
108 | /// @todo replace with setOption when dropping the BC layer |
||
109 | $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_ALWAYS); |
||
110 | $this->client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => true, CURLOPT_POSTREDIR => 3)); |
||
111 | |||
112 | $this->$method(); |
||
113 | } |
||
114 | |||
115 | public function testAcceptCharset() |
||
116 | { |
||
117 | if (version_compare(PHP_VERSION, '5.6.0', '<')) |
||
118 | { |
||
119 | $this->markTestSkipped('cannot test accept-charset on php < 5.6'); |
||
120 | return; |
||
121 | } |
||
122 | if (!function_exists('mb_list_encodings')) |
||
123 | { |
||
124 | $this->markTestSkipped('mbstring missing: cannot test accept-charset'); |
||
125 | return; |
||
126 | } |
||
127 | |||
128 | $r = new \PhpXmlRpc\Request('examples.stringecho', array(new \PhpXmlRpc\Value('€'))); |
||
129 | //chr(164) |
||
130 | |||
131 | $originalEncoding = \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding; |
||
132 | \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8'; |
||
133 | |||
134 | $this->addQueryParams(array('RESPONSE_ENCODING' => 'auto')); |
||
135 | $this->client->accepted_charset_encodings = array( |
||
136 | 'utf-1234;q=0.1', |
||
137 | 'windows-1252;q=0.8' |
||
138 | ); |
||
139 | $v = $this->send($r, 0, true); |
||
140 | $h = $v->httpResponse(); |
||
141 | $this->assertEquals('text/xml; charset=Windows-1252', $h['headers']['content-type']); |
||
142 | if ($v) { |
||
143 | $this->assertEquals('€', $v->value()->scalarval()); |
||
144 | } |
||
145 | \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = $originalEncoding; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @dataProvider getSingleHttpTestMethods |
||
150 | * @param string $method |
||
151 | */ |
||
152 | public function testProxy($method) |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @dataProvider getSingleHttpTestMethods |
||
167 | * @param string $method |
||
168 | */ |
||
169 | public function testHttp11($method) |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @dataProvider getSingleHttpTestMethods |
||
186 | * @param string $method |
||
187 | */ |
||
188 | public function testHttp10Curl($method) |
||
189 | { |
||
190 | if (!function_exists('curl_init')) |
||
191 | { |
||
192 | $this->markTestSkipped('CURL missing: cannot test http 1.1'); |
||
193 | return; |
||
194 | } |
||
195 | |||
196 | $this->method = 'http10'; // not an error the double assignment! |
||
197 | $this->client->method = 'http10'; |
||
198 | /// @todo replace with setOption when dropping the BC layer |
||
199 | $this->client->keepalive = false; |
||
200 | $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_ALWAYS); |
||
201 | |||
202 | $this->$method(); |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @dataProvider getSingleHttpTestMethods |
||
207 | * @param string $method |
||
208 | */ |
||
209 | public function testHttp11Gzip($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('gzip'); |
||
220 | $this->client->request_compression = 'gzip'; |
||
221 | |||
222 | $this->$method(); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * @dataProvider getSingleHttpTestMethods |
||
227 | * @param string $method |
||
228 | */ |
||
229 | public function testHttp11Deflate($method) |
||
230 | { |
||
231 | if (!function_exists('curl_init')) |
||
232 | { |
||
233 | $this->markTestSkipped('CURL missing: cannot test http 1.1'); |
||
234 | return; |
||
235 | } |
||
236 | $this->method = 'http11'; // not an error the double assignment! |
||
237 | $this->client->method = 'http11'; |
||
238 | $this->client->keepalive = false; |
||
239 | $this->client->accepted_compression = array('deflate'); |
||
240 | $this->client->request_compression = 'deflate'; |
||
241 | |||
242 | $this->$method(); |
||
243 | } |
||
244 | |||
245 | /** |
||
246 | * @dataProvider getSingleHttpTestMethods |
||
247 | * @param string $method |
||
248 | */ |
||
249 | public function testHttp11Proxy($method) |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @dataProvider getSingleHttpTestMethods |
||
272 | * @param string $method |
||
273 | */ |
||
274 | public function testHttps($method) |
||
275 | { |
||
276 | if (!function_exists('curl_init')) |
||
277 | { |
||
278 | $this->markTestSkipped('CURL missing: cannot test https functionality'); |
||
279 | return; |
||
280 | } |
||
281 | else if ($this->args['HTTPSSERVER'] == '') |
||
282 | { |
||
283 | $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https'); |
||
284 | return; |
||
285 | } |
||
286 | |||
287 | $this->client->server = $this->args['HTTPSSERVER']; |
||
288 | $this->method = 'https'; |
||
289 | $this->client->method = 'https'; |
||
290 | $this->client->path = $this->args['HTTPSURI']; |
||
291 | /// @todo replace with setOptions when dropping the BC layer |
||
292 | $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']); |
||
293 | $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']); |
||
294 | $this->client->setSSLVersion($this->args['SSLVERSION']); |
||
295 | if (version_compare(PHP_VERSION, '8.0', '>') && $this->args['SSLVERSION'] == 0) |
||
296 | { |
||
297 | $version = explode('.', PHP_VERSION); |
||
298 | $this->client->setSSLVersion(4 + $version[1]); |
||
299 | } |
||
300 | |||
301 | $this->$method(); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @dataProvider getSingleHttpTestMethods |
||
306 | * @param string $method |
||
307 | */ |
||
308 | public function testHttpsSocket($method) |
||
309 | { |
||
310 | if ($this->args['HTTPSSERVER'] == '') |
||
311 | { |
||
312 | $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https'); |
||
313 | return; |
||
314 | } |
||
315 | |||
316 | /// @todo investigate: can we make this work? |
||
317 | if (version_compare(PHP_VERSION, '7.2', '<')) |
||
318 | { |
||
319 | if (is_readable('/etc/os-release')) { |
||
320 | $output = file_get_contents('/etc/os-release'); |
||
321 | preg_match('/VERSION="?([0-9]+)/', $output, $matches); |
||
322 | $ubuntuVersion = @$matches[1]; |
||
323 | } else { |
||
324 | exec('uname -a', $output, $retval); |
||
325 | preg_match('/ubunutu([0-9]+)/', $output[0], $matches); |
||
326 | $ubuntuVersion = @$matches[1]; |
||
327 | } |
||
328 | if ($ubuntuVersion >= 20) { |
||
329 | $this->markTestSkipped('HTTPS via Socket known to fail on php less than 7.2 on Ubuntu 20 and higher'); |
||
330 | return; |
||
331 | } |
||
332 | } |
||
333 | |||
334 | $this->client->server = $this->args['HTTPSSERVER']; |
||
335 | $this->method = 'https'; |
||
336 | $this->client->method = 'https'; |
||
337 | $this->client->path = $this->args['HTTPSURI']; |
||
338 | /// @todo replace with setOptions when dropping the BC layer |
||
339 | $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']); |
||
340 | $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']); |
||
341 | $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_NEVER); |
||
342 | $this->client->setSSLVersion($this->args['SSLVERSION']); |
||
343 | if (version_compare(PHP_VERSION, '8.0', '>')) |
||
344 | { |
||
345 | $version = explode('.', PHP_VERSION); |
||
346 | $this->client->setOption(\PhpXmlRpc\Client::OPT_EXTRA_SOCKET_OPTS, |
||
347 | array('ssl' => array('security_level' => 2 + $version[1]))); |
||
348 | if ($this->args['SSLVERSION'] == 0) { |
||
349 | $this->client->setSSLVersion(4 + $version[1]); |
||
350 | } |
||
351 | } |
||
352 | |||
353 | $this->$method(); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @dataProvider getSingleHttpTestMethods |
||
358 | * @param string $method |
||
359 | */ |
||
360 | public function testHttpsProxy($method) |
||
361 | { |
||
362 | if (!function_exists('curl_init')) |
||
363 | { |
||
364 | $this->markTestSkipped('CURL missing: cannot test https w. proxy'); |
||
365 | return; |
||
366 | } |
||
367 | else if ($this->args['PROXYSERVER'] == '') |
||
368 | { |
||
369 | $this->markTestSkipped('PROXYSERVER definition missing: cannot test proxy w. https'); |
||
370 | return; |
||
371 | } |
||
372 | else if ($this->args['HTTPSSERVER'] == '') |
||
373 | { |
||
374 | $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https w. proxy'); |
||
375 | return; |
||
376 | } |
||
377 | |||
378 | $this->method = 'https'; |
||
379 | $this->client->method = 'https'; |
||
380 | $this->client->server = $this->args['HTTPSSERVER']; |
||
381 | $this->client->path = $this->args['HTTPSURI']; |
||
382 | /// @todo replace with setOptions when dropping the BC layer |
||
383 | $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']); |
||
384 | $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']); |
||
385 | $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']); |
||
386 | $this->client->setSSLVersion($this->args['SSLVERSION']); |
||
387 | if (version_compare(PHP_VERSION, '8.0', '>') && $this->args['SSLVERSION'] == 0) |
||
388 | { |
||
389 | $version = explode('.', PHP_VERSION); |
||
390 | $this->client->setSSLVersion(4 + $version[1]); |
||
391 | } |
||
392 | |||
393 | $this->$method(); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * @dataProvider getSingleHttpTestMethods |
||
398 | * @param string $method |
||
399 | */ |
||
400 | public function testHttp2NoTls($method) |
||
401 | { |
||
402 | if (!function_exists('curl_init')) |
||
403 | { |
||
404 | $this->markTestSkipped('CURL missing: cannot test http/2'); |
||
405 | return; |
||
406 | } else if (!defined('CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE')) |
||
407 | { |
||
408 | $this->markTestSkipped('CURL http/2 support missing: cannot test http/2'); |
||
409 | return; |
||
410 | } |
||
411 | |||
412 | $this->method = 'h2c'; // not an error the double assignment! |
||
413 | $this->client->method = 'h2c'; |
||
414 | //$this->client->keepalive = false; // q: is this a good idea? |
||
415 | |||
416 | $this->expectHttp2 = true; |
||
417 | $this->$method(); |
||
418 | $this->expectHttp2 = false; |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * @dataProvider getSingleHttpTestMethods |
||
423 | * @param string $method |
||
424 | */ |
||
425 | public function testHttp2tls($method) |
||
426 | { |
||
427 | if (!function_exists('curl_init')) |
||
428 | { |
||
429 | $this->markTestSkipped('CURL missing: cannot test http/2 tls'); |
||
430 | return; |
||
431 | } else if ($this->args['HTTPSSERVER'] == '') |
||
432 | { |
||
433 | $this->markTestSkipped('HTTPS SERVER definition missing: cannot test http/2 tls'); |
||
434 | return; |
||
435 | } else if (!defined('CURL_HTTP_VERSION_2_0')) |
||
436 | { |
||
437 | $this->markTestSkipped('CURL http/2 support missing: cannot test http/2 tls'); |
||
438 | return; |
||
439 | } |
||
440 | |||
441 | $this->method = 'h2'; |
||
442 | $this->client->method = 'h2'; |
||
443 | $this->client->server = $this->args['HTTPSSERVER']; |
||
444 | $this->client->path = $this->args['HTTPSURI']; |
||
445 | /// @todo replace with setOptions when dropping the BC layer |
||
446 | $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']); |
||
447 | $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']); |
||
448 | $this->client->setSSLVersion($this->args['SSLVERSION']); |
||
449 | |||
450 | $this->expectHttp2 = true; |
||
451 | $this->$method(); |
||
452 | $this->expectHttp2 = false; |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * @dataProvider getSingleHttpTestMethods |
||
457 | * @param string $method |
||
458 | */ |
||
459 | public function testUTF8Responses($method) |
||
460 | { |
||
461 | $this->addQueryParams(array('RESPONSE_ENCODING' => 'UTF-8')); |
||
462 | |||
463 | $this->$method(); |
||
464 | } |
||
465 | |||
466 | /** |
||
467 | * @dataProvider getSingleHttpTestMethods |
||
468 | * @param string $method |
||
469 | */ |
||
470 | public function testUTF8Requests($method) |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * @dataProvider getSingleHttpTestMethods |
||
479 | * @param string $method |
||
480 | */ |
||
481 | public function testISOResponses($method) |
||
482 | { |
||
483 | $this->addQueryParams(array('RESPONSE_ENCODING' => 'ISO-8859-1')); |
||
484 | |||
485 | $this->$method(); |
||
486 | } |
||
487 | |||
488 | /** |
||
489 | * @dataProvider getSingleHttpTestMethods |
||
490 | * @param string $method |
||
491 | */ |
||
492 | public function testISORequests($method) |
||
493 | { |
||
494 | $this->client->request_charset_encoding = 'ISO-8859-1'; |
||
495 | |||
496 | $this->$method(); |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * @dataProvider getSingleHttpTestMethods |
||
501 | * @param string $method |
||
502 | */ |
||
503 | public function testBasicAuth($method) |
||
504 | { |
||
505 | $this->client->setCredentials('test', 'test'); |
||
506 | $this->addQueryParams(array('FORCE_AUTH' => 'Basic')); |
||
507 | |||
508 | $this->$method(); |
||
509 | } |
||
510 | |||
511 | /** |
||
512 | * @dataProvider getSingleHttpTestMethods |
||
513 | * @param string $method |
||
514 | */ |
||
515 | public function testDigestAuth($method) |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * @param \PhpXmlRpc\Response $r |
||
533 | * @return void |
||
534 | */ |
||
535 | protected function validateResponse($r) |
||
541 | |||
542 | } |
||
543 | } |
||
544 | } |
||
545 |