685bd59842ea0a554396c9c97bebe8ac2ae3c373
[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 class InvalidHostTest extends PHPUnit_Framework_TestCase
8 {
9     /** @var xmlrpc_client $client */
10     public $client = null;
11     public $args = array();
12
13     public function setUp()
14     {
15         $this->args = argParser::getArgs();
16
17         $this->client = new xmlrpc_client('/NOTEXIST.php', $this->args['LOCALSERVER'], 80);
18         if ($this->args['DEBUG']) {
19             $this->client->setDebug($this->args['DEBUG']);
20         }
21     }
22
23     public function test404()
24     {
25         $f = new xmlrpcmsg('examples.echo', array(
26             new xmlrpcval('hello', 'string'),
27         ));
28         $r = $this->client->send($f, 5);
29         $this->assertEquals(5, $r->faultCode());
30     }
31
32     public function testSrvNotFound()
33     {
34         $f = new xmlrpcmsg('examples.echo', array(
35             new xmlrpcval('hello', 'string'),
36         ));
37         $this->client->server .= 'XXX';
38         $r = $this->client->send($f, 5);
39         // make sure there's no freaking catchall DNS in effect
40         $dnsinfo = dns_get_record($this->client->server);
41         if ($dnsinfo) {
42             $this->markTestSkipped('Seems like there is a catchall DNS in effect: host ' . $this->client->server . ' found');
43         } else {
44             $this->assertEquals(5, $r->faultCode());
45         }
46     }
47
48     public function testCurlKAErr()
49     {
50         if (!function_exists('curl_init')) {
51             $this->markTestSkipped('CURL missing: cannot test curl keepalive errors');
52
53             return;
54         }
55         $f = new xmlrpcmsg('examples.stringecho', array(
56             new xmlrpcval('hello', 'string'),
57         ));
58         // test 2 calls w. keepalive: 1st time connection ko, second time ok
59         $this->client->server .= 'XXX';
60         $this->client->keepalive = true;
61         $r = $this->client->send($f, 5, 'http11');
62         // in case we have a "universal dns resolver" getting in the way, we might get a 302 instead of a 404
63         $this->assertTrue($r->faultCode() === 8 || $r->faultCode() == 5);
64
65         // now test a successful connection
66         $server = explode(':', $this->args['LOCALSERVER']);
67         if (count($server) > 1) {
68             $this->client->port = $server[1];
69         }
70         $this->client->server = $server[0];
71         $this->client->path = $this->args['URI'];
72
73         $r = $this->client->send($f, 5, 'http11');
74         $this->assertEquals(0, $r->faultCode());
75         $ro = $r->value();
76         is_object($ro) && $this->assertEquals('hello', $ro->scalarVal());
77     }
78 }