TestRemoting.hx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package unit;
  2. import haxe.remoting.SocketProtocol;
  3. #if flash
  4. import haxe.remoting.SocketWrapper;
  5. #end
  6. class TestRemoting extends Test {
  7. static var HOST = "dev.unit-tests";
  8. static var PORT = 1999;
  9. static var _ : Dynamic = init();
  10. static var ecnx : haxe.remoting.ExternalConnection;
  11. static var ecnx2 : haxe.remoting.ExternalConnection;
  12. static var ecnx3 : haxe.remoting.ExternalConnection;
  13. static var lcnx : haxe.remoting.LocalConnection;
  14. static var fjscnx : haxe.remoting.FlashJsConnection;
  15. static function staticMethod( a : Int, b : Int ) {
  16. return a + b;
  17. }
  18. static function init() {
  19. var ctx = RemotingApi.context();
  20. #if flash
  21. if( !flash.external.ExternalInterface.available ) return;
  22. ecnx = haxe.remoting.ExternalConnection.jsConnect("cnx",ctx);
  23. ecnx3 = haxe.remoting.ExternalConnection.jsConnect("unknown",ctx);
  24. lcnx = haxe.remoting.LocalConnection.connect("local",ctx,[HOST]);
  25. fjscnx = haxe.remoting.FlashJsConnection.connect("cnx",#if flash9 "haxeFlash8" #else "haxeFlash9" #end,ctx);
  26. #elseif js
  27. ecnx = haxe.remoting.ExternalConnection.flashConnect("cnx","haxeFlash8",ctx);
  28. ecnx2 = haxe.remoting.ExternalConnection.flashConnect("cnx","haxeFlash9",ctx);
  29. ecnx3 = haxe.remoting.ExternalConnection.flashConnect("nothing","haxeFlash8",ctx);
  30. #end
  31. }
  32. public function test() {
  33. #if flash
  34. if( !flash.external.ExternalInterface.available ) return;
  35. #end
  36. // external connection
  37. #if (flash || js)
  38. doTestConnection(ecnx);
  39. #end
  40. #if !php // accessing the properties of a null object generates a fatal error in php
  41. exc(function() ecnx3.api.add.call([1,3]));
  42. #end
  43. #if js
  44. doTestConnection(ecnx2);
  45. #end
  46. #if flash
  47. // local connection
  48. doTestAsyncConnection(lcnx);
  49. // flash-flash through-js connection
  50. doTestAsyncConnection(fjscnx);
  51. #end
  52. #if (js || neko || php)
  53. // http sync connection
  54. var hcnx = haxe.remoting.HttpConnection.urlConnect("http://"+HOST+"/remoting.n");
  55. doTestConnection(hcnx);
  56. // test wrappers
  57. var dcnx = haxe.remoting.AsyncDebugConnection.create(haxe.remoting.AsyncAdapter.create(hcnx));
  58. dcnx.setErrorDebug(function(path,args,e) {});
  59. dcnx.setResultDebug(function(path,args,ret) {});
  60. dcnx.setCallDebug(function(path,args) {});
  61. doTestAsyncConnection(dcnx);
  62. #end
  63. // http async connection
  64. var hcnx = haxe.remoting.HttpAsyncConnection.urlConnect("http://"+HOST+"/remoting.n");
  65. doTestAsyncConnection(hcnx);
  66. var dcnx = haxe.remoting.DelayedConnection.create();
  67. dcnx.connection = hcnx;
  68. doTestAsyncConnection(dcnx);
  69. // socket connection
  70. #if (flash || neko || php)
  71. async( doConnect, new Socket(), true );
  72. #elseif js
  73. async( doConnect, new Socket("haxeFlash8"), true );
  74. async( doConnect, new Socket("haxeFlash9"), true );
  75. #end
  76. #if swf_mark
  77. return;
  78. #end
  79. var actx = new haxe.remoting.ContextAll();
  80. actx.addObject("fake",{ TestRemoting : TestRemoting },true);
  81. eq( actx.call(["unit","TestRemoting","staticMethod"],[2,3]), 5 );
  82. exc( function() actx.call(["unit2","TestRemoting","staticMethod"],[2,3]) );
  83. exc( function() actx.call(["unit","TestRemoting2","staticMethod"],[2,3]) );
  84. exc( function() actx.call(["unit","TestRemoting","staticMethod2"],[2,3]) );
  85. eq( actx.call(["fake","TestRemoting","staticMethod"],[2,3]), 5 );
  86. }
  87. function doConnect( s : Socket, onResult : Bool -> Void ) {
  88. var me = this;
  89. #if flash9
  90. var connected = false;
  91. s.addEventListener(flash.events.Event.CONNECT,function(e) {
  92. connected = true;
  93. me.doTestSocket(s);
  94. onResult(true);
  95. });
  96. s.addEventListener(flash.events.SecurityErrorEvent.SECURITY_ERROR,function(e) {
  97. onResult(false);
  98. });
  99. s.addEventListener(flash.events.Event.CLOSE,function(e) {
  100. if( !connected )
  101. onResult(false);
  102. });
  103. s.connect(HOST,PORT);
  104. #elseif (flash || js)
  105. s.onConnect = function(success) {
  106. if( success ) me.doTestSocket(s);
  107. onResult(success);
  108. };
  109. s.connect(HOST,PORT);
  110. #elseif neko
  111. var ret = try { s.connect(new neko.net.Host(HOST),PORT); true; } catch( e : Dynamic ) false;
  112. if( ret ) doTestSocket(s);
  113. onResult(ret);
  114. #elseif php
  115. var ret = try { s.connect(new php.net.Host(HOST),PORT); true; } catch( e : Dynamic ) false;
  116. if( ret ) doTestSocket(s);
  117. onResult(ret);
  118. #end
  119. }
  120. function doTestSocket( s : Socket ) {
  121. #if (neko || php)
  122. var scnx = haxe.remoting.SyncSocketConnection.create(s,new haxe.remoting.Context());
  123. doTestConnection(scnx);
  124. #else
  125. var scnx = haxe.remoting.SocketConnection.create(s,new haxe.remoting.Context());
  126. doTestAsyncConnection(scnx);
  127. #end
  128. }
  129. function doTestConnection( cnx : haxe.remoting.Connection ) {
  130. eq( cnx.api.add.call([1,2]), 3 );
  131. var strings = ["bla","\n","\r","\n\r","\t"," "," ","&","<",">","&nbsp;","&gt;","<br/>"];
  132. for( s in strings ) {
  133. infos("using "+s);
  134. eq( cnx.api.id.call([s]), s );
  135. eq( cnx.api.arr.call([[s,s,s]]), [s,s,s].join("#") );
  136. }
  137. infos(null);
  138. eq( cnx.api.exc.call([null]), null );
  139. exc( function() cnx.api.exc.call([5]) );
  140. exc( function() cnx.api.call([]) );
  141. exc( function() cnx.call([]) );
  142. exc( function() cnx.api.unknown.call([]) );
  143. exc( function() cnx.api.sub.add.call([1,2]) );
  144. eq( cnx.apirec.sub.add.call([1,2]), 3 );
  145. }
  146. function doTestAsyncConnection( cnx : haxe.remoting.AsyncConnection ) {
  147. var asyncExc = callback(asyncExc,cnx.setErrorHandler);
  148. async( cnx.api.add.call, [1,2], 3 );
  149. var strings = ["bla","\n","\r","\n\r","\t"," "," ","&","<",">","&nbsp;","&gt;","<br/>"];
  150. for( s in strings ) {
  151. async( cnx.api.id.call, [s], s );
  152. async( cnx.api.arr.call, [[s,s,s]], [s,s,s].join("#") );
  153. }
  154. async( cnx.api.exc.call, [null], null );
  155. asyncExc( cnx.api.exc.call, [5] );
  156. asyncExc( cnx.api.call, [] );
  157. asyncExc( cnx.call, [] );
  158. asyncExc( cnx.api.unknown.call, [] );
  159. asyncExc( cnx.api.sub.add.call, [1,2] );
  160. async( cnx.apirec.sub.add.call, [1,2], 3 );
  161. }
  162. }