ServerTest.php 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. <?php
  2. // Call Zend_Amf_ServerTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Amf_ServerTest::main");
  5. }
  6. /**
  7. * Test helper
  8. */
  9. require_once dirname(__FILE__) . '/../../TestHelper.php';
  10. require_once 'Zend/Amf/Server.php';
  11. require_once 'Zend/Amf/Request.php';
  12. require_once 'Zend/Amf/Parse/TypeLoader.php';
  13. require_once 'Zend/Amf/Value/Messaging/RemotingMessage.php';
  14. require_once 'ServiceA.php';
  15. require_once 'ServiceB.php';
  16. require_once 'Zend/Session.php';
  17. class Zend_Amf_ServerTest extends PHPUnit_Framework_TestCase
  18. {
  19. protected $_server;
  20. public static function main()
  21. {
  22. $suite = new PHPUnit_Framework_TestSuite("Zend_Amf_ServerTest");
  23. $result = PHPUnit_TextUI_TestRunner::run($suite);
  24. }
  25. public function setUp()
  26. {
  27. $this->_server = new Zend_Amf_Server();
  28. $this->_server->setProduction(false);
  29. Zend_Amf_Parse_TypeLoader::resetMap();
  30. }
  31. public function testDown()
  32. {
  33. unset($this->_server);
  34. //Zend_Amf_Parse_TypeLoader::resetMap();
  35. }
  36. /**
  37. * Call as method call
  38. *
  39. * Returns: void
  40. */
  41. public function test__construct()
  42. {
  43. $this->assertTrue($this->_server instanceof Zend_Amf_Server);
  44. }
  45. public function testIsProductionByDefault()
  46. {
  47. $this->_server = new Zend_Amf_Server;
  48. $this->assertTrue($this->_server->isProduction());
  49. }
  50. public function testProductionFlagShouldBeMutable()
  51. {
  52. $this->testIsProductionByDefault();
  53. $this->_server->setProduction(false);
  54. $this->assertFalse($this->_server->isProduction());
  55. $this->_server->setProduction(true);
  56. $this->assertTrue($this->_server->isProduction());
  57. }
  58. public function testSetClass()
  59. {
  60. $this->_server->setClass('Zend_Amf_testclass', 'test');
  61. $methods = $this->_server->listMethods();
  62. $this->assertTrue(in_array('test.test1', $methods));
  63. $this->assertTrue(in_array('test.test2', $methods));
  64. $this->assertFalse(in_array('test._test3', $methods));
  65. $this->assertFalse(in_array('test.__construct', $methods));
  66. }
  67. /**
  68. * @expectedException Zend_Amf_Server_Exception
  69. */
  70. public function testSetClassShouldRaiseExceptionOnInvalidClassname()
  71. {
  72. $this->_server->setClass('foobar');
  73. }
  74. /**
  75. * @expectedException Zend_Amf_Server_Exception
  76. */
  77. public function testSetClassShouldRaiseExceptionOnInvalidClasstype()
  78. {
  79. $this->_server->setClass(array('foobar'));
  80. }
  81. /**
  82. * @expectedException Zend_Amf_Server_Exception
  83. */
  84. public function testSetClassShouldRaiseExceptionOnDuplicateMethodName()
  85. {
  86. $this->_server->setClass('Zend_Amf_testclass', 'tc');
  87. $this->_server->setClass('Zend_Amf_testclassPrivate', 'tc');
  88. }
  89. /**
  90. * ZF-5393
  91. */
  92. public function testSetClassUsingObject()
  93. {
  94. $testClass = new Zend_Amf_testclass();
  95. $this->_server->setClass($testClass);
  96. $this->assertEquals(8, count($this->_server->getFunctions()));
  97. }
  98. /**
  99. * addFunction() test
  100. *
  101. * Call as method call
  102. *
  103. * Expects:
  104. * - function:
  105. * - namespace: Optional; has default;
  106. *
  107. * Returns: void
  108. */
  109. public function testAddFunction()
  110. {
  111. try {
  112. $this->_server->addFunction('Zend_Amf_Server_testFunction', 'test');
  113. } catch (Exception $e) {
  114. $this->fail('Attachment should have worked');
  115. }
  116. $methods = $this->_server->listMethods();
  117. $this->assertTrue(in_array('test.Zend_Amf_Server_testFunction', $methods), var_export($methods, 1));
  118. try {
  119. $this->_server->addFunction('nosuchfunction');
  120. $this->fail('nosuchfunction() should not exist and should throw an exception');
  121. } catch (Exception $e) {
  122. // do nothing
  123. }
  124. $server = new Zend_Amf_Server();
  125. try {
  126. $server->addFunction(
  127. array(
  128. 'Zend_Amf_Server_testFunction',
  129. 'Zend_Amf_Server_testFunction2',
  130. ),
  131. 'zsr'
  132. );
  133. } catch (Exception $e) {
  134. $this->fail('Error attaching array of functions: ' . $e->getMessage());
  135. }
  136. $methods = $server->listMethods();
  137. $this->assertTrue(in_array('zsr.Zend_Amf_Server_testFunction', $methods));
  138. $this->assertTrue(in_array('zsr.Zend_Amf_Server_testFunction2', $methods));
  139. }
  140. /**
  141. * @expectedException Zend_Amf_Server_Exception
  142. */
  143. public function testAddFunctionShouldRaiseExceptionForInvalidFunctionName()
  144. {
  145. $this->_server->addFunction(true);
  146. }
  147. /**
  148. * @expectedException Zend_Amf_Server_Exception
  149. */
  150. public function testAddFunctionShouldRaiseExceptionOnDuplicateMethodName()
  151. {
  152. $this->_server->addFunction('Zend_Amf_Server_testFunction', 'tc');
  153. $this->_server->addFunction('Zend_Amf_Server_testFunction', 'tc');
  154. }
  155. /**
  156. * Test sending data to the remote class and make sure we
  157. * recieve the proper response.
  158. *
  159. */
  160. public function testHandleLoadedClassAmf0()
  161. {
  162. // serialize the data to an AMF output stream
  163. $data[] = "12345";
  164. $this->_server->setClass('Zend_Amf_testclass');
  165. $newBody = new Zend_Amf_Value_MessageBody("Zend_Amf_testclass.test1","/1",$data);
  166. $request = new Zend_Amf_Request();
  167. $request->addAmfBody($newBody);
  168. $request->setObjectEncoding(0x00);
  169. $result = $this->_server->handle($request);
  170. $response = $this->_server->getResponse();
  171. $responseBody = $response->getAmfBodies();
  172. // Now check if the return data was properly set.
  173. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  174. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  175. $this->assertEquals("String: 12345", $responseBody[0]->getData(), var_export($responseBody, 1));
  176. }
  177. public function testShouldAllowHandlingFunctionCallsViaAmf0()
  178. {
  179. // serialize the data to an AMF output stream
  180. $data = array('foo', 'bar');
  181. $this->_server->addFunction('Zend_Amf_Server_testFunction');
  182. $newBody = new Zend_Amf_Value_MessageBody("Zend_Amf_Server_testFunction","/1",$data);
  183. $request = new Zend_Amf_Request();
  184. $request->addAmfBody($newBody);
  185. $request->setObjectEncoding(0x00);
  186. $result = $this->_server->handle($request);
  187. $response = $this->_server->getResponse();
  188. $responseBody = $response->getAmfBodies();
  189. // Now check if the return data was properly set.
  190. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  191. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  192. $this->assertEquals("bar: foo", $responseBody[0]->getData(), var_export($responseBody, 1));
  193. }
  194. /**
  195. * Test to make sure that AMF3 basic requests are handled for loading
  196. * a class.
  197. * This type of call is sent from NetConnection rather than RemoteObject
  198. *
  199. * @group ZF-4680
  200. */
  201. public function testHandleLoadedClassAmf3NetConnection()
  202. {
  203. // serialize the data to an AMF output stream
  204. $data[] = "12345";
  205. $this->_server->setClass('Zend_Amf_testclass');
  206. $newBody = new Zend_Amf_Value_MessageBody("Zend_Amf_testclass.test1","/1",$data);
  207. $request = new Zend_Amf_Request();
  208. $request->addAmfBody($newBody);
  209. $request->setObjectEncoding(0x03);
  210. $result = $this->_server->handle($request);
  211. $response = $this->_server->getResponse();
  212. $responseBody = $response->getAmfBodies();
  213. // Now check if the return data was properly set.
  214. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  215. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  216. $this->assertEquals("String: 12345", $responseBody[0]->getData(), var_export($responseBody, 1));
  217. }
  218. /**
  219. * Test to make sure that AMF3 basic requests are handled for function calls.
  220. * This type of call is sent from net connection rather than RemoteObject
  221. *
  222. * @group ZF-4680
  223. */
  224. public function testShouldAllowHandlingFunctionCallsViaAmf3NetConnection()
  225. {
  226. // serialize the data to an AMF output stream
  227. $data = array('foo', 'bar');
  228. $this->_server->addFunction('Zend_Amf_Server_testFunction');
  229. $newBody = new Zend_Amf_Value_MessageBody("Zend_Amf_Server_testFunction","/1",$data);
  230. $request = new Zend_Amf_Request();
  231. $request->addAmfBody($newBody);
  232. $request->setObjectEncoding(0x03);
  233. $result = $this->_server->handle($request);
  234. $response = $this->_server->getResponse();
  235. $responseBody = $response->getAmfBodies();
  236. // Now check if the return data was properly set.
  237. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  238. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  239. $this->assertEquals("bar: foo", $responseBody[0]->getData(), var_export($responseBody, 1));
  240. }
  241. /**
  242. * Test sending data to the remote class and make sure we
  243. * recieve the proper response.
  244. *
  245. */
  246. public function testHandleLoadedClassAmf3()
  247. {
  248. // serialize the data to an AMF output stream
  249. $data[] = "12345";
  250. $this->_server->setClass('Zend_Amf_testclass');
  251. // create a mock remoting message
  252. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  253. $message->operation = 'test1';
  254. $message->source = 'Zend_Amf_testclass';
  255. $message->body = $data;
  256. // create a mock message body to place th remoting message inside
  257. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  258. $request = new Zend_Amf_Request();
  259. // at the requested service to a request
  260. $request->addAmfBody($newBody);
  261. $request->setObjectEncoding(0x03);
  262. // let the server handle mock request
  263. $result = $this->_server->handle($request);
  264. $response = $this->_server->getResponse();
  265. $responseBody = $response->getAmfBodies();
  266. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  267. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  268. // Now check if the return data was properly set.
  269. $acknowledgeMessage = $responseBody[0]->getData();
  270. // check that we have a message beening returned
  271. $this->assertTrue($acknowledgeMessage instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  272. // Check the message body is the expected data to be returned
  273. $this->assertEquals("String: 12345", $acknowledgeMessage->body);
  274. }
  275. /**
  276. * Test to make sure that you can have the same method name in two different classes.
  277. *
  278. * @group ZF-5040
  279. */
  280. public function testSameMethodNameInTwoServices()
  281. {
  282. $this->_server->setClass('ServiceA');
  283. $this->_server->setClass('ServiceB');
  284. // create a mock remoting message
  285. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  286. $message->operation = 'getMenu';
  287. $message->source = 'ServiceB';
  288. $message->body = array();
  289. // create a mock message body to place th remoting message inside
  290. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  291. $request = new Zend_Amf_Request();
  292. // at the requested service to a request
  293. $request->addAmfBody($newBody);
  294. $request->setObjectEncoding(0x03);
  295. // let the server handle mock request
  296. $result = $this->_server->handle($request);
  297. $response = $this->_server->getResponse();
  298. $responseBody = $response->getAmfBodies();
  299. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  300. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  301. // Now check if the return data was properly set.
  302. $acknowledgeMessage = $responseBody[0]->getData();
  303. // check that we have a message beening returned
  304. $this->assertTrue($acknowledgeMessage instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  305. // Check the message body is the expected data to be returned
  306. $this->assertEquals("myMenuB", $acknowledgeMessage->body);
  307. }
  308. /**
  309. * test command message. THis is the first call the Flex
  310. * makes before any subsequent service calls.
  311. */
  312. public function testCommandMessagePingOperation()
  313. {
  314. $message = new Zend_Amf_Value_Messaging_CommandMessage();
  315. $message->operation = 5;
  316. $message->messageId = $message->generateId();
  317. // create a mock message body to place th remoting message inside
  318. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  319. $request = new Zend_Amf_Request();
  320. // at the requested service to a request
  321. $request->addAmfBody($newBody);
  322. $request->setObjectEncoding(0x03);
  323. // let the server handle mock request
  324. $result = $this->_server->handle($request);
  325. $response = $this->_server->getResponse();
  326. $responseBody = $response->getAmfBodies();
  327. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  328. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  329. // Now check if the return data was properly set.
  330. $acknowledgeMessage = $responseBody[0]->getData();
  331. // check that we have a message beening returned
  332. $this->assertTrue($acknowledgeMessage instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  333. // Check that the MessageID was not corrupeted when set to the correlationId
  334. $this->assertEquals($acknowledgeMessage->correlationId, $message->messageId);
  335. }
  336. public function testInvalidAmf0MessageShouldResultInErrorMessage()
  337. {
  338. // serialize the data to an AMF output stream
  339. $data[] = "12345";
  340. $this->_server->setClass('Zend_Amf_testclass');
  341. $newBody = new Zend_Amf_Value_MessageBody("bogus","/1",$data);
  342. $request = new Zend_Amf_Request();
  343. $request->addAmfBody($newBody);
  344. $request->setObjectEncoding(0x00);
  345. $result = $this->_server->handle($request);
  346. $bodies = $result->getAmfBodies();
  347. $found = false;
  348. foreach ($bodies as $body) {
  349. $data = $body->getData();
  350. if (!is_array($data)) {
  351. continue;
  352. }
  353. if (!array_key_exists('description', $data)) {
  354. continue;
  355. }
  356. if (strstr($data['description'], 'does not exist')) {
  357. $found = true;
  358. break;
  359. }
  360. }
  361. $this->assertTrue($found, 'Invalid method did not raise error condition' . var_export($bodies, 1));
  362. }
  363. public function testInvalidCommandMessageShouldResultInErrorMessage()
  364. {
  365. $message = new Zend_Amf_Value_Messaging_CommandMessage();
  366. $message->operation = 'pong';
  367. $message->messageId = $message->generateId();
  368. // create a mock message body to place th remoting message inside
  369. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  370. $request = new Zend_Amf_Request();
  371. // at the requested service to a request
  372. $request->addAmfBody($newBody);
  373. $request->setObjectEncoding(0x03);
  374. // let the server handle mock request
  375. $result = $this->_server->handle($request);
  376. $response = $this->_server->getResponse();
  377. $responseBody = $response->getAmfBodies();
  378. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  379. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  380. // Now check if the return data was properly set.
  381. $message = $responseBody[0]->getData();
  382. // check that we have a message beening returned
  383. $this->assertTrue($message instanceof Zend_Amf_Value_Messaging_ErrorMessage);
  384. }
  385. /**
  386. * Add a class mapping and lookup the mapping to make sure
  387. * the mapping succeeds
  388. */
  389. public function testClassMap()
  390. {
  391. $this->_server->setClassMap('controller.test', 'Zend_Amf_testclass');
  392. $className = Zend_Amf_Parse_TypeLoader::getMappedClassName('Zend_Amf_testclass');
  393. $this->assertEquals('controller.test', $className);
  394. }
  395. public function testDispatchingMethodShouldReturnErrorMessageForInvalidMethod()
  396. {
  397. // serialize the data to an AMF output stream
  398. $data[] = "12345";
  399. $this->_server->setClass('Zend_Amf_testclass');
  400. // create a mock remoting message
  401. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  402. $message->operation = 'bogus'; // INVALID method!
  403. $message->body = $data;
  404. // create a mock message body to place th remoting message inside
  405. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  406. $request = new Zend_Amf_Request();
  407. // at the requested service to a request
  408. $request->addAmfBody($newBody);
  409. $request->setObjectEncoding(0x03);
  410. // let the server handle mock request
  411. $result = $this->_server->handle($request);
  412. $bodies = $result->getAmfBodies();
  413. $found = false;
  414. foreach ($bodies as $body) {
  415. $data = $body->getData();
  416. if ($data instanceof Zend_Amf_Value_Messaging_ErrorMessage) {
  417. if (strstr($data->faultString, 'does not exist')) {
  418. $found = true;
  419. break;
  420. }
  421. }
  422. }
  423. $this->assertTrue($found, 'Invalid method did not raise error condition: ' . var_export($bodies, 1));
  424. }
  425. public function testDispatchingMethodThatThrowsExceptionShouldReturnErrorMessageWhenProductionFlagOff()
  426. {
  427. // serialize the data to an AMF output stream
  428. $data = array();
  429. $this->_server->setClass('Zend_Amf_testclass');
  430. // create a mock remoting message
  431. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  432. $message->operation = 'throwException';
  433. $message->source = 'Zend_Amf_testclass';
  434. $message->body = $data;
  435. // create a mock message body to place th remoting message inside
  436. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  437. $request = new Zend_Amf_Request();
  438. // at the requested service to a request
  439. $request->addAmfBody($newBody);
  440. $request->setObjectEncoding(0x03);
  441. // let the server handle mock request
  442. $result = $this->_server->handle($request);
  443. $bodies = $result->getAmfBodies();
  444. $found = false;
  445. foreach ($bodies as $body) {
  446. $data = $body->getData();
  447. if ($data instanceof Zend_Amf_Value_Messaging_ErrorMessage) {
  448. if (strstr($data->faultString, 'should not be displayed')) {
  449. $found = true;
  450. break;
  451. }
  452. }
  453. }
  454. $this->assertTrue($found, 'Method raising exception should display error message when not in production');
  455. }
  456. public function testDispatchingMethodThatThrowsExceptionShouldNotReturnErrorMessageWhenProductionFlagOn()
  457. {
  458. // serialize the data to an AMF output stream
  459. $data = array();
  460. $this->_server->setClass('Zend_Amf_testclass')
  461. ->setProduction(true);
  462. // create a mock remoting message
  463. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  464. $message->operation = 'throwException';
  465. $message->source = 'Zend_Amf_testclass';
  466. $message->body = $data;
  467. // create a mock message body to place th remoting message inside
  468. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  469. $request = new Zend_Amf_Request();
  470. // at the requested service to a request
  471. $request->addAmfBody($newBody);
  472. $request->setObjectEncoding(0x03);
  473. // let the server handle mock request
  474. $result = $this->_server->handle($request);
  475. $bodies = $result->getAmfBodies();
  476. $found = false;
  477. foreach ($bodies as $body) {
  478. $data = $body->getData();
  479. if ($data instanceof Zend_Amf_Value_Messaging_ErrorMessage) {
  480. if (strstr($data->faultString, 'should not be displayed')) {
  481. $found = true;
  482. break;
  483. }
  484. }
  485. }
  486. $this->assertFalse($found, 'Method raising exception should not display error message when in production');
  487. }
  488. public function testDispatchingMethodShouldPassInvocationArgumentsToMethod()
  489. {
  490. // serialize the data to an AMF output stream
  491. $data[] = "baz";
  492. $this->_server->setClass('Zend_Amf_testclass', '', 'foo', 'bar');
  493. // create a mock remoting message
  494. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  495. $message->operation = 'checkArgv';
  496. $message->source = 'Zend_Amf_testclass';
  497. $message->body = $data;
  498. // create a mock message body to place th remoting message inside
  499. $newBody = new Zend_Amf_Value_MessageBody(null, "/1" ,$message);
  500. $request = new Zend_Amf_Request();
  501. // at the requested service to a request
  502. $request->addAmfBody($newBody);
  503. $request->setObjectEncoding(0x03);
  504. // let the server handle mock request
  505. $result = $this->_server->handle($request);
  506. $bodies = $result->getAmfBodies();
  507. $found = false;
  508. foreach ($bodies as $body) {
  509. $data = $body->getData();
  510. if ('Zend_Amf_Value_Messaging_AcknowledgeMessage' == get_class($data)) {
  511. if ('baz:foo:bar' == $data->body) {
  512. $found = true;
  513. break;
  514. }
  515. }
  516. }
  517. $this->assertTrue($found, 'Valid response not found');
  518. }
  519. public function testServerShouldSeamlesslyInvokeStaticMethods()
  520. {
  521. // serialize the data to an AMF output stream
  522. $data[] = "testing";
  523. $this->_server->setClass('Zend_Amf_testclass');
  524. // create a mock remoting message
  525. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  526. $message->operation = 'checkStaticUsage';
  527. $message->source = 'Zend_Amf_testclass';
  528. $message->body = $data;
  529. // create a mock message body to place th remoting message inside
  530. $newBody = new Zend_Amf_Value_MessageBody(null, "/1" ,$message);
  531. $request = new Zend_Amf_Request();
  532. // at the requested service to a request
  533. $request->addAmfBody($newBody);
  534. $request->setObjectEncoding(0x03);
  535. // let the server handle mock request
  536. $result = $this->_server->handle($request);
  537. $bodies = $result->getAmfBodies();
  538. $found = false;
  539. foreach ($bodies as $body) {
  540. $data = $body->getData();
  541. if ('Zend_Amf_Value_Messaging_AcknowledgeMessage' == get_class($data)) {
  542. if ('testing' == $data->body) {
  543. $found = true;
  544. break;
  545. }
  546. }
  547. }
  548. $this->assertTrue($found, 'Valid response not found');
  549. }
  550. public function testServerShouldSeamlesslyInvokeFunctions()
  551. {
  552. // serialize the data to an AMF output stream
  553. $data[] = 'foo';
  554. $data[] = 'bar';
  555. $this->_server->addFunction('Zend_Amf_Server_testFunction');
  556. // create a mock remoting message
  557. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  558. $message->operation = 'Zend_Amf_Server_testFunction';
  559. $message->source = null;
  560. $message->body = $data;
  561. // create a mock message body to place th remoting message inside
  562. $newBody = new Zend_Amf_Value_MessageBody(null, "/1" ,$message);
  563. $request = new Zend_Amf_Request();
  564. // at the requested service to a request
  565. $request->addAmfBody($newBody);
  566. $request->setObjectEncoding(0x03);
  567. // let the server handle mock request
  568. $result = $this->_server->handle($request);
  569. $bodies = $result->getAmfBodies();
  570. $found = false;
  571. foreach ($bodies as $body) {
  572. $data = $body->getData();
  573. if ('Zend_Amf_Value_Messaging_AcknowledgeMessage' == get_class($data)) {
  574. if ('bar: foo' == $data->body) {
  575. $found = true;
  576. break;
  577. }
  578. }
  579. }
  580. $this->assertTrue($found, 'Valid response not found');
  581. }
  582. public function testDispatchingMethodCorrespondingToClassWithPrivateConstructorShouldReturnErrorMessage()
  583. {
  584. // serialize the data to an AMF output stream
  585. $data[] = "baz";
  586. $this->_server->setClass('Zend_Amf_testclassPrivate');
  587. // create a mock remoting message
  588. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  589. $message->operation = 'test1';
  590. $message->source = 'Zend_Amf_testclassPrivate';
  591. $message->body = $data;
  592. // create a mock message body to place th remoting message inside
  593. $newBody = new Zend_Amf_Value_MessageBody(null, "/1" ,$message);
  594. $request = new Zend_Amf_Request();
  595. // at the requested service to a request
  596. $request->addAmfBody($newBody);
  597. $request->setObjectEncoding(0x03);
  598. // let the server handle mock request
  599. $result = $this->_server->handle($request);
  600. $bodies = $result->getAmfBodies();
  601. $found = false;
  602. foreach ($bodies as $body) {
  603. $data = $body->getData();
  604. if ('Zend_Amf_Value_Messaging_ErrorMessage' == get_class($data)) {
  605. if (strstr($data->faultString, 'Error instantiating class')) {
  606. $found = true;
  607. break;
  608. }
  609. }
  610. }
  611. $this->assertTrue($found, 'Method succeeded?');
  612. }
  613. public function testNotPassingRequestToHandleShouldResultInServerCreatingRequest()
  614. {
  615. $this->_server->setClass('Zend_Amf_testclass');
  616. ob_start();
  617. $result = $this->_server->handle();
  618. $content = ob_get_clean();
  619. $request = $this->_server->getRequest();
  620. $this->assertTrue($request instanceof Zend_Amf_Request_Http);
  621. $bodies = $request->getAmfBodies();
  622. $this->assertEquals(0, count($bodies));
  623. $this->assertContains('Endpoint', $content);
  624. }
  625. public function testSetRequestShouldAllowValidStringClassNames()
  626. {
  627. $this->_server->setRequest('Zend_Amf_Request');
  628. $request = $this->_server->getRequest();
  629. $this->assertTrue($request instanceof Zend_Amf_Request);
  630. $this->assertFalse($request instanceof Zend_Amf_Request_Http);
  631. }
  632. /**
  633. * @expectedException Zend_Amf_Server_Exception
  634. */
  635. public function testSetRequestShouldRaiseExceptionOnInvalidStringClassName()
  636. {
  637. $this->_server->setRequest('Zend_Amf_ServerTest_BogusRequest');
  638. }
  639. public function testSetRequestShouldAllowValidRequestObjects()
  640. {
  641. $request = new Zend_Amf_Request;
  642. $this->_server->setRequest($request);
  643. $this->assertSame($request, $this->_server->getRequest());
  644. }
  645. /**
  646. * @expectedException Zend_Amf_Server_Exception
  647. */
  648. public function testSetRequestShouldRaiseExceptionOnInvalidRequestObjects()
  649. {
  650. require_once 'Zend/XmlRpc/Request.php';
  651. $request = new Zend_XmlRpc_Request;
  652. $this->_server->setRequest($request);
  653. }
  654. public function testSetResponseShouldAllowValidStringClassNames()
  655. {
  656. $this->_server->setResponse('Zend_Amf_Response');
  657. $response = $this->_server->getResponse();
  658. $this->assertTrue($response instanceof Zend_Amf_Response);
  659. $this->assertFalse($response instanceof Zend_Amf_Response_Http);
  660. }
  661. /**
  662. * @expectedException Zend_Amf_Server_Exception
  663. */
  664. public function testSetResponseShouldRaiseExceptionOnInvalidStringClassName()
  665. {
  666. $this->_server->setResponse('Zend_Amf_ServerTest_BogusResponse');
  667. }
  668. public function testSetResponseShouldAllowValidResponseObjects()
  669. {
  670. $response = new Zend_Amf_Response;
  671. $this->_server->setResponse($response);
  672. $this->assertSame($response, $this->_server->getResponse());
  673. }
  674. /**
  675. * @expectedException Zend_Amf_Server_Exception
  676. */
  677. public function testSetResponseShouldRaiseExceptionOnInvalidResponseObjects()
  678. {
  679. require_once 'Zend/XmlRpc/Response.php';
  680. $response = new Zend_XmlRpc_Response;
  681. $this->_server->setResponse($response);
  682. }
  683. public function testGetFunctionsShouldReturnArrayOfDispatchables()
  684. {
  685. $this->_server->addFunction('Zend_Amf_Server_testFunction', 'tf')
  686. ->setClass('Zend_Amf_testclass', 'tc')
  687. ->setClass('Zend_Amf_testclassPrivate', 'tcp');
  688. $functions = $this->_server->getFunctions();
  689. $this->assertTrue(is_array($functions));
  690. $this->assertTrue(0 < count($functions));
  691. $namespaces = array('tf', 'tc', 'tcp');
  692. foreach ($functions as $key => $value) {
  693. $this->assertTrue(strstr($key, '.') ? true : false, $key);
  694. $ns = substr($key, 0, strpos($key, '.'));
  695. $this->assertContains($ns, $namespaces, $key);
  696. $this->assertTrue($value instanceof Zend_Server_Reflection_Function_Abstract);
  697. }
  698. }
  699. public function testFaultShouldBeUnimplemented()
  700. {
  701. $this->assertNull($this->_server->fault());
  702. }
  703. public function testPersistenceShouldBeUnimplemented()
  704. {
  705. $this->assertNull($this->_server->setPersistence(true));
  706. }
  707. public function testLoadFunctionsShouldBeUnimplemented()
  708. {
  709. $this->assertNull($this->_server->loadFunctions(true));
  710. }
  711. /**
  712. * @group ZF-5388
  713. * Issue if only one parameter of type array is passed it is nested into another array.
  714. */
  715. public function testSingleArrayParamaterAMF3()
  716. {
  717. // serialize the data to an AMF output stream
  718. $data[] = array('item1', 'item2');
  719. $this->_server->setClass('Zend_Amf_testclass');
  720. // create a mock remoting message
  721. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  722. $message->operation = 'testSingleArrayParamater';
  723. $message->source = 'Zend_Amf_testclass';
  724. $message->body = $data;
  725. // create a mock message body to place th remoting message inside
  726. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  727. $request = new Zend_Amf_Request();
  728. // at the requested service to a request
  729. $request->addAmfBody($newBody);
  730. $request->setObjectEncoding(0x03);
  731. // let the server handle mock request
  732. $result = $this->_server->handle($request);
  733. $response = $this->_server->getResponse();
  734. $responseBody = $response->getAmfBodies();
  735. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  736. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  737. // Now check if the return data was properly set.
  738. $acknowledgeMessage = $responseBody[0]->getData();
  739. // check that we have a message beening returned
  740. $this->assertTrue($acknowledgeMessage instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  741. // Check the message body is the expected data to be returned
  742. $this->assertTrue($acknowledgeMessage->body);
  743. }
  744. /**
  745. * @group ZF-5388
  746. * Issue if only one parameter of type array is passed it is nested into another array.
  747. */
  748. public function testSingleArrayParamaterAMF0()
  749. {
  750. $data[] = array('item1', 'item2');
  751. $this->_server->setClass('Zend_Amf_testclass');
  752. $newBody = new Zend_Amf_Value_MessageBody("Zend_Amf_testclass.testSingleArrayParamater","/1",$data);
  753. $request = new Zend_Amf_Request();
  754. $request->addAmfBody($newBody);
  755. $request->setObjectEncoding(0x00);
  756. $result = $this->_server->handle($request);
  757. $response = $this->_server->getResponse();
  758. $responseBody = $response->getAmfBodies();
  759. // Now check if the return data was properly set.
  760. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  761. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  762. $this->assertTrue($responseBody[0]->getData(), var_export($responseBody, 1));
  763. }
  764. /**
  765. * @group ZF-5388
  766. * Issue if only one parameter of type array is passed it is nested into another array.
  767. */
  768. public function testMutiArrayParamaterAMF3()
  769. {
  770. // serialize the data to an AMF output stream
  771. $data[] = array('item1', 'item2');
  772. $data[] = array('item3', 'item4');
  773. $this->_server->setClass('Zend_Amf_testclass');
  774. // create a mock remoting message
  775. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  776. $message->operation = 'testMultiArrayParamater';
  777. $message->source = 'Zend_Amf_testclass';
  778. $message->body = $data;
  779. // create a mock message body to place th remoting message inside
  780. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  781. $request = new Zend_Amf_Request();
  782. // at the requested service to a request
  783. $request->addAmfBody($newBody);
  784. $request->setObjectEncoding(0x03);
  785. // let the server handle mock request
  786. $result = $this->_server->handle($request);
  787. $response = $this->_server->getResponse();
  788. $responseBody = $response->getAmfBodies();
  789. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  790. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  791. // Now check if the return data was properly set.
  792. $acknowledgeMessage = $responseBody[0]->getData();
  793. // check that we have a message beening returned
  794. $this->assertTrue($acknowledgeMessage instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  795. // Check the message body is the expected data to be returned
  796. $this->assertEquals(4, count($acknowledgeMessage->body));
  797. }
  798. /**
  799. * @group ZF-5388
  800. * Issue if multipol parameters are sent and one is of type array is passed.
  801. */
  802. public function testMutiArrayParamaterAMF0()
  803. {
  804. $data[] = array('item1', 'item2');
  805. $data[] = array('item3', 'item4');
  806. $this->_server->setClass('Zend_Amf_testclass');
  807. $newBody = new Zend_Amf_Value_MessageBody("Zend_Amf_testclass.testMultiArrayParamater","/1",$data);
  808. $request = new Zend_Amf_Request();
  809. $request->addAmfBody($newBody);
  810. $request->setObjectEncoding(0x00);
  811. $result = $this->_server->handle($request);
  812. $response = $this->_server->getResponse();
  813. $responseBody = $response->getAmfBodies();
  814. // Now check if the return data was properly set.
  815. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  816. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  817. $this->assertEquals(4, count($responseBody[0]->getData()), var_export($responseBody, 1));
  818. }
  819. /**
  820. * @group ZF-5346
  821. */
  822. public function testSingleObjectParamaterAMF3()
  823. {
  824. // serialize the data to an AMF output stream
  825. $data[] = array('item1', 'item2');
  826. $data[] = array('item3', 'item4');
  827. $this->_server->setClass('Zend_Amf_testclass');
  828. // create a mock remoting message
  829. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  830. $message->operation = 'testMultiArrayParamater';
  831. $message->source = 'Zend_Amf_testclass';
  832. $message->body = $data;
  833. // create a mock message body to place th remoting message inside
  834. $newBody = new Zend_Amf_Value_MessageBody(null,"/1",$message);
  835. $request = new Zend_Amf_Request();
  836. // at the requested service to a request
  837. $request->addAmfBody($newBody);
  838. $request->setObjectEncoding(0x03);
  839. // let the server handle mock request
  840. $result = $this->_server->handle($request);
  841. $response = $this->_server->getResponse();
  842. $responseBody = $response->getAmfBodies();
  843. $this->assertTrue(0 < count($responseBody), var_export($responseBody, 1));
  844. $this->assertTrue(array_key_exists(0, $responseBody), var_export($responseBody, 1));
  845. // Now check if the return data was properly set.
  846. $acknowledgeMessage = $responseBody[0]->getData();
  847. // check that we have a message beening returned
  848. $this->assertTrue($acknowledgeMessage instanceof Zend_Amf_Value_Messaging_AcknowledgeMessage);
  849. // Check the message body is the expected data to be returned
  850. $this->assertEquals(4, count($acknowledgeMessage->body));
  851. }
  852. /**
  853. * Check that when using server->setSession you get an amf header that has an append to gateway sessionID
  854. * @group ZF-5381
  855. */
  856. public function testSessionAmf3()
  857. {
  858. Zend_Session::start();
  859. $this->_server->setClass('Zend_Amf_testSession');
  860. $this->_server->setSession();
  861. // create a mock remoting message
  862. $message = new Zend_Amf_Value_Messaging_RemotingMessage();
  863. $message->operation = 'getCount';
  864. $message->source = 'Zend_Amf_testSession';
  865. $message->body = array();
  866. // create a mock message body to place th remoting message inside
  867. $newBody = new Zend_Amf_Value_MessageBody(null,"/1", $message);
  868. $request = new Zend_Amf_Request();
  869. // at the requested service to a request
  870. $request->addAmfBody($newBody);
  871. $request->setObjectEncoding(0x03);
  872. // let the server handle mock request
  873. $result = $this->_server->handle($request);
  874. $response = $this->_server->getResponse();
  875. $responseBody = $response->getAmfBodies();
  876. // Now check if the return data was properly set.
  877. $acknowledgeMessage = $responseBody[0]->getData();
  878. // check that we have a message beening returned
  879. $this->assertEquals(1, $acknowledgeMessage->body);
  880. // check that a header is being returned for the session id
  881. $headerBody = $response->getAmfHeaders();
  882. $this->assertEquals('AppendToGatewayUrl',$headerBody[0]->name);
  883. // Do not stop session since it still can be used by other tests
  884. // Zend_Session::stop();
  885. }
  886. }
  887. if (PHPUnit_MAIN_METHOD == "Zend_Amf_ServerTest::main") {
  888. Zend_Amf_ServerTest::main();
  889. }
  890. /**
  891. * Zend_Amf_Server_testFunction
  892. *
  893. * Function for use with Amf server unit tests
  894. *
  895. * @param array $var1
  896. * @param string $var2
  897. * @return string
  898. */
  899. function Zend_Amf_Server_testFunction($var1, $var2 = 'optional')
  900. {
  901. return $var2 . ': ' . implode(',', (array) $var1);
  902. }
  903. /**
  904. * Zend_Amf_Server_testFunction2
  905. *
  906. * Function for use with Amf server unit tests
  907. *
  908. * @return string
  909. */
  910. function Zend_Amf_Server_testFunction2()
  911. {
  912. return 'function2';
  913. }
  914. /**
  915. * Class to used with Zend_Amf_Server unit tests.
  916. *
  917. */
  918. class Zend_Amf_testclass
  919. {
  920. public function __construct()
  921. {
  922. }
  923. /**
  924. * Concatinate a string
  925. *
  926. * @param string
  927. * @return string
  928. */
  929. public function test1($string = '')
  930. {
  931. return 'String: '. (string) $string;
  932. }
  933. /**
  934. * Test2
  935. *
  936. * Returns imploded array
  937. *
  938. * @param array $array
  939. * @return string
  940. */
  941. public static function test2($array)
  942. {
  943. return implode('; ', (array) $array);
  944. }
  945. /**
  946. * Test3
  947. *
  948. * Should not be available...
  949. *
  950. * @return void
  951. */
  952. protected function _test3()
  953. {
  954. }
  955. /**
  956. * Test base64 encoding in request and response
  957. *
  958. * @param base64 $data
  959. * @return base64
  960. */
  961. public function base64($data)
  962. {
  963. return $data;
  964. }
  965. /**
  966. * Test that invoke arguments are passed
  967. *
  968. * @param string $message message argument for comparisons
  969. * @return string
  970. */
  971. public function checkArgv($message)
  972. {
  973. $argv = func_get_args();
  974. return implode(':', $argv);
  975. }
  976. /**
  977. * Test static usage
  978. *
  979. * @param string $message
  980. * @return string
  981. */
  982. public static function checkStaticUsage($message)
  983. {
  984. return $message;
  985. }
  986. /**
  987. * Test throwing exceptions
  988. *
  989. * @return void
  990. */
  991. public function throwException()
  992. {
  993. throw new Exception('This exception should not be displayed');
  994. }
  995. /**
  996. * test if we can send an array as a paramater without it getting nested two
  997. * Used to test ZF-5388
  998. */
  999. public function testSingleArrayParamater($inputArray){
  1000. if( $inputArray[0] == 'item1' ){
  1001. return true;
  1002. }
  1003. return false;
  1004. }
  1005. /**
  1006. * This will crash if two arrays are not passed into the function.
  1007. * Used to test ZF-5388
  1008. */
  1009. public function testMultiArrayParamater($arrayOne, $arrayTwo)
  1010. {
  1011. return array_merge($arrayOne, $arrayTwo);
  1012. }
  1013. }
  1014. /**
  1015. * Class with private constructor
  1016. */
  1017. class Zend_Amf_testclassPrivate
  1018. {
  1019. private function __construct()
  1020. {
  1021. }
  1022. /**
  1023. * Test1
  1024. *
  1025. * Returns 'String: ' . $string
  1026. *
  1027. * @param string $string
  1028. * @return string
  1029. */
  1030. public function test1($string = '')
  1031. {
  1032. return 'String: '. (string) $string;
  1033. }
  1034. }
  1035. /**
  1036. * Example class for sending a session back to ActionScript.
  1037. */
  1038. class Zend_Amf_testSession
  1039. {
  1040. /** Check if the session is available or create it. */
  1041. public function __construct() {
  1042. if (!isset($_SESSION['count'])) {
  1043. $_SESSION['count'] = 0;
  1044. }
  1045. }
  1046. /** increment the current count session variable and return it's value */
  1047. public function getCount()
  1048. {
  1049. $_SESSION['count']++;
  1050. return $_SESSION['count'];
  1051. }
  1052. }