Total Complexity | 60 |
Total Lines | 514 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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() |
||
78 | { |
||
79 | if (!function_exists('curl_init')) |
||
80 | { |
||
81 | $this->markTestSkipped('CURL missing: cannot test http 1.1'); |
||
82 | return; |
||
83 | } |
||
84 | |||
85 | $this->method = 'http11'; |
||
86 | $this->client->method = 'http11'; |
||
87 | $this->client->keepalive = true; |
||
88 | |||
89 | // to successfully test keepalive, we have to reuse the same client for all tests, we can not recreate one on setup/teardown... |
||
90 | foreach ($this->getSingleHttpTestMethods() as $methods) { |
||
91 | $method = $methods[0]; |
||
92 | $this->$method(); |
||
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 | $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_ALWAYS); |
||
109 | $this->client->setCurlOptions(array(CURLOPT_FOLLOWLOCATION => true, CURLOPT_POSTREDIR => 3)); |
||
110 | |||
111 | $this->$method(); |
||
112 | } |
||
113 | |||
114 | public function testAcceptCharset() |
||
115 | { |
||
116 | if (version_compare(PHP_VERSION, '5.6.0', '<')) |
||
117 | { |
||
118 | $this->markTestSkipped('cannot test accept-charset on php < 5.6'); |
||
119 | return; |
||
120 | } |
||
121 | if (!function_exists('mb_list_encodings')) |
||
122 | { |
||
123 | $this->markTestSkipped('mbstring missing: cannot test accept-charset'); |
||
124 | return; |
||
125 | } |
||
126 | |||
127 | $r = new \PhpXmlRpc\Request('examples.stringecho', array(new \PhpXmlRpc\Value('€'))); |
||
128 | //chr(164) |
||
129 | |||
130 | $originalEncoding = \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding; |
||
131 | \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8'; |
||
132 | |||
133 | $this->addQueryParams(array('RESPONSE_ENCODING' => 'auto')); |
||
134 | $this->client->accepted_charset_encodings = array( |
||
135 | 'utf-1234;q=0.1', |
||
136 | 'windows-1252;q=0.8' |
||
137 | ); |
||
138 | $v = $this->send($r, 0, true); |
||
139 | $h = $v->httpResponse(); |
||
140 | $this->assertEquals('text/xml; charset=Windows-1252', $h['headers']['content-type']); |
||
141 | if ($v) { |
||
142 | $this->assertEquals('€', $v->value()->scalarval()); |
||
143 | } |
||
144 | \PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = $originalEncoding; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * @dataProvider getSingleHttpTestMethods |
||
149 | * @param string $method |
||
150 | */ |
||
151 | public function testProxy($method) |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @dataProvider getSingleHttpTestMethods |
||
166 | * @param string $method |
||
167 | */ |
||
168 | public function testHttp11($method) |
||
169 | { |
||
170 | if (!function_exists('curl_init')) |
||
171 | { |
||
172 | $this->markTestSkipped('CURL missing: cannot test http 1.1'); |
||
173 | return; |
||
174 | } |
||
175 | |||
176 | $this->method = 'http11'; // not an error the double assignment! |
||
177 | $this->client->method = 'http11'; |
||
178 | $this->client->keepalive = false; |
||
179 | |||
180 | $this->$method(); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @dataProvider getSingleHttpTestMethods |
||
185 | * @param string $method |
||
186 | */ |
||
187 | public function testHttp10Curl($method) |
||
188 | { |
||
189 | if (!function_exists('curl_init')) |
||
190 | { |
||
191 | $this->markTestSkipped('CURL missing: cannot test http 1.1'); |
||
192 | return; |
||
193 | } |
||
194 | |||
195 | $this->method = 'http10'; // not an error the double assignment! |
||
196 | $this->client->method = 'http10'; |
||
197 | $this->client->keepalive = false; |
||
198 | $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_ALWAYS); |
||
199 | |||
200 | $this->$method(); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @dataProvider getSingleHttpTestMethods |
||
205 | * @param string $method |
||
206 | */ |
||
207 | public function testHttp11Gzip($method) |
||
208 | { |
||
209 | if (!function_exists('curl_init')) |
||
210 | { |
||
211 | $this->markTestSkipped('CURL missing: cannot test http 1.1'); |
||
212 | return; |
||
213 | } |
||
214 | $this->method = 'http11'; // not an error the double assignment! |
||
215 | $this->client->method = 'http11'; |
||
216 | $this->client->keepalive = false; |
||
217 | $this->client->accepted_compression = array('gzip'); |
||
218 | $this->client->request_compression = 'gzip'; |
||
219 | |||
220 | $this->$method(); |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * @dataProvider getSingleHttpTestMethods |
||
225 | * @param string $method |
||
226 | */ |
||
227 | public function testHttp11Deflate($method) |
||
228 | { |
||
229 | if (!function_exists('curl_init')) |
||
230 | { |
||
231 | $this->markTestSkipped('CURL missing: cannot test http 1.1'); |
||
232 | return; |
||
233 | } |
||
234 | $this->method = 'http11'; // not an error the double assignment! |
||
235 | $this->client->method = 'http11'; |
||
236 | $this->client->keepalive = false; |
||
237 | $this->client->accepted_compression = array('deflate'); |
||
238 | $this->client->request_compression = 'deflate'; |
||
239 | |||
240 | $this->$method(); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @dataProvider getSingleHttpTestMethods |
||
245 | * @param string $method |
||
246 | */ |
||
247 | public function testHttp11Proxy($method) |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * @dataProvider getSingleHttpTestMethods |
||
270 | * @param string $method |
||
271 | */ |
||
272 | public function testHttps($method) |
||
273 | { |
||
294 | } |
||
295 | |||
296 | /** |
||
297 | * @dataProvider getSingleHttpTestMethods |
||
298 | * @param string $method |
||
299 | */ |
||
300 | public function testHttpsSocket($method) |
||
301 | { |
||
302 | if ($this->args['HTTPSSERVER'] == '') |
||
303 | { |
||
304 | $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https'); |
||
305 | return; |
||
306 | } |
||
307 | |||
308 | /// @todo investigate: can we make this work? |
||
309 | if (version_compare(PHP_VERSION, '7.2', '<')) |
||
310 | { |
||
311 | if (is_readable('/etc/os-release')) { |
||
312 | $output = file_get_contents('/etc/os-release'); |
||
313 | preg_match('/VERSION="?([0-9]+)/', $output, $matches); |
||
314 | $ubuntuVersion = @$matches[1]; |
||
315 | } else { |
||
316 | exec('uname -a', $output, $retval); |
||
317 | preg_match('/ubunutu([0-9]+)/', $output[0], $matches); |
||
318 | $ubuntuVersion = @$matches[1]; |
||
319 | } |
||
320 | if ($ubuntuVersion >= 20) { |
||
321 | $this->markTestSkipped('HTTPS via Socket known to fail on php less than 7.2 on Ubuntu 20 and higher'); |
||
322 | return; |
||
323 | } |
||
324 | } |
||
325 | |||
326 | $this->client->server = $this->args['HTTPSSERVER']; |
||
327 | $this->method = 'https'; |
||
328 | $this->client->method = 'https'; |
||
329 | $this->client->path = $this->args['HTTPSURI']; |
||
330 | $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']); |
||
331 | $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']); |
||
332 | $this->client->setSSLVersion($this->args['SSLVERSION']); |
||
333 | $this->client->setUseCurl(\PhpXmlRpc\Client::USE_CURL_NEVER); |
||
334 | |||
335 | if (version_compare(PHP_VERSION, '8.0', '>')) |
||
336 | { |
||
337 | $version = explode('.', PHP_VERSION); |
||
338 | $this->client->setOption(\PhpXmlRpc\Client::OPT_EXTRA_SOCKET_OPTS, |
||
339 | array('ssl' => array('security_level' => 2 + $version[1]))); |
||
340 | } |
||
341 | |||
342 | $this->$method(); |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @dataProvider getSingleHttpTestMethods |
||
347 | * @param string $method |
||
348 | */ |
||
349 | public function testHttpsProxy($method) |
||
350 | { |
||
351 | if (!function_exists('curl_init')) |
||
352 | { |
||
353 | $this->markTestSkipped('CURL missing: cannot test https w. proxy'); |
||
354 | return; |
||
355 | } |
||
356 | else if ($this->args['PROXYSERVER'] == '') |
||
357 | { |
||
358 | $this->markTestSkipped('PROXYSERVER definition missing: cannot test proxy w. https'); |
||
359 | return; |
||
360 | } |
||
361 | else if ($this->args['HTTPSSERVER'] == '') |
||
362 | { |
||
363 | $this->markTestSkipped('HTTPS SERVER definition missing: cannot test https w. proxy'); |
||
364 | return; |
||
365 | } |
||
366 | |||
367 | $this->client->server = $this->args['HTTPSSERVER']; |
||
368 | $this->method = 'https'; |
||
369 | $this->client->method = 'https'; |
||
370 | $this->client->setProxy($this->args['PROXYSERVER'], $this->args['PROXYPORT']); |
||
371 | $this->client->path = $this->args['HTTPSURI']; |
||
372 | $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']); |
||
373 | $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']); |
||
374 | $this->client->setSSLVersion($this->args['SSLVERSION']); |
||
375 | |||
376 | $this->$method(); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @dataProvider getSingleHttpTestMethods |
||
381 | * @param string $method |
||
382 | */ |
||
383 | public function testHttp2($method) |
||
384 | { |
||
385 | if (!function_exists('curl_init')) |
||
386 | { |
||
387 | $this->markTestSkipped('CURL missing: cannot test http/2'); |
||
388 | return; |
||
389 | } else if (!defined('CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE')) |
||
390 | { |
||
391 | $this->markTestSkipped('CURL http/2 support missing: cannot test http/2'); |
||
392 | return; |
||
393 | } |
||
394 | |||
395 | $this->method = 'h2c'; // not an error the double assignment! |
||
396 | $this->client->method = 'h2c'; |
||
397 | //$this->client->keepalive = false; // q: is this a good idea? |
||
398 | |||
399 | $this->expectHttp2 = true; |
||
400 | $this->$method(); |
||
401 | $this->expectHttp2 = false; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * @dataProvider getSingleHttpTestMethods |
||
406 | * @param string $method |
||
407 | */ |
||
408 | public function testHttp2tls($method) |
||
409 | { |
||
410 | if (!function_exists('curl_init')) |
||
411 | { |
||
412 | $this->markTestSkipped('CURL missing: cannot test http/2 tls'); |
||
413 | return; |
||
414 | } else if ($this->args['HTTPSSERVER'] == '') |
||
415 | { |
||
416 | $this->markTestSkipped('HTTPS SERVER definition missing: cannot test http/2 tls'); |
||
417 | return; |
||
418 | } else if (!defined('CURL_HTTP_VERSION_2_0')) |
||
419 | { |
||
420 | $this->markTestSkipped('CURL http/2 support missing: cannot test http/2 tls'); |
||
421 | return; |
||
422 | } |
||
423 | |||
424 | $this->client->server = $this->args['HTTPSSERVER']; |
||
425 | $this->method = 'h2'; |
||
426 | $this->client->method = 'h2'; |
||
427 | $this->client->path = $this->args['HTTPSURI']; |
||
428 | $this->client->setSSLVerifyPeer(!$this->args['HTTPSIGNOREPEER']); |
||
429 | $this->client->setSSLVerifyHost($this->args['HTTPSVERIFYHOST']); |
||
430 | $this->client->setSSLVersion($this->args['SSLVERSION']); |
||
431 | |||
432 | $this->expectHttp2 = true; |
||
433 | $this->$method(); |
||
434 | $this->expectHttp2 = false; |
||
435 | } |
||
436 | |||
437 | /** |
||
438 | * @dataProvider getSingleHttpTestMethods |
||
439 | * @param string $method |
||
440 | */ |
||
441 | public function testUTF8Responses($method) |
||
442 | { |
||
443 | $this->addQueryParams(array('RESPONSE_ENCODING' => 'UTF-8')); |
||
444 | |||
445 | $this->$method(); |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * @dataProvider getSingleHttpTestMethods |
||
450 | * @param string $method |
||
451 | */ |
||
452 | public function testUTF8Requests($method) |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * @dataProvider getSingleHttpTestMethods |
||
461 | * @param string $method |
||
462 | */ |
||
463 | public function testISOResponses($method) |
||
464 | { |
||
465 | $this->addQueryParams(array('RESPONSE_ENCODING' => 'ISO-8859-1')); |
||
466 | |||
467 | $this->$method(); |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * @dataProvider getSingleHttpTestMethods |
||
472 | * @param string $method |
||
473 | */ |
||
474 | public function testISORequests($method) |
||
475 | { |
||
476 | $this->client->request_charset_encoding = 'ISO-8859-1'; |
||
477 | |||
478 | $this->$method(); |
||
479 | } |
||
480 | |||
481 | /** |
||
482 | * @dataProvider getSingleHttpTestMethods |
||
483 | * @param string $method |
||
484 | */ |
||
485 | public function testBasicAuth($method) |
||
486 | { |
||
487 | $this->client->setCredentials('test', 'test'); |
||
488 | $this->addQueryParams(array('FORCE_AUTH' => 'Basic')); |
||
489 | |||
490 | $this->$method(); |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * @dataProvider getSingleHttpTestMethods |
||
495 | * @param string $method |
||
496 | */ |
||
497 | public function testDigestAuth($method) |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * @param \PhpXmlRpc\Response $r |
||
515 | * @return void |
||
516 | */ |
||
517 | protected function validateResponse($r) |
||
523 | |||
524 | } |
||
525 | } |
||
526 | } |
||
527 |