3 include_once __DIR__ . '/../lib/xmlrpc.inc';
4 include_once __DIR__ . '/../lib/xmlrpc_wrappers.inc';
6 include_once __DIR__ . '/parse_args.php';
9 * Tests which involve interaction between the client and the server.
10 * They are run against the server found in demo/server.php
12 class LocalhostTest extends PHPUnit_Framework_TestCase
14 /** @var xmlrpc_client $client */
15 protected $client = null;
16 protected $method = 'http';
17 protected $timeout = 10;
18 protected $request_compression = null;
19 protected $accepted_compression = '';
20 protected $args = array();
22 protected static $failed_tests = array();
25 /** @var boolean $collectCodeCoverageInformation */
26 protected $collectCodeCoverageInformation;
27 protected $coverageScriptUrl;
29 public static function fail($message = '')
31 // save in a static var that this particular test has failed
32 // (but only if not called from subclass objects / multitests)
33 if (function_exists('debug_backtrace') && strtolower(get_called_class()) == 'localhosttests') {
34 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
35 for ($i = 0; $i < count($trace); $i++) {
36 if (strpos($trace[$i]['function'], 'test') === 0) {
37 self::$failed_tests[$trace[$i]['function']] = true;
43 parent::fail($message);
47 * Reimplemented to allow us to collect code coverage info from the target server.
48 * Code taken from PHPUnit_Extensions_Selenium2TestCase
50 * @param PHPUnit_Framework_TestResult $result
51 * @return PHPUnit_Framework_TestResult
54 public function run(PHPUnit_Framework_TestResult $result = NULL)
56 $this->testId = get_class($this) . '__' . $this->getName();
58 if ($result === NULL) {
59 $result = $this->createResult();
62 $this->collectCodeCoverageInformation = $result->getCollectCodeCoverageInformation();
66 if ($this->collectCodeCoverageInformation) {
67 $coverage = new PHPUnit_Extensions_SeleniumCommon_RemoteCoverage(
68 $this->coverageScriptUrl,
71 $result->getCodeCoverage()->append(
72 $coverage->get(), $this
76 // do not call this before to give the time to the Listeners to run
77 //$this->getStrategy()->endOfTest($this->session);
82 public function setUp()
84 $this->args = argParser::getArgs();
86 $server = explode(':', $this->args['LOCALSERVER']);
87 if (count($server) > 1) {
88 $this->client = new xmlrpc_client($this->args['URI'], $server[0], $server[1]);
90 $this->client = new xmlrpc_client($this->args['URI'], $this->args['LOCALSERVER']);
93 $this->client->setDebug($this->args['DEBUG']);
94 $this->client->request_compression = $this->request_compression;
95 $this->client->accepted_compression = $this->accepted_compression;
97 $this->coverageScriptUrl = 'http://' . $this->args['LOCALSERVER'] . '/' . str_replace( '/demo/server/server.php', 'tests/phpunit_coverage.php', $this->args['URI'] );
99 if ($this->args['DEBUG'] == 1)
103 protected function tearDown()
105 if ($this->args['DEBUG'] != 1)
107 $out = ob_get_clean();
108 $status = $this->getStatus();
109 if ($status == PHPUnit_Runner_BaseTestRunner::STATUS_ERROR
110 || $status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
116 * @param PhpXmlRpc\Request|array $msg
117 * @param int|array $errorCode
118 * @param bool $returnResponse
119 * @return mixed|\PhpXmlRpc\Response|\PhpXmlRpc\Response[]|\PhpXmlRpc\Value|string|null
121 protected function send($msg, $errorCode = 0, $returnResponse = false)
123 if ($this->collectCodeCoverageInformation) {
124 $this->client->setCookie('PHPUNIT_SELENIUM_TEST_ID', $this->testId);
127 $r = $this->client->send($msg, $this->timeout, $this->method);
128 // for multicall, return directly array of responses
132 if (is_array($errorCode)) {
133 $this->assertContains($r->faultCode(), $errorCode, 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
135 $this->assertEquals($errorCode, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
137 if (!$r->faultCode()) {
138 if ($returnResponse) {
149 * Adds (and replaces) query params to the url currently used by the client
152 protected function addQueryParams($data)
154 $query = parse_url($this->client->path, PHP_URL_QUERY);
155 parse_str($query, $vars);
156 $query = http_build_query(array_merge($vars, $data));
157 $this->client->path = parse_url($this->client->path, PHP_URL_PATH) . '?' . $query;
160 public function testString()
162 $sendString = "here are 3 \"entities\": < > & " .
163 "and here's a dollar sign: \$pretendvarname and a backslash too: " . chr(92) .
164 " - isn't that great? \\\"hackery\\\" at it's best " .
165 " also don't want to miss out on \$item[0]. " .
166 "The real weird stuff follows: CRLF here" . chr(13) . chr(10) .
167 "a simple CR here" . chr(13) .
168 "a simple LF here" . chr(10) .
169 "and then LFCR" . chr(10) . chr(13) .
170 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne, and an xml comment closing tag: -->";
171 $m = new xmlrpcmsg('examples.stringecho', array(
172 new xmlrpcval($sendString, 'string'),
174 $v = $this->send($m);
176 // when sending/receiving non-US-ASCII encoded strings, XML says cr-lf can be normalized.
177 // so we relax our tests...
178 $l1 = strlen($sendString);
179 $l2 = strlen($v->scalarval());
181 $this->assertEquals($sendString, $v->scalarval());
183 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendString), $v->scalarval());
188 public function testLatin1String()
191 "last but not least weird names: G" . chr(252) . "nter, El" . chr(232) . "ne";
192 $x = '<?xml version="1.0" encoding="ISO-8859-1"?><methodCall><methodName>examples.stringecho</methodName><params><param><value>'.
194 '</value></param></params></methodCall>';
195 $v = $this->send($x);
197 $this->assertEquals($sendString, $v->scalarval());
201 public function testExoticCharsetsRequests()
203 // note that we should disable this call also when mbstring is missing server-side
204 if (!function_exists('mb_convert_encoding')) {
205 $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
208 $sendString = 'κόσμε'; // Greek word 'kosme'. NB: NOT a valid ISO8859 string!
209 $str = '<?xml version="1.0" encoding="_ENC_"?>
211 <methodName>examples.stringecho</methodName>
214 <value><string>'.$sendString.'</string></value>
219 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
220 // we have to set the encoding declaration either in the http header or xml prolog, as mb_detect_encoding
221 // (used on the server side) will fail recognizing these 2 charsets
222 $v = $this->send(mb_convert_encoding(str_replace('_ENC_', 'UCS-4', $str), 'UCS-4', 'UTF-8'));
223 $this->assertEquals($sendString, $v->scalarval());
224 $v = $this->send(mb_convert_encoding(str_replace('_ENC_', 'UTF-16', $str), 'UTF-16', 'UTF-8'));
225 $this->assertEquals($sendString, $v->scalarval());
226 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
229 public function testExoticCharsetsRequests2()
231 // note that we should disable this call also when mbstring is missing server-side
232 if (!function_exists('mb_convert_encoding')) {
233 $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
236 $sendString = '安室奈美恵'; // No idea what this means :-) NB: NOT a valid ISO8859 string!
237 $str = '<?xml version="1.0"?>
239 <methodName>examples.stringecho</methodName>
242 <value><string>'.$sendString.'</string></value>
247 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
248 // no encoding declaration either in the http header or xml prolog, let mb_detect_encoding
249 // (used on the server side) sort it out
250 $this->addQueryParams(array('DETECT_ENCODINGS' => array('EUC-JP', 'UTF-8')));
251 $v = $this->send(mb_convert_encoding($str, 'EUC-JP', 'UTF-8'));
252 $this->assertEquals($sendString, $v->scalarval());
253 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
256 public function testExoticCharsetsRequests3()
258 // note that we should disable this call also when mbstring is missing server-side
259 if (!function_exists('mb_convert_encoding')) {
260 $this->markTestSkipped('Miss mbstring extension to test exotic charsets');
263 $sendString = utf8_decode('élève');
264 $str = '<?xml version="1.0"?>
266 <methodName>examples.stringecho</methodName>
269 <value><string>'.$sendString.'</string></value>
274 // no encoding declaration either in the http header or xml prolog, let mb_detect_encoding
275 // (used on the server side) sort it out
276 $this->addQueryParams(array('DETECT_ENCODINGS' => array('ISO-8859-1', 'UTF-8')));
277 $v = $this->send($str);
278 $this->assertEquals($sendString, $v->scalarval());
281 /*public function testLatin1Method()
283 $f = new xmlrpcmsg("tests.iso88591methodname." . chr(224) . chr(252) . chr(232), array(
284 new xmlrpcval('hello')
286 $v = $this->send($f);
288 $this->assertEquals('hello', $v->scalarval());
292 public function testUtf8Method()
294 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'UTF-8';
295 $m = new xmlrpcmsg("tests.utf8methodname." . 'κόσμε', array(
296 new xmlrpcval('hello')
298 $v = $this->send($m);
300 $this->assertEquals('hello', $v->scalarval());
302 PhpXmlRpc\PhpXmlRpc::$xmlrpc_internalencoding = 'ISO-8859-1';
305 public function testAddingDoubles()
307 // note that rounding errors mean we
308 // keep precision to sensible levels here ;-)
311 $m = new xmlrpcmsg('examples.addtwodouble', array(
312 new xmlrpcval($a, 'double'),
313 new xmlrpcval($b, 'double'),
315 $v = $this->send($m);
317 $this->assertEquals($a + $b, $v->scalarval());
321 public function testAdding()
323 $m = new xmlrpcmsg('examples.addtwo', array(
324 new xmlrpcval(12, 'int'),
325 new xmlrpcval(-23, 'int'),
327 $v = $this->send($m);
329 $this->assertEquals(12 - 23, $v->scalarval());
333 public function testInvalidNumber()
335 $m = new xmlrpcmsg('examples.addtwo', array(
336 new xmlrpcval('fred', 'int'),
337 new xmlrpcval("\"; exec('ls')", 'int'),
339 $v = $this->send($m);
340 /// @todo a fault condition should be generated here
341 /// by the server, which we pick up on
343 $this->assertEquals(0, $v->scalarval());
347 public function testBoolean()
349 $m = new xmlrpcmsg('examples.invertBooleans', array(
351 new xmlrpcval(true, 'boolean'),
352 new xmlrpcval(false, 'boolean'),
353 new xmlrpcval(1, 'boolean'),
354 new xmlrpcval(0, 'boolean')
359 $v = $this->send($m);
361 $sz = $v->arraysize();
363 for ($i = 0; $i < $sz; $i++) {
364 $b = $v->arraymem($i);
365 if ($b->scalarval()) {
371 $this->assertEquals($answer, $got);
375 public function testBase64()
377 $sendString = 'Mary had a little lamb,
378 Whose fleece was white as snow,
379 And everywhere that Mary went
380 the lamb was sure to go.
382 Mary had a little lamb
383 She tied it to a pylon
384 Ten thousand volts went down its back
385 And turned it into nylon';
386 $m = new xmlrpcmsg('examples.decode64', array(
387 new xmlrpcval($sendString, 'base64'),
389 $v = $this->send($m);
391 if (strlen($sendString) == strlen($v->scalarval())) {
392 $this->assertEquals($sendString, $v->scalarval());
394 $this->assertEquals(str_replace(array("\r\n", "\r"), array("\n", "\n"), $sendString), $v->scalarval());
399 public function testDateTime()
402 $t1 = new xmlrpcval($time, 'dateTime.iso8601');
403 $t2 = new xmlrpcval(iso8601_encode($time), 'dateTime.iso8601');
404 $this->assertEquals($t1->serialize(), $t2->serialize());
405 if (class_exists('DateTime')) {
406 $datetime = new DateTime();
407 // skip this test for php 5.2. It is a bit harder there to build a DateTime from unix timestamp with proper TZ info
408 if (is_callable(array($datetime, 'setTimestamp'))) {
409 $t3 = new xmlrpcval($datetime->setTimestamp($time), 'dateTime.iso8601');
410 $this->assertEquals($t1->serialize(), $t3->serialize());
415 public function testCountEntities()
417 $sendString = "h'fd>onc>>l>>rw&bpu>q>e<v&gxs<ytjzkami<";
418 $m = new xmlrpcmsg('validator1.countTheEntities', array(
419 new xmlrpcval($sendString, 'string'),
421 $v = $this->send($m);
425 $expect_array = array('ctLeftAngleBrackets', 'ctRightAngleBrackets', 'ctAmpersands', 'ctApostrophes', 'ctQuotes');
426 foreach($expect_array as $val) {
427 $b = $v->structmem($val);
428 $got .= $b->me['int'];
430 $this->assertEquals($expected, $got);
434 public function _multicall_msg($method, $params)
436 $struct['methodName'] = new xmlrpcval($method, 'string');
437 $struct['params'] = new xmlrpcval($params, 'array');
439 return new xmlrpcval($struct, 'struct');
442 public function testServerMulticall()
444 // We manually construct a system.multicall() call to ensure
445 // that the server supports it.
447 // NB: This test will NOT pass if server does not support system.multicall.
449 // Based on http://xmlrpc-c.sourceforge.net/hacks/test_multicall.py
450 $good1 = $this->_multicall_msg(
452 array(php_xmlrpc_encode('system.listMethods')));
453 $bad = $this->_multicall_msg(
455 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
456 $recursive = $this->_multicall_msg(
458 array(new xmlrpcval(array(), 'array')));
459 $good2 = $this->_multicall_msg(
460 'system.methodSignature',
461 array(php_xmlrpc_encode('system.listMethods')));
462 $arg = new xmlrpcval(
463 array($good1, $bad, $recursive, $good2),
467 $m = new xmlrpcmsg('system.multicall', array($arg));
468 $v = $this->send($m);
470 //$this->assertTrue($r->faultCode() == 0, "fault from system.multicall");
471 $this->assertTrue($v->arraysize() == 4, "bad number of return values");
473 $r1 = $v->arraymem(0);
475 $r1->kindOf() == 'array' && $r1->arraysize() == 1,
476 "did not get array of size 1 from good1"
479 $r2 = $v->arraymem(1);
481 $r2->kindOf() == 'struct',
485 $r3 = $v->arraymem(2);
487 $r3->kindOf() == 'struct',
488 "recursive system.multicall did not fail"
491 $r4 = $v->arraymem(3);
493 $r4->kindOf() == 'array' && $r4->arraysize() == 1,
494 "did not get array of size 1 from good2"
499 public function testClientMulticall1()
501 // NB: This test will NOT pass if server does not support system.multicall.
503 $noMultiCall = $this->client->no_multicall;
504 $this->client->no_multicall = false;
506 $good1 = new xmlrpcmsg('system.methodHelp',
507 array(php_xmlrpc_encode('system.listMethods')));
508 $bad = new xmlrpcmsg('test.nosuch',
509 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
510 $recursive = new xmlrpcmsg('system.multicall',
511 array(new xmlrpcval(array(), 'array')));
512 $good2 = new xmlrpcmsg('system.methodSignature',
513 array(php_xmlrpc_encode('system.listMethods'))
516 $r = $this->send(array($good1, $bad, $recursive, $good2));
518 $this->assertTrue(count($r) == 4, "wrong number of return values");
521 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
522 if (!$r[0]->faultCode()) {
523 $val = $r[0]->value();
525 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
526 "good1 did not return string"
529 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
530 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
531 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
532 if (!$r[3]->faultCode()) {
533 $val = $r[3]->value();
534 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
536 // This is the only assert in this test which should fail
537 // if the test server does not support system.multicall.
538 $this->assertTrue($this->client->no_multicall == false,
539 "server does not support system.multicall"
542 $this->client->no_multicall = $noMultiCall;
545 public function testClientMulticall2()
547 // NB: This test will NOT pass if server does not support system.multicall.
549 $noMultiCall = $this->client->no_multicall;
550 $this->client->no_multicall = true;
552 $good1 = new xmlrpcmsg('system.methodHelp',
553 array(php_xmlrpc_encode('system.listMethods')));
554 $bad = new xmlrpcmsg('test.nosuch',
555 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
556 $recursive = new xmlrpcmsg('system.multicall',
557 array(new xmlrpcval(array(), 'array')));
558 $good2 = new xmlrpcmsg('system.methodSignature',
559 array(php_xmlrpc_encode('system.listMethods'))
562 $r = $this->send(array($good1, $bad, $recursive, $good2));
564 $this->assertTrue(count($r) == 4, "wrong number of return values");
567 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
568 if (!$r[0]->faultCode()) {
569 $val = $r[0]->value();
571 $val->kindOf() == 'scalar' && $val->scalartyp() == 'string',
572 "good1 did not return string");
574 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
575 $this->assertTrue($r[2]->faultCode() == 0, "fault from (non recursive) system.multicall");
576 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
577 if (!$r[3]->faultCode()) {
578 $val = $r[3]->value();
579 $this->assertTrue($val->kindOf() == 'array', "good2 did not return array");
582 $this->client->no_multicall = $noMultiCall;
585 public function testClientMulticall3()
587 // NB: This test will NOT pass if server does not support system.multicall.
589 $noMultiCall = $this->client->no_multicall;
590 $returnType = $this->client->return_type;
592 $this->client->return_type = 'phpvals';
593 $this->client->no_multicall = false;
595 $good1 = new xmlrpcmsg('system.methodHelp',
596 array(php_xmlrpc_encode('system.listMethods')));
597 $bad = new xmlrpcmsg('test.nosuch',
598 array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
599 $recursive = new xmlrpcmsg('system.multicall',
600 array(new xmlrpcval(array(), 'array')));
601 $good2 = new xmlrpcmsg('system.methodSignature',
602 array(php_xmlrpc_encode('system.listMethods'))
605 $r = $this->send(array($good1, $bad, $recursive, $good2));
607 $this->assertTrue(count($r) == 4, "wrong number of return values");
609 $this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
610 if (!$r[0]->faultCode()) {
611 $val = $r[0]->value();
613 is_string($val), "good1 did not return string");
615 $this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
616 $this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
617 $this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
618 if (!$r[3]->faultCode()) {
619 $val = $r[3]->value();
620 $this->assertTrue(is_array($val), "good2 did not return array");
623 $this->client->return_type = $returnType;
624 $this->client->no_multicall = $noMultiCall;
627 public function testCatchWarnings()
629 $m = new xmlrpcmsg('tests.generatePHPWarning', array(
630 new xmlrpcval('whatever', 'string'),
632 $v = $this->send($m);
634 $this->assertEquals(true, $v->scalarval());
638 public function testCatchExceptions()
640 $m = new xmlrpcmsg('tests.raiseException', array(
641 new xmlrpcval('whatever', 'string'),
643 $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
644 $this->addQueryParams(array('EXCEPTION_HANDLING' => 1));
645 $v = $this->send($m, 1); // the error code of the expected exception
646 $this->addQueryParams(array('EXCEPTION_HANDLING' => 2));
647 // depending on whether display_errors is ON or OFF on the server, we will get back a different error here,
648 // as php will generate an http status code of either 200 or 500...
649 $v = $this->send($m, array($GLOBALS['xmlrpcerr']['invalid_return'], $GLOBALS['xmlrpcerr']['http_error']));
652 public function testZeroParams()
654 $m = new xmlrpcmsg('system.listMethods');
655 $v = $this->send($m);
658 public function testNullParams()
660 $m = new xmlrpcmsg('tests.getStateName.12', array(
661 new xmlrpcval('whatever', 'null'),
662 new xmlrpcval(23, 'int'),
664 $v = $this->send($m);
666 $this->assertEquals('Michigan', $v->scalarval());
668 $m = new xmlrpcmsg('tests.getStateName.12', array(
669 new xmlrpcval(23, 'int'),
670 new xmlrpcval('whatever', 'null'),
672 $v = $this->send($m);
674 $this->assertEquals('Michigan', $v->scalarval());
676 $m = new xmlrpcmsg('tests.getStateName.12', array(
677 new xmlrpcval(23, 'int')
679 $v = $this->send($m, array($GLOBALS['xmlrpcerr']['incorrect_params']));
682 public function testCodeInjectionServerSide()
684 $m = new xmlrpcmsg('system.MethodHelp');
685 $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>";
686 $v = $this->send($m);
688 $this->assertEquals(0, $v->structsize());
692 public function testServerWrappedFunction()
694 $m = new xmlrpcmsg('tests.getStateName.2', array(
695 new xmlrpcval(23, 'int'),
697 $v = $this->send($m);
698 $this->assertEquals('Michigan', $v->scalarval());
700 // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response
701 $m = new xmlrpcmsg('tests.getStateName.2', array(
702 new xmlrpcval(0, 'int'),
704 $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
706 // check if the generated function dispatch map is fine, by checking if the server registered it
707 $m = new xmlrpcmsg('system.methodSignature', array(
708 new xmlrpcval('tests.getStateName.2'),
710 $v = $this->send($m);
711 $encoder = new \PhpXmlRpc\Encoder();
712 $this->assertEquals(array(array('string', 'int')), $encoder->decode($v));
715 public function testServerWrappedFunctionAsSource()
717 $m = new xmlrpcmsg('tests.getStateName.6', array(
718 new xmlrpcval(23, 'int'),
720 $v = $this->send($m);
721 $this->assertEquals('Michigan', $v->scalarval());
723 // this generates an exception in the function which was wrapped, which is by default wrapped in a known error response
724 $m = new xmlrpcmsg('tests.getStateName.6', array(
725 new xmlrpcval(0, 'int'),
727 $v = $this->send($m, $GLOBALS['xmlrpcerr']['server_error']);
730 public function testServerWrappedObjectMethods()
732 $m = new xmlrpcmsg('tests.getStateName.3', array(
733 new xmlrpcval(23, 'int'),
735 $v = $this->send($m);
736 $this->assertEquals('Michigan', $v->scalarval());
738 $m = new xmlrpcmsg('tests.getStateName.4', array(
739 new xmlrpcval(23, 'int'),
741 $v = $this->send($m);
742 $this->assertEquals('Michigan', $v->scalarval());
744 $m = new xmlrpcmsg('tests.getStateName.5', array(
745 new xmlrpcval(23, 'int'),
747 $v = $this->send($m);
748 $this->assertEquals('Michigan', $v->scalarval());
750 $m = new xmlrpcmsg('tests.getStateName.7', array(
751 new xmlrpcval(23, 'int'),
753 $v = $this->send($m);
754 $this->assertEquals('Michigan', $v->scalarval());
756 $m = new xmlrpcmsg('tests.getStateName.8', array(
757 new xmlrpcval(23, 'int'),
759 $v = $this->send($m);
760 $this->assertEquals('Michigan', $v->scalarval());
762 $m = new xmlrpcmsg('tests.getStateName.9', array(
763 new xmlrpcval(23, 'int'),
765 $v = $this->send($m);
766 $this->assertEquals('Michigan', $v->scalarval());
769 public function testServerWrappedObjectMethodsAsSource()
771 $m = new xmlrpcmsg('tests.getStateName.7', array(
772 new xmlrpcval(23, 'int'),
774 $v = $this->send($m);
775 $this->assertEquals('Michigan', $v->scalarval());
777 $m = new xmlrpcmsg('tests.getStateName.8', array(
778 new xmlrpcval(23, 'int'),
780 $v = $this->send($m);
781 $this->assertEquals('Michigan', $v->scalarval());
783 $m = new xmlrpcmsg('tests.getStateName.9', array(
784 new xmlrpcval(23, 'int'),
786 $v = $this->send($m);
787 $this->assertEquals('Michigan', $v->scalarval());
790 public function testServerClosure()
792 $m = new xmlrpcmsg('tests.getStateName.10', array(
793 new xmlrpcval(23, 'int'),
795 $v = $this->send($m);
796 $this->assertEquals('Michigan', $v->scalarval());
799 public function testServerWrappedClosure()
801 $m = new xmlrpcmsg('tests.getStateName.11', array(
802 new xmlrpcval(23, 'int'),
804 $v = $this->send($m);
805 $this->assertEquals('Michigan', $v->scalarval());
808 public function testServerWrappedClass()
810 $m = new xmlrpcmsg('tests.xmlrpcServerMethodsContainer.findState', array(
811 new xmlrpcval(23, 'int'),
813 $v = $this->send($m);
814 $this->assertEquals('Michigan', $v->scalarval());
817 public function testWrappedMethod()
819 // make a 'deep client copy' as the original one might have many properties set
820 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0));
821 if ($func == false) {
822 $this->fail('Registration of examples.getStateName failed');
825 // work around bug in current (or old?) version of phpunit when reporting the error
826 /*if (is_object($v)) {
827 $v = var_export($v, true);
829 $this->assertEquals('Michigan', $v);
833 public function testWrappedMethodAsSource()
835 // make a 'deep client copy' as the original one might have many properties set
836 $func = wrap_xmlrpc_method($this->client, 'examples.getStateName', array('simple_client_copy' => 0, 'return_source' => true));
837 if ($func == false) {
838 $this->fail('Registration of examples.getStateName failed');
840 eval($func['source']);
841 $func = $func['function'];
843 // work around bug in current (or old?) version of phpunit when reporting the error
844 /*if (is_object($v)) {
845 $v = var_export($v, true);
847 $this->assertEquals('Michigan', $v);
851 public function testWrappedClass()
853 // make a 'deep client copy' as the original one might have many properties set
854 // also for speed only wrap one method of the whole server
855 $class = wrap_xmlrpc_server($this->client, array('simple_client_copy' => 0, 'method_filter' => '/examples\.getStateName/' ));
857 $this->fail('Registration of remote server failed');
860 if (!is_callable(array($obj, 'examples_getStateName'))) {
861 $this->fail('Registration of remote server failed to import method "examples_getStateName"');
863 $v = $obj->examples_getStateName(23);
864 // work around bug in current (or old?) version of phpunit when reporting the error
865 /*if (is_object($v)) {
866 $v = var_export($v, true);
868 $this->assertEquals('Michigan', $v);
873 public function testTransferOfObjectViaWrapping()
875 // make a 'deep client copy' as the original one might have many properties set
876 $func = wrap_xmlrpc_method($this->client, 'tests.returnPhpObject', array('simple_client_copy' => 0,
877 'decode_php_objs' => true));
878 if ($func == false) {
879 $this->fail('Registration of tests.returnPhpObject failed');
882 $obj = new stdClass();
883 $obj->hello = 'world';
884 $this->assertEquals($obj, $v);
888 public function testGetCookies()
890 // let server set to us some cookies we tell it
893 'c2' => array('value' => 'c2'),
894 'c3' => array('value' => 'c3', 'expires' => time() + 60 * 60 * 24 * 30),
895 'c4' => array('value' => 'c4', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/'),
896 'c5' => array('value' => 'c5', 'expires' => time() + 60 * 60 * 24 * 30, 'path' => '/', 'domain' => 'localhost'),
898 $cookiesval = php_xmlrpc_encode($cookies);
899 $m = new xmlrpcmsg('examples.setcookies', array($cookiesval));
900 $r = $this->send($m, 0, true);
903 $this->assertEquals(1, $v->scalarval());
904 // now check if we decoded the cookies as we had set them
905 $rcookies = $r->cookies();
906 // remove extra cookies which might have been set by proxies
907 foreach ($rcookies as $c => $v) {
908 if (!in_array($c, array('c2', 'c3', 'c4', 'c5'))) {
909 unset($rcookies[$c]);
911 // Seems like we get this when using php-fpm and php 5.5+ ...
912 if (isset($rcookies[$c]['Max-Age'])) {
913 unset($rcookies[$c]['Max-Age']);
916 foreach ($cookies as $c => $v) {
917 // format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
918 // but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
919 if (isset($v['expires'])) {
920 if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-')) {
921 $cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T', $cookies[$c]['expires']);
923 $cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T', $cookies[$c]['expires']);
928 $this->assertEquals($cookies, $rcookies);
932 public function testSetCookies()
934 // let server set to us some cookies we tell it
939 'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-',
941 $m = new xmlrpcmsg('examples.getcookies', array());
942 foreach ($cookies as $cookie => $val) {
943 $this->client->setCookie($cookie, $val);
944 $cookies[$cookie] = (string)$cookies[$cookie];
946 $r = $this->client->send($m, $this->timeout, $this->method);
947 $this->assertEquals(0, $r->faultCode(), 'Error ' . $r->faultCode() . ' connecting to server: ' . $r->faultString());
948 if (!$r->faultCode()) {
950 $v = php_xmlrpc_decode($v);
952 // take care for the extra cookie used for coverage collection
953 if (isset($v['PHPUNIT_SELENIUM_TEST_ID'])) {
954 unset($v['PHPUNIT_SELENIUM_TEST_ID']);
957 // on IIS and Apache getallheaders returns something slightly different...
958 $this->assertEquals($cookies, $v);
962 public function testServerComments()
964 $m = new xmlrpcmsg('tests.xmlrpcServerMethodsContainer.debugMessageGenerator', array(
965 new xmlrpcval('hello world', 'string'),
967 $r = $this->send($m, 0, true);
968 $this->assertContains('hello world', $r->raw_data);
971 public function testSendTwiceSameMsg()
973 $m = new xmlrpcmsg('examples.stringecho', array(
974 new xmlrpcval('hello world', 'string'),
976 $v1 = $this->send($m);
977 $v2 = $this->send($m);
979 $this->assertEquals($v1, $v2);