test cleanups
[plcapi.git] / tests / 2InvalidHostTest.php
1 <?php
2
3 include_once __DIR__ . '/../lib/xmlrpc.inc';
4
5 include_once __DIR__ . '/parse_args.php';
6
7 include_once __DIR__ . '/PolyfillTestCase.php';
8
9 use PHPUnit\Runner\BaseTestRunner;
10
11 /**
12  * Tests involving requests sent to non-existing servers
13  */
14 class InvalidHostTest extends PhpXmlRpc_PolyfillTestCase
15 {
16     /** @var xmlrpc_client $client */
17     public $client = null;
18     public $args = array();
19
20     public function set_up()
21     {
22         $this->args = argParser::getArgs();
23
24         $this->client = new xmlrpc_client('/NOTEXIST.php', $this->args['HTTPSERVER'], 80);
25         $this->client->setDebug($this->args['DEBUG']);
26
27         if ($this->args['DEBUG'] == 1)
28             ob_start();
29     }
30
31     protected function tear_down()
32     {
33         if ($this->args['DEBUG'] != 1)
34             return;
35         $out = ob_get_clean();
36         $status = $this->getStatus();
37         if ($status == BaseTestRunner::STATUS_ERROR
38             || $status == BaseTestRunner::STATUS_FAILURE) {
39             echo $out;
40         }
41     }
42
43     public function test404()
44     {
45         $m = new xmlrpcmsg('examples.echo', array(
46             new xmlrpcval('hello', 'string'),
47         ));
48         $r = $this->client->send($m, 5);
49         $this->assertEquals(5, $r->faultCode());
50     }
51
52     public function testSrvNotFound()
53     {
54         $m = new xmlrpcmsg('examples.echo', array(
55             new xmlrpcval('hello', 'string'),
56         ));
57         $this->client->server .= 'XXX';
58         $r = $this->client->send($m, 5);
59         // make sure there's no freaking catchall DNS in effect
60         $dnsinfo = dns_get_record($this->client->server);
61         if ($dnsinfo) {
62             $this->markTestSkipped('Seems like there is a catchall DNS in effect: host ' . $this->client->server . ' found');
63         } else {
64             $this->assertEquals(5, $r->faultCode());
65         }
66     }
67
68     public function testCurlKAErr()
69     {
70         if (!function_exists('curl_init')) {
71             $this->markTestSkipped('CURL missing: cannot test curl keepalive errors');
72
73             return;
74         }
75         $m = new xmlrpcmsg('examples.stringecho', array(
76             new xmlrpcval('hello', 'string'),
77         ));
78         // test 2 calls w. keepalive: 1st time connection ko, second time ok
79         $this->client->server .= 'XXX';
80         $this->client->keepalive = true;
81         $r = $this->client->send($m, 5, 'http11');
82         // in case we have a "universal dns resolver" getting in the way, we might get a 302 instead of a 404
83         $this->assertTrue($r->faultCode() === 8 || $r->faultCode() == 5);
84
85         // now test a successful connection
86         $server = explode(':', $this->args['HTTPSERVER']);
87         if (count($server) > 1) {
88             $this->client->port = $server[1];
89         }
90         $this->client->server = $server[0];
91         $this->client->path = $this->args['HTTPURI'];
92
93         $r = $this->client->send($m, 5, 'http11');
94         $this->assertEquals(0, $r->faultCode());
95         $ro = $r->value();
96         is_object($ro) && $this->assertEquals('hello', $ro->scalarVal());
97     }
98 }