testsuite.php

来自「PHP 知识管理系统(基于树结构的知识管理系统), 英文原版的PHP源码。」· PHP 代码 · 共 800 行 · 第 1/2 页

PHP
800
字号
			$this->client->no_multicall = false;

			$good1 = new xmlrpcmsg('system.methodHelp',
				array(php_xmlrpc_encode('system.listMethods')));
			$bad = new xmlrpcmsg('test.nosuch',
				array(php_xmlrpc_encode(1), php_xmlrpc_encode(2)));
			$recursive = new xmlrpcmsg('system.multicall',
				array(new xmlrpcval(array(), 'array')));
			$good2 = new xmlrpcmsg('system.methodSignature',
				array(php_xmlrpc_encode('system.listMethods'))
			);

			$r = $this->send(array($good1, $bad, $recursive, $good2));
			if($r)
			{
				$this->assertTrue(count($r) == 4, "wrong number of return values");
			}
			$this->assertTrue($r[0]->faultCode() == 0, "fault from good1");
			if(!$r[0]->faultCode())
			{
				$val = $r[0]->value();
				$this->assertTrue(
					is_string($val) , "good1 did not return string");
			}
			$this->assertTrue($r[1]->faultCode() != 0, "no fault from bad");
			$this->assertTrue($r[2]->faultCode() != 0, "no fault from recursive system.multicall");
			$this->assertTrue($r[3]->faultCode() == 0, "fault from good2");
			if(!$r[3]->faultCode())
			{
				$val = $r[3]->value();
				$this->assertTrue(is_array($val), "good2 did not return array");
			}
			$this->client->return_type = 'xmlrpcvals';
		}

		function testCatchWarnings()
		{
			$f = new xmlrpcmsg('examples.generatePHPWarning', array(
				new xmlrpcval('whatever', 'string')
			));
			$v = $this->send($f);
			if($v)
			{
				$this->assertEquals($v->scalarval(), true);
			}
		}

		function testZeroParams()
		{
			$f = new xmlrpcmsg('system.listMethods');
			$v = $this->send($f);
		}

		function testCodeInjectionServerSide()
		{
			$f = new xmlrpcmsg('system.MethodHelp');
			$f->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>";
			$v = $this->send($f);
			//$v = $r->faultCode();
			if ($v)
			{
				$this->assertEquals(0, $v->structsize());
			}
		}

		function testAutoRegisteredFunction()
		{
			$f=new xmlrpcmsg('examples.php.getStateName',array(
				new xmlrpcval(23, 'int')
			));
			$v=$this->send($f);
			if($v)
			{
				$this->assertEquals('Michigan', $v->scalarval());
			}
			else
			{
				$this->fail('Note: server can only auto register functions if running with PHP 5.0.3 and up');
			}
		}

		function testAutoRegisteredMethod()
		{
			$func=wrap_xmlrpc_method($this->client, 'examples.getStateName');
			if($func == '')
			{
				$this->fail('Registration of examples.getStateName failed');
			}
			else
			{
				$v=$func(23);
				$this->assertEquals('Michigan', $v);
			}
		}

		function testGetCookies()
		{
			// let server set to us some cookies we tell it
			$cookies = array(
				//'c1' => array(),
				'c2' => array('value' => 'c2'),
				'c3' => array('value' => 'c3', 'expires' => time()+60*60*24*30),
				'c4' => array('value' => 'c4', 'expires' => time()+60*60*24*30, 'path' => '/'),
				'c5' => array('value' => 'c5', 'expires' => time()+60*60*24*30, 'path' => '/', 'domain' => 'localhost'),
			);
			$cookiesval =& php_xmlrpc_encode($cookies);
			$f=new xmlrpcmsg('examples.setcookies',array($cookiesval));
			$r=$this->send($f, 0, true);
			if($r)
			{
				$v = $r->value();
				$this->assertEquals(1, $v->scalarval());
				// now check if we decoded the cookies as we had set them
				$rcookies = $r->cookies();
				foreach($cookies as $c => $v)
				// format for date string in cookies: 'Mon, 31 Oct 2005 13:50:56 GMT'
				// but PHP versions differ on that, some use 'Mon, 31-Oct-2005 13:50:56 GMT'...
					if(isset($v['expires']))
					{
						if (isset($rcookies[$c]['expires']) && strpos($rcookies[$c]['expires'], '-'))
						{
							$cookies[$c]['expires'] = gmdate('D, d\-M\-Y H:i:s \G\M\T' ,$cookies[$c]['expires']);
						}
						else
						{
							$cookies[$c]['expires'] = gmdate('D, d M Y H:i:s \G\M\T' ,$cookies[$c]['expires']);
						}
					}
				$this->assertEquals($cookies, $rcookies);
			}
		}

		function testSetCookies()
		{
			// let server set to us some cookies we tell it
			$cookies = array(
				'c0' => null,
				'c1' => 1,
				'c2' => '2 3',
				'c3' => '!@#$%^&*()_+|}{":?><,./\';[]\\=-'
			);
			$f=new xmlrpcmsg('examples.getallheaders',array());
			foreach ($cookies as $cookie => $val)
			{
				$this->client->setCookie($cookie, $val);
				// build the same Cookie header returned by php in getallheaders
				$cookies[$cookie] = "$cookie=".urlencode($val);
			}
			$r = $this->client->send($f, $this->timeout, $this->method);
			$this->assertEquals($r->faultCode(), 0, 'Error '.$r->faultCode().' connecting to server: '.$r->faultString());
			if(!$r->faultCode())
			{
				$v = $r->value();
				$v = $v->structmem('Cookie');
				// on IIS and Apache getallheaders returns something slightly different...
				$this->assertEquals(implode(',', $cookies), str_replace(', ', ',', $v->scalarval()));
			}
		}

		function testSendTwiceSameMsg()
		{
			$f=new xmlrpcmsg('examples.stringecho', array(
				new xmlrpcval('hello world', 'string')
			));
			$v1 = $this->send($f);
			$v2 = $this->send($f);
			//$v = $r->faultCode();
			if ($v1 && $v2)
			{
				$this->assertEquals($v2, $v1);
			}
		}
	}

	class LocalHostMultiTests extends LocalhostTests
	{
		function _runtests()
		{
			global $failed_tests;
			foreach(get_class_methods('LocalhostTests') as $meth)
			{
				if(strpos($meth, 'test') === 0 && $meth != 'testHttps')
				{
					if (!isset($failed_tests[$meth]))
						$this->$meth();
				}
				if ($this->_failed)
				{
					break;
				}
			}
		}

		function testDeflate()
		{
			if(!function_exists('gzdeflate'))
			{
				$this->fail('Zlib missing: cannot test deflate functionality');
				return;
			}
			$this->client->accepted_compression = array('deflate');
			$this->client->request_compression = 'deflate';
			$this->_runtests();
		}

		function testGzip()
		{
			if(!function_exists('gzdeflate'))
			{
				$this->fail('Zlib missing: cannot test gzip functionality');
				return;
			}
			$this->client->accepted_compression = array('gzip');
			$this->client->request_compression = 'gzip';
			$this->_runtests();
		}

		function testKeepAlives()
		{
			if(!function_exists('curl_init'))
			{
				$this->fail('CURL missing: cannot test http 1.1');
				return;
			}
			$this->method = 'http11';
			$this->client->keepalive = true;
			$this->_runtests();
		}

		function testProxy()
		{
			global $PROXYSERVER, $PROXYPORT;
            if ($PROXYSERVER)
            {
				$this->client->setProxy($PROXYSERVER, $PROXYPORT);
				$this->_runtests();
            }
            else
            	$this->fail('PROXY definition missing: cannot test proxy');
		}

		function testHttp11()
		{
			if(!function_exists('curl_init'))
			{
				$this->fail('CURL missing: cannot test http 1.1');
				return;
			}
			$this->method = 'http11'; // not an error the double assignment!
			$this->client->method = 'http11';
			//$this->client->verifyhost = 0;
			//$this->client->verifypeer = 0;
			$this->client->keepalive = false;
			$this->_runtests();
		}

		function testHttp11Gzip()
		{
			if(!function_exists('curl_init'))
			{
				$this->fail('CURL missing: cannot test http 1.1');
				return;
			}
			$this->method = 'http11'; // not an error the double assignment!
			$this->client->method = 'http11';
			$this->client->keepalive = false;
			$this->client->accepted_compression = array('gzip');
			$this->client->request_compression = 'gzip';
			$this->_runtests();
		}

		function testHttp11Deflate()
		{
			if(!function_exists('curl_init'))
			{
				$this->fail('CURL missing: cannot test http 1.1');
				return;
			}
			$this->method = 'http11'; // not an error the double assignment!
			$this->client->method = 'http11';
			$this->client->keepalive = false;
			$this->client->accepted_compression = array('deflate');
			$this->client->request_compression = 'deflate';
			$this->_runtests();
		}

		function testHttp11Proxy()
		{
			global $PROXYSERVER, $PROXYPORT;
			if(!function_exists('curl_init'))
			{
				$this->fail('CURL missing: cannot test http 1.1 w. proxy');
				return;
			}
            else if ($PROXYSERVER == '')
            {
            	$this->fail('PROXY definition missing: cannot test proxy w. http 1.1');
                return;
            }
			$this->method = 'http11'; // not an error the double assignment!
			$this->client->method = 'http11';
            $this->client->setProxy($PROXYSERVER, $PROXYPORT);
			//$this->client->verifyhost = 0;
			//$this->client->verifypeer = 0;
			$this->client->keepalive = false;
			$this->_runtests();
		}

		function testHttps()
		{
			global $HTTPSSERVER, $HTTPSURI;
			if(!function_exists('curl_init'))
			{
				$this->fail('CURL missing: cannot test https functionality');
				return;
			}
			$this->client->server = $HTTPSSERVER;
			$this->method = 'https';
			$this->client->method = 'https';
			$this->client->path = $HTTPSURI;
			$this->_runtests();
		}

		function testHttpsProxy()
		{
			global $HTTPSSERVER, $HTTPSURI, $PROXYSERVER, $PROXYPORT;;
			if(!function_exists('curl_init'))
			{
				$this->fail('CURL missing: cannot test https functionality');
				return;
			}
            else if ($PROXYSERVER == '')
            {
            	$this->fail('PROXY definition missing: cannot test proxy w. http 1.1');
                return;
            }
			$this->client->server = $HTTPSSERVER;
			$this->method = 'https';
			$this->client->method = 'https';
            $this->client->setProxy($PROXYSERVER, $PROXYPORT);
			$this->client->path = $HTTPSURI;
			$this->_runtests();
		}

		function testUTF8Responses()
		{
			global $URI;
			//$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
			$this->client->path = $URI.'?RESPONSE_ENCODING=UTF-8';
			$this->_runtests();
		}

		function testUTF8Requests()
		{
			$this->client->request_charset_encoding = 'UTF-8';
			$this->_runtests();
		}

		function testISOResponses()
		{
			global $URI;
			//$this->client->path = strpos($URI, '?') === null ? $URI.'?RESPONSE_ENCODING=UTF-8' : $URI.'&RESPONSE_ENCODING=UTF-8';
			$this->client->path = $URI.'?RESPONSE_ENCODING=ISO-8859-1';
			$this->_runtests();
		}

		function testISORequests()
		{
			$this->client->request_charset_encoding = 'ISO-8859-1';
			$this->_runtests();
		}
	}

	class ParsingBugsTests extends PHPUnit_TestCase
	{
		function testMinusOneString()
		{
			$v=new xmlrpcval('-1');
			$u=new xmlrpcval('-1', 'string');
			$this->assertEquals($u->scalarval(), $v->scalarval());
		}

		function testUnicodeInMemberName(){
			$v = array('G黱ter, El鑞e' => new xmlrpcval(1));
			$r = new xmlrpcresp(new xmlrpcval($v, 'struct'));
			$r = $r->serialize();
			$m = new xmlrpcmsg('dummy');
			$r = $m->parseResponse($r);
			$v = $r->value();
			$this->assertEquals($v->structmemexists('G黱ter, El鑞e'), true);
		}

		function testUnicodeInErrorString()
		{
			$response = utf8_encode(
'<?xml version="1.0"?>
<!-- $Id -->
<!-- found by G. giunta, covers what happens when lib receives
  UTF8 chars in reponse text and comments -->
<!-- 帱

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?