3 include_once __DIR__ . '/../lib/xmlrpc.inc';
4 include_once __DIR__ . '/../lib/xmlrpc_wrappers.inc';
6 include_once __DIR__ . '/parse_args.php';
8 include_once __DIR__ . '/PolyfillTestCase.php';
10 use PHPUnit\Framework\TestResult;
13 * Tests which involve interaction between the client and the server.
14 * They are run against the server found in demo/server.php
16 class LocalhostTest extends PhpXmlRpc_PolyfillTestCase
18 /** @var xmlrpc_client $client */
19 protected $client = null;
20 protected $method = 'http';
21 protected $timeout = 10;
22 protected $request_compression = null;
23 protected $accepted_compression = '';
24 protected $args = array();
26 protected static $failed_tests = array();
29 /** @var boolean $collectCodeCoverageInformation */
30 protected $collectCodeCoverageInformation;
31 protected $coverageScriptUrl;
33 public static function _fail($message = '')
35 // save in a static var that this particular test has failed
36 // (but only if not called from subclass objects / multitests)
37 if (function_exists('debug_backtrace') && strtolower(get_called_class()) == 'localhosttests') {
38 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
39 for ($i = 0; $i < count($trace); $i++) {
40 if (strpos($trace[$i]['function'], 'test') === 0) {
41 self::$failed_tests[$trace[$i]['function']] = true;
47 parent::_fail($message);
51 * Reimplemented to allow us to collect code coverage info from the target server.
52 * Code taken from PHPUnit_Extensions_Selenium2TestCase
54 * @param TestResult $result
58 public function _run($result = NULL)
60 $this->testId = get_class($this) . '__' . $this->getName();
62 if ($result === NULL) {
63 $result = $this->createResult();
66 $this->collectCodeCoverageInformation = $result->getCollectCodeCoverageInformation();
68 parent::_run($result);
70 if ($this->collectCodeCoverageInformation) {
71 $coverage = new PHPUnit_Extensions_SeleniumCommon_RemoteCoverage(
72 $this->coverageScriptUrl,
75 $result->getCodeCoverage()->append(
76 $coverage->get(), $this
80 // do not call this before to give the time to the Listeners to run
81 //$this->getStrategy()->endOfTest($this->session);
86 public function set_up()
88 $this->args = argParser::getArgs();
90 $server = explode(':', $this->args['LOCALSERVER']);
91 if (count($server) > 1) {
92 $this->client = new xmlrpc_client($this->args['URI'], $server[0], $server[1]);
94 $this->client = new xmlrpc_client($this->args['URI'], $this->args['LOCALSERVER']);
97 $this->client->setDebug($this->args['DEBUG']);
98 $this->client->request_compression = $this->request_compression;
99 $this->client->accepted_compression = $this->accepted_compression;
101 $this->coverageScriptUrl = 'http://' . $this->args['LOCALSERVER'] . '/' . str_replace( '/demo/server/server.php', 'tests/phpunit_coverage.php', $this->args['URI'] );
103 if ($this->args['DEBUG'] == 1)
107 protected function tear_down()
109 if ($this->args['DEBUG'] != 1)
111 $out = ob_get_clean();
112 $status = $this->getStatus();
113 if ($status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR
114 || $status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
120 * @param PhpXmlRpc\Request|array $msg
121 * @param int|array $errorCode
122 * @param bool $returnResponse
123 * @return mixed|\PhpXmlRpc\Response|\PhpXmlRpc\Response[]|\PhpXmlRpc\Value|string|null
125 protected function send($msg, $errorCode = 0, $returnResponse = false)
127 if ($this->collectCodeCoverageInformation) {
128 $this->client->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->testId);
131 $r = $this->client->send($msg, $this->timeout, $this->method);
132 // for multicall, return directly array of responses
136 if (is_array($errorCode)) {
137 $this->assertContains($r->faultCode(), $errorCode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
139 $this->assertEquals($errorCode, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
141 if (!$r->faultCode()) {
142 if ($returnResponse) {
153 * Adds (and replaces) query params to the url currently used by the client
156 protected function addQueryParams($data)
158 $query = parse_url($this->client->path, PHP_URL_QUERY);
159 parse_str($query, $vars);
160 $query = http_build_query(array_merge($vars, $data));
161 $this->client->path = parse_url($this->client->path, PHP_URL_PATH) . '?' . $query;
164 public function testString()
166 $sendString = "here are 3 \"entities\": < > & " .
167 "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
168 " - isn't that great? \\\"hackery\\\" at it's best " .
169 " also don't want to miss out on \$item[0]. " .
170 "The real weird stuff follows: CRLF here" . chr(13) . chr(10) .
171 "a simple CR here" . chr(13) .
172 "a simple LF here" . chr(10) .
173 "and then LFCR" . chr(10) . chr(13) .
174 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne, and an xml comment closing tag: -->";
175 $m = new xmlrpcmsg('examples.stringecho', array(
176 new xmlrpcval($sendString, 'string'),
178 $v = $this->send($m);
180 // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
181 // so we relax our tests...
182 $l1 = strlen($sendString);
183 $l2 = strlen($v->scalarval());
185 $this->assertEquals($sendString, $v->scalarval());
187 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendString), $v->scalarval());
192 public function testLatin1String()
195 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne";
196 $x = '<?xml version="1.0" encoding="ISO-8859-1"?><methodCall><methodName>examples.stringecho</methodName><params><param><value>'.
198 '</value></param></params></methodCall>';
199 $v = $this->send($x);
201 $this->assertEquals($sendString, $v->scalarval());
205 public function testExoticCharsetsRequests()
207 // note that we should disable this call also when mbstring is missing server-side
208 if (!function_exists('mb_convert_encoding')) {
209 $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
212 $sendString = 'κόσμε'; // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
213 $str = '<?xml version="1.0" encoding="_ENC_"?>
215 <methodName>examples.stringecho</methodName>
218 <value><string>'.$sendString.'</string></value>
223 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
224 // we have to set the encoding declaration either in the http header or xml prolog, as mb_detect_encoding
225 // (used on the server side) will fail recognizing these 2 charsets
226 $v = $this->send(mb_convert_encoding(str_replace('_ENC_', 'UCS-4', $str), 'UCS-4', 'UTF-8'));
227 $this->assertEquals($sendString, $v->scalarval());
228 $v = $this->send(mb_convert_encoding(str_replace('_ENC_', 'UTF-16', $str), 'UTF-16', 'UTF-8'));
229 $this->assertEquals($sendString, $v->scalarval());
230 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
233 public function testExoticCharsetsRequests2()
235 // note that we should disable this call also when mbstring is missing server-side
236 if (!function_exists('mb_convert_encoding')) {
237 $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
240 $sendString = '安室奈美恵'; // No idea what this means :-) NB: NOT a valid ISO8859 string!
241 $str = '<?xml version="1.0"?>
243 <methodName>examples.stringecho</methodName>
246 <value><string>'.$sendString.'</string></value>
251 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
252 // no encoding declaration either in the http header or xml prolog, let mb_detect_encoding
253 // (used on the server side) sort it out
254 $this->addQueryParams(array('DETECT_ENCODINGS' => array('EUC-JP', 'UTF-8')));
255 $v = $this->send(mb_convert_encoding($str, 'EUC-JP', 'UTF-8'));
256 $this->assertEquals($sendString, $v->scalarval());
257 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
260 public function testExoticCharsetsRequests3()
262 // note that we should disable this call also when mbstring is missing server-side
263 if (!function_exists('mb_convert_encoding')) {
264 $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
267 $sendString = utf8_decode('élève');
268 $str = '<?xml version="1.0"?>
270 <methodName>examples.stringecho</methodName>
273 <value><string>'.$sendString.'</string></value>
278 // no encoding declaration either in the http header or xml prolog, let mb_detect_encoding
279 // (used on the server side) sort it out
280 $this->addQueryParams(array('DETECT_ENCODINGS' => array('ISO-8859-1', 'UTF-8')));
281 $v = $this->send($str);
282 $this->assertEquals($sendString, $v->scalarval());
285 /*public function testLatin1Method()
287 $f = new xmlrpcmsg("tests.iso88591methodname." . chr(224) . chr(252) . chr(232), array(
288 new xmlrpcval('hello')
290 $v = $this->send($f);
292 $this->assertEquals('hello', $v->scalarval());
296 public function testUtf8Method()
298 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
299 $m = new xmlrpcmsg("tests.utf8methodname." . 'κόσμε', array(
300 new xmlrpcval('hello')
302 $v = $this->send($m);
304 $this->assertEquals('hello', $v->scalarval());
306 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
309 public function testAddingDoubles()
311 // note that rounding errors mean we
312 // keep precision to sensible levels here ;-)
315 $m = new xmlrpcmsg('examples.addtwodouble', array(
316 new xmlrpcval($a, 'double'),
317 new xmlrpcval($b, 'double'),
319 $v = $this->send($m);
321 $this->assertEquals($a + $b, $v->scalarval());
325 public function testAdding()
327 $m = new xmlrpcmsg('examples.addtwo', array(
328 new xmlrpcval(12, 'int'),
329 new xmlrpcval(-23, 'int'),
331 $v = $this->send($m);
333 $this->assertEquals(12 - 23, $v->scalarval());
337 public function testInvalidNumber()
339 $m = new xmlrpcmsg('examples.addtwo', array(
340 new xmlrpcval('fred', 'int'),
341 new xmlrpcval("\"; exec('ls')", 'int'),
343 $v = $this->send($m);
344 /// @todo a fault condition should be generated here
345 /// by the server, which we pick up on
347 $this->assertEquals(0, $v->scalarval());
351 public function testBoolean()
353 $m = new xmlrpcmsg('examples.invertBooleans', array(
355 new xmlrpcval(true, 'boolean'),
356 new xmlrpcval(false, 'boolean'),
357 new xmlrpcval(1, 'boolean'),
358 new xmlrpcval(0, 'boolean')
363 $v = $this->send($m);
365 $sz = $v->arraysize();
367 for ($i = 0; $i < $sz; $i++) {
368 $b = $v->arraymem($i);
369 if ($b->scalarval()) {
375 $this->assertEquals($answer, $got);
379 public function testBase64()
381 $sendString = 'Mary had a little lamb,
382 Whose fleece was white as snow,
383 And everywhere that Mary went
384 the lamb was sure to go.
386 Mary had a little lamb
387 She tied it to a pylon
388 Ten thousand volts went down its back
389 And turned it into nylon';
390 $m = new xmlrpcmsg('examples.decode64', array(
391 new xmlrpcval($sendString, 'base64'),
393 $v = $this->send($m);
395 if (strlen($sendString) == strlen($v->scalarval())) {
396 $this->assertEquals($sendString, $v->scalarval());
398 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendString), $v->scalarval());
403 public function testDateTime()
406 $t1 = new xmlrpcval($time, 'dateTime.iso8601');
407 $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
408 $this->assertEquals($t1->serialize(), $t2->serialize());
409 if (class_exists('DateTime')) {
410 $datetime = new DateTime();
411 // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info
412 if (is_callable(array($datetime, 'setTimestamp'))) {
413 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
414 $this->assertEquals($t1->serialize(), $t3->serialize());
419 public function testCountEntities()
421 $sendString = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
422 $m = new xmlrpcmsg('validator1.countTheEntities', array(
423 new xmlrpcval($sendString, 'string'),
425 $v = $this->send($m);
429 $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes');
430 foreach($expect_array as $val) {
431 $b = $v->structmem($val);
432 $got .= $b->me['int'];
434 $this->assertEquals($expected, $got);
438 public function _multicall_msg($method, $params)
440 $struct['methodName'] = new xmlrpcval($method, 'string');
441 $struct['params'] = new xmlrpcval($params, 'array');
443 return new xmlrpcval($struct, 'struct');
446 public function testServerMulticall()
448 // We manually construct a system.multicall() call to ensure
449 // that the server supports it.
451 // NB: This test will NOT pass if server does not support system.multicall.
453 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
454 $good1 = $this->_multicall_msg(
456 array(php_xmlrpc_encode('system.listMethods')));
457 $bad = $this->_multicall_msg(
459 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
460 $recursive = $this->_multicall_msg(
462 array(new xmlrpcval(array(), 'array')));
463 $good2 = $this->_multicall_msg(
464 'system.methodSignature',
465 array(php_xmlrpc_encode('system.listMethods')));
466 $arg = new xmlrpcval(
467 array($good1, $bad, $recursive, $good2),
471 $m = new xmlrpcmsg('system.multicall', array($arg));
472 $v = $this->send($m);
474 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
475 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
477 $r1 = $v->arraymem(0);
479 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
480 "did not get array of size 1 from good1"
483 $r2 = $v->arraymem(1);
485 $r2->kindOf() == 'struct',
489 $r3 = $v->arraymem(2);
491 $r3->kindOf() == 'struct',
492 "recursive system.multicall did not fail"
495 $r4 = $v->arraymem(3);
497 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
498 "did not get array of size 1 from good2"
503 public function testClientMulticall1()
505 // NB: This test will NOT pass if server does not support system.multicall.
507 $noMultiCall = $this->client->no_multicall;
508 $this->client->no_multicall = false;
510 $good1 = new xmlrpcmsg('system.methodHelp',
511 array(php_xmlrpc_encode('system.listMethods')));
512 $bad = new xmlrpcmsg('test.nosuch',
513 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
514 $recursive = new xmlrpcmsg('system.multicall',
515 array(new xmlrpcval(array(), 'array')));
516 $good2 = new xmlrpcmsg('system.methodSignature',
517 array(php_xmlrpc_encode('system.listMethods'))
520 $r = $this->send(array($good1, $bad, $recursive, $good2));
522 $this->assertTrue(count($r) == 4, "wrong number of return values");
525 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
526 if (!$r[0]->faultCode()) {
527 $val = $r[0]->value();
529 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
530 "good1 did not return string"
533 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
534 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
535 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
536 if (!$r[3]->faultCode()) {
537 $val = $r[3]->value();
538 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
540 // This is the only assert in this test which should fail
541 // if the test server does not support system.multicall.
542 $this->assertTrue($this->client->no_multicall == false,
543 "server does not support system.multicall"
546 $this->client->no_multicall = $noMultiCall;
549 public function testClientMulticall2()
551 // NB: This test will NOT pass if server does not support system.multicall.
553 $noMultiCall = $this->client->no_multicall;
554 $this->client->no_multicall = true;
556 $good1 = new xmlrpcmsg('system.methodHelp',
557 array(php_xmlrpc_encode('system.listMethods')));
558 $bad = new xmlrpcmsg('test.nosuch',
559 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
560 $recursive = new xmlrpcmsg('system.multicall',
561 array(new xmlrpcval(array(), 'array')));
562 $good2 = new xmlrpcmsg('system.methodSignature',
563 array(php_xmlrpc_encode('system.listMethods'))
566 $r = $this->send(array($good1, $bad, $recursive, $good2));
568 $this->assertTrue(count($r) == 4, "wrong number of return values");
571 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
572 if (!$r[0]->faultCode()) {
573 $val = $r[0]->value();
575 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
576 "good1 did not return string");
578 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
579 $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
580 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
581 if (!$r[3]->faultCode()) {
582 $val = $r[3]->value();
583 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
586 $this->client->no_multicall = $noMultiCall;
589 public function testClientMulticall3()
591 // NB: This test will NOT pass if server does not support system.multicall.
593 $noMultiCall = $this->client->no_multicall;
594 $returnType = $this->client->return_type;
596 $this->client->return_type = 'phpvals';
597 $this->client->no_multicall = false;
599 $good1 = new xmlrpcmsg('system.methodHelp',
600 array(php_xmlrpc_encode('system.listMethods')));
601 $bad = new xmlrpcmsg('test.nosuch',
602 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
603 $recursive = new xmlrpcmsg('system.multicall',
604 array(new xmlrpcval(array(), 'array')));
605 $good2 = new xmlrpcmsg('system.methodSignature',
606 array(php_xmlrpc_encode('system.listMethods'))
609 $r = $this->send(array($good1, $bad, $recursive, $good2));
611 $this->assertTrue(count($r) == 4, "wrong number of return values");
613 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
614 if (!$r[0]->faultCode()) {
615 $val = $r[0]->value();
617 is_string($val), "good1 did not return string");
619 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
620 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
621 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
622 if (!$r[3]->faultCode()) {
623 $val = $r[3]->value();
624 $this->assertTrue(is_array($val), "good2 did not return array");
627 $this->client->return_type = $returnType;
628 $this->client->no_multicall = $noMultiCall;
631 public function testCatchWarnings()
633 $m = new xmlrpcmsg('tests.generatePHPWarning', array(
634 new xmlrpcval('whatever', 'string'),
636 $v = $this->send($m);
638 $this->assertEquals(true, $v->scalarval());
642 public function testCatchExceptions()
644 $m = new xmlrpcmsg('tests.raiseException', array(
645 new xmlrpcval('whatever', 'string'),
647 $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
648 $this->addQueryParams(array('EXCEPTION_HANDLING' => 1));
649 $v = $this->send($m, 1); // the error code of the expected exception
650 $this->addQueryParams(array('EXCEPTION_HANDLING' => 2));
651 // depending on whether display_errors is ON or OFF on the server, we will get back a different error here,
652 // as php will generate an http status code of either 200 or 500...
653 $v = $this->send($m, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error']));
656 public function testZeroParams()
658 $m = new xmlrpcmsg('system.listMethods');
659 $v = $this->send($m);
662 public function testNullParams()
664 $m = new xmlrpcmsg('tests.getStateName.12', array(
665 new xmlrpcval('whatever', 'null'),
666 new xmlrpcval(23, 'int'),
668 $v = $this->send($m);
670 $this->assertEquals('Michigan', $v->scalarval());
672 $m = new xmlrpcmsg('tests.getStateName.12', array(
673 new xmlrpcval(23, 'int'),
674 new xmlrpcval('whatever', 'null'),
676 $v = $this->send($m);
678 $this->assertEquals('Michigan', $v->scalarval());
680 $m = new xmlrpcmsg('tests.getStateName.12', array(
681 new xmlrpcval(23, 'int')
683 $v = $this->send($m, array($GLOBALS['xmlrpcerr']['incorrect_params']));
686 public function testCodeInjectionServerSide()
688 $m = new xmlrpcmsg('system.MethodHelp');
689 $m->payload = "<?xml version=\"1.0\"?><methodCall><methodName>validator1.echoStructTest</methodName><params><param><value><struct><member><name>','')); echo('gotcha!'); die(); //</name></member></struct></value></param></params></methodCall>";
690 $v = $this->send($m);
692 $this->assertEquals(0, $v->structsize());
696 public function testServerWrappedFunction()
698 $m = new xmlrpcmsg('tests.getStateName.2', array(
699 new xmlrpcval(23, 'int'),
701 $v = $this->send($m);
702 $this->assertEquals('Michigan', $v->scalarval());
704 // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response
705 $m = new xmlrpcmsg('tests.getStateName.2', array(
706 new xmlrpcval(0, 'int'),
708 $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
710 // check if the generated function dispatch map is fine, by checking if the server registered it
711 $m = new xmlrpcmsg('system.methodSignature', array(
712 new xmlrpcval('tests.getStateName.2'),
714 $v = $this->send($m);
715 $encoder = new \PhpXmlRpc\Encoder();
716 $this->assertEquals(array(array('string', 'int')), $encoder->decode($v));
719 public function testServerWrappedFunctionAsSource()
721 $m = new xmlrpcmsg('tests.getStateName.6', array(
722 new xmlrpcval(23, 'int'),
724 $v = $this->send($m);
725 $this->assertEquals('Michigan', $v->scalarval());
727 // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response
728 $m = new xmlrpcmsg('tests.getStateName.6', array(
729 new xmlrpcval(0, 'int'),
731 $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
734 public function testServerWrappedObjectMethods()
736 $m = new xmlrpcmsg('tests.getStateName.3', array(
737 new xmlrpcval(23, 'int'),
739 $v = $this->send($m);
740 $this->assertEquals('Michigan', $v->scalarval());
742 $m = new xmlrpcmsg('tests.getStateName.4', array(
743 new xmlrpcval(23, 'int'),
745 $v = $this->send($m);
746 $this->assertEquals('Michigan', $v->scalarval());
748 $m = new xmlrpcmsg('tests.getStateName.5', array(
749 new xmlrpcval(23, 'int'),
751 $v = $this->send($m);
752 $this->assertEquals('Michigan', $v->scalarval());
754 $m = new xmlrpcmsg('tests.getStateName.7', array(
755 new xmlrpcval(23, 'int'),
757 $v = $this->send($m);
758 $this->assertEquals('Michigan', $v->scalarval());
760 $m = new xmlrpcmsg('tests.getStateName.8', array(
761 new xmlrpcval(23, 'int'),
763 $v = $this->send($m);
764 $this->assertEquals('Michigan', $v->scalarval());
766 $m = new xmlrpcmsg('tests.getStateName.9', array(
767 new xmlrpcval(23, 'int'),
769 $v = $this->send($m);
770 $this->assertEquals('Michigan', $v->scalarval());
773 public function testServerWrappedObjectMethodsAsSource()
775 $m = new xmlrpcmsg('tests.getStateName.7', array(
776 new xmlrpcval(23, 'int'),
778 $v = $this->send($m);
779 $this->assertEquals('Michigan', $v->scalarval());
781 $m = new xmlrpcmsg('tests.getStateName.8', array(
782 new xmlrpcval(23, 'int'),
784 $v = $this->send($m);
785 $this->assertEquals('Michigan', $v->scalarval());
787 $m = new xmlrpcmsg('tests.getStateName.9', array(
788 new xmlrpcval(23, 'int'),
790 $v = $this->send($m);
791 $this->assertEquals('Michigan', $v->scalarval());
794 public function testServerClosure()
796 $m = new xmlrpcmsg('tests.getStateName.10', array(
797 new xmlrpcval(23, 'int'),
799 $v = $this->send($m);
800 $this->assertEquals('Michigan', $v->scalarval());
803 public function testServerWrappedClosure()
805 $m = new xmlrpcmsg('tests.getStateName.11', array(
806 new xmlrpcval(23, 'int'),
808 $v = $this->send($m);
809 $this->assertEquals('Michigan', $v->scalarval());
812 public function testServerWrappedClass()
814 $m = new xmlrpcmsg('tests.xmlrpcServerMethodsContainer.findState', array(
815 new xmlrpcval(23, 'int'),
817 $v = $this->send($m);
818 $this->assertEquals('Michigan', $v->scalarval());
821 public function testWrappedMethod()
823 // make a 'deep client copy' as the original one might have many properties set
824 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0));
825 if ($func == false) {
826 $this->fail('Registration of examples.getStateName failed');
829 // work around bug in current (or old?) version of phpunit when reporting the error
830 /*if (is_object($v)) {
831 $v = var_export($v, true);
833 $this->assertEquals('Michigan', $v);
837 public function testWrappedMethodAsSource()
839 // make a 'deep client copy' as the original one might have many properties set
840 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0, 'return_source' => true));
841 if ($func == false) {
842 $this->fail('Registration of examples.getStateName failed');
844 eval($func['source']);
845 $func = $func['function'];
847 // work around bug in current (or old?) version of phpunit when reporting the error
848 /*if (is_object($v)) {
849 $v = var_export($v, true);
851 $this->assertEquals('Michigan', $v);
855 public function testWrappedClass()
857 // make a 'deep client copy' as the original one might have many properties set
858 // also for speed only wrap one method of the whole server
859 $class = wrap_xmlrpc_server($this->client, array('simple_client_copy' => 0, 'method_filter' => '/examples\.getStateName/' ));
861 $this->fail('Registration of remote server failed');
864 if (!is_callable(array($obj, 'examples_getStateName'))) {
865 $this->fail('Registration of remote server failed to import method "examples_getStateName"');
867 $v = $obj->examples_getStateName(23);
868 // work around bug in current (or old?) version of phpunit when reporting the error
869 /*if (is_object($v)) {
870 $v = var_export($v, true);
872 $this->assertEquals('Michigan', $v);
877 public function testTransferOfObjectViaWrapping()
879 // make a 'deep client copy' as the original one might have many properties set
880 $func = wrap_xmlrpc_method($this->client, 'tests.returnPhpObject', array('simple_client_copy' => 0,
881 'decode_php_objs' => true));
882 if ($func == false) {
883 $this->fail('Registration of tests.returnPhpObject failed');
886 $obj = new stdClass();
887 $obj->hello = 'world';
888 $this->assertEquals($obj, $v);
892 public function testGetCookies()
894 // let server set to us some cookies we tell it
897 'c2' => array('value' => 'c2'),
898 'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
899 'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
900 'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
902 $cookiesval = php_xmlrpc_encode($cookies);
903 $m = new xmlrpcmsg('examples.setcookies', array($cookiesval));
904 $r = $this->send($m, 0, true);
907 $this->assertEquals(1, $v->scalarval());
908 // now check if we decoded the cookies as we had set them
909 $rcookies = $r->cookies();
910 // remove extra cookies which might have been set by proxies
911 foreach ($rcookies as $c => $v) {
912 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
913 unset($rcookies[$c]);
915 // Seems like we get this when using php-fpm and php 5.5+ ...
916 if (isset($rcookies[$c]['Max-Age'])) {
917 unset($rcookies[$c]['Max-Age']);
920 foreach ($cookies as $c => $v) {
921 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
922 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
923 if (isset($v['expires'])) {
924 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
925 $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
927 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
932 $this->assertEquals($cookies, $rcookies);
936 public function testSetCookies()
938 // let server set to us some cookies we tell it
943 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
945 $m = new xmlrpcmsg('examples.getcookies', array());
946 foreach ($cookies as $cookie => $val) {
947 $this->client->setCookie($cookie, $val);
948 $cookies[$cookie] = (string)$cookies[$cookie];
950 $r = $this->client->send($m, $this->timeout, $this->method);
951 $this->assertEquals(0, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
952 if (!$r->faultCode()) {
954 $v = php_xmlrpc_decode($v);
956 // take care for the extra cookie used for coverage collection
957 if (isset($v['PHPUNIT_SELENIUM_TEST_ID'])) {
958 unset($v['PHPUNIT_SELENIUM_TEST_ID']);
961 // on IIS and Apache getallheaders returns something slightly different...
962 $this->assertEquals($cookies, $v);
966 public function testServerComments()
968 $m = new xmlrpcmsg('tests.xmlrpcServerMethodsContainer.debugMessageGenerator', array(
969 new xmlrpcval('hello world', 'string'),
971 $r = $this->send($m, 0, true);
972 $this->assertContains('hello world', $r->raw_data);
975 public function testSendTwiceSameMsg()
977 $m = new xmlrpcmsg('examples.stringecho', array(
978 new xmlrpcval('hello world', 'string'),
980 $v1 = $this->send($m);
981 $v2 = $this->send($m);
983 $this->assertEquals($v1, $v2);