Passed
Pull Request — master (#84)
by
unknown
10:39
created

ClientTest::testCurlKAErr()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
c 0
b 0
f 0
nc 5
nop 0
dl 0
loc 29
rs 9.3554
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 the Client class.
13
 * So far: only tests requests sent to non-existing servers
14
 */
15
class ClientTest extends PhpXmlRpc_PolyfillTestCase
16
{
17
    /** @var xmlrpc_client $client */
18
    public $client = null;
19
    public $args = array();
20
21
    public function set_up()
22
    {
23
        $this->args = argParser::getArgs();
24
25
        $this->client = new xmlrpc_client('/NOTEXIST.php', $this->args['HTTPSERVER'], 80);
26
        $this->client->setDebug($this->args['DEBUG']);
27
28
        if ($this->args['DEBUG'] == 1)
29
            ob_start();
30
    }
31
32
    protected function tear_down()
33
    {
34
        if ($this->args['DEBUG'] != 1)
35
            return;
36
        $out = ob_get_clean();
37
        $status = $this->getStatus();
38
        if ($status == BaseTestRunner::STATUS_ERROR
39
            || $status == BaseTestRunner::STATUS_FAILURE) {
40
            echo $out;
41
        }
42
    }
43
44
    public function test404()
45
    {
46
        $m = new xmlrpcmsg('examples.echo', array(
47
            new xmlrpcval('hello', 'string'),
48
        ));
49
        $r = $this->client->send($m, 5);
50
        $this->assertEquals(5, $r->faultCode());
51
    }
52
53
    public function testSrvNotFound()
54
    {
55
        $m = new xmlrpcmsg('examples.echo', array(
56
            new xmlrpcval('hello', 'string'),
57
        ));
58
        $this->client->server .= 'XXX';
59
        $dnsinfo = @dns_get_record($this->client->server);
60
        if ($dnsinfo) {
61
            $this->markTestSkipped('Seems like there is a catchall DNS in effect: host ' . $this->client->server . ' found');
62
        } else {
63
            $r = $this->client->send($m, 5);
64
            // make sure there's no freaking catchall DNS in effect
65
            $this->assertEquals(5, $r->faultCode());
66
        }
67
    }
68
69
    public function testTimedOutSendPayloadSocket()
70
    {
71
        $m = new xmlrpcmsg('examples.timedout');
72
73
        // now test a successful connection
74
        $server = explode(':', $this->args['HTTPSERVER']);
75
        if (count($server) > 1) {
76
            $this->client->port = $server[1];
77
        }
78
        $this->client->server = $server[0];
79
        $this->client->path = $this->args['HTTPURI'];
80
81
        $r = $this->client->send($m, 5);
82
        $this->assertEquals(19, $r->faultCode());
83
    }
84
85
    public function testTimedOutSendPayloadCURL()
86
    {
87
        if (!function_exists('curl_init')) {
88
            $this->markTestSkipped('CURL missing: cannot test curl keepalive errors');
89
90
            return;
91
        }
92
93
        $m = new xmlrpcmsg('examples.timedout');
94
95
        // now test a successful connection
96
        $server = explode(':', $this->args['HTTPSERVER']);
97
        if (count($server) > 1) {
98
            $this->client->port = $server[1];
99
        }
100
        $this->client->server = $server[0];
101
        $this->client->path = $this->args['HTTPURI'];
102
103
        $r = $this->client->send($m, 5, 'http11');
104
        $this->assertContains('CURL error: Operation timed out', $r->faultString());
105
    }
106
107
    public function testCurlKAErr()
108
    {
109
        if (!function_exists('curl_init')) {
110
            $this->markTestSkipped('CURL missing: cannot test curl keepalive errors');
111
112
            return;
113
        }
114
        $m = new xmlrpcmsg('examples.stringecho', array(
115
            new xmlrpcval('hello', 'string'),
116
        ));
117
        // test 2 calls w. keepalive: 1st time connection ko, second time ok
118
        $this->client->server .= 'XXX';
119
        $this->client->keepalive = true;
120
        $r = $this->client->send($m, 5, 'http11');
121
        // in case we have a "universal dns resolver" getting in the way, we might get a 302 instead of a 404
122
        $this->assertTrue($r->faultCode() === 8 || $r->faultCode() == 5);
123
124
        // now test a successful connection
125
        $server = explode(':', $this->args['HTTPSERVER']);
126
        if (count($server) > 1) {
127
            $this->client->port = $server[1];
128
        }
129
        $this->client->server = $server[0];
130
        $this->client->path = $this->args['HTTPURI'];
131
132
        $r = $this->client->send($m, 5, 'http11');
133
        $this->assertEquals(0, $r->faultCode());
134
        $ro = $r->value();
135
        is_object($ro) && $this->assertEquals('hello', $ro->scalarVal());
136
    }
137
}
138