/trunk/src/main/webapp/test/core.js

http://jquery-stream.googlecode.com/ · JavaScript · 549 lines · 491 code · 58 blank · 0 comment · 25 complexity · 49c7353d6566e2b3cb19632fa45ce682 MD5 · raw file

  1. (function() {
  2. var script = "";
  3. $.ajax("../jquery.stream.js", {async: false})
  4. .success(function(data) {
  5. script = data;
  6. });
  7. this.teardown = function() {
  8. $(window).trigger("unload.stream");
  9. $.globalEval(script);
  10. };
  11. })();
  12. module("jQuery.stream", {
  13. setup: function() {
  14. $.stream.setup({
  15. enableXDR: true
  16. });
  17. },
  18. teardown: teardown
  19. });
  20. test("Finding the first stream object", function() {
  21. ok($.stream("stream", {}) == $.stream());
  22. ok($.stream("stream?second", {}) != $.stream());
  23. });
  24. test("Finding a stream object by url", function() {
  25. ok($.stream("stream", {}) == $.stream("stream"));
  26. });
  27. test("Finding a stream object by alias", function() {
  28. ok($.stream("stream", {alias: "s"}) == $.stream("s"));
  29. });
  30. asyncTest("Setting defaut options", function() {
  31. $.stream.setup({
  32. context: $("#undertow")
  33. });
  34. $.stream("stream", {
  35. open: function() {
  36. ok(true);
  37. equal(this.attr("id"), "undertow");
  38. start();
  39. }
  40. });
  41. });
  42. asyncTest("Global stream event handlers", 6, function() {
  43. var url = "stream?message=true&close=true";
  44. $("#undertow")
  45. .streamOpen(function(e, event, stream) {
  46. if (stream.url === url) {
  47. ok(true);
  48. }
  49. })
  50. .streamMessage(function(e, event, stream) {
  51. if (stream.url === url) {
  52. equal(event.data, "data");
  53. }
  54. })
  55. .streamClose(function(e, event, stream) {
  56. if (stream.url === url) {
  57. ok(true);
  58. start();
  59. }
  60. });
  61. $.stream(url, {
  62. reconnect: false,
  63. open: function() {
  64. ok(true);
  65. },
  66. message: function(event, stream) {
  67. equal(event.data, "data");
  68. },
  69. close: function() {
  70. ok(true);
  71. }
  72. });
  73. });
  74. asyncTest("Only local stream event handlers", 3, function() {
  75. var url = "stream?message=true&close=true";
  76. $("#undertow")
  77. .streamOpen(function(e, event, stream) {
  78. if (stream.url === url) {
  79. ok(false);
  80. }
  81. })
  82. .streamMessage(function(e, event, stream) {
  83. if (stream.url === url) {
  84. ok(false);
  85. }
  86. })
  87. .streamClose(function(e, event, stream) {
  88. if (stream.url === url) {
  89. ok(false);
  90. }
  91. });
  92. $.stream(url, {
  93. reconnect: false,
  94. global: false,
  95. open: function() {
  96. ok(true);
  97. },
  98. message: function(event, stream) {
  99. equal(event.data, "data");
  100. },
  101. close: function() {
  102. ok(true);
  103. start();
  104. }
  105. });
  106. });
  107. $.each({http: "HTTP Streaming", ws: "WebSocket"}, function(type, moduleName) {
  108. if (type === "ws" && !window.WebSocket) {
  109. return;
  110. }
  111. module(moduleName, {
  112. setup: function() {
  113. $.stream.setup({
  114. type: type,
  115. enableXDR: true
  116. });
  117. },
  118. teardown: teardown
  119. });
  120. asyncTest("Open event", function() {
  121. $.stream("stream", {
  122. open: function(event, stream) {
  123. ok(true);
  124. equal(event.type, "open");
  125. ok(stream == $.stream());
  126. equal(stream.readyState, 1);
  127. start();
  128. }
  129. });
  130. });
  131. asyncTest("Multiple event handlers", function() {
  132. var text = "";
  133. $.stream("stream", {
  134. open: [
  135. function() {
  136. text += "A";
  137. },
  138. function() {
  139. text += "B";
  140. },
  141. function() {
  142. text += "C";
  143. equal(text, "ABC");
  144. },
  145. start
  146. ]
  147. });
  148. });
  149. asyncTest("Events with context", function() {
  150. var ts = new Date().getTime();
  151. $.stream("stream", {
  152. context: {ts: ts},
  153. open: function(event, stream) {
  154. equal(this.ts, ts);
  155. start();
  156. }
  157. });
  158. });
  159. asyncTest("Message event", function() {
  160. $.stream("stream?message=true", {
  161. message: function(event, stream) {
  162. ok(event.data);
  163. equal(event.data, "data");
  164. start();
  165. }
  166. });
  167. });
  168. asyncTest("Message event with text data", function() {
  169. $.stream("stream?message=true&dataType=text", {
  170. dataType: "text",
  171. message: function(event, stream) {
  172. ok(event.data);
  173. equal(typeof event.data, "string");
  174. equal(event.data, "data");
  175. start();
  176. }
  177. });
  178. });
  179. asyncTest("Message event with json data", function() {
  180. $.stream("stream?message=true&dataType=json", {
  181. dataType: "json",
  182. message: function(event, stream) {
  183. ok(event.data);
  184. ok($.isPlainObject(event.data));
  185. equal(event.data.data, "data");
  186. start();
  187. }
  188. });
  189. });
  190. asyncTest("Message event with xml data", function() {
  191. $.stream("stream?message=true&dataType=xml", {
  192. dataType: "xml",
  193. message: function(event, stream) {
  194. ok(event.data);
  195. ok($.isXMLDoc(event.data));
  196. equal($("data", event.data).text(), "data");
  197. start();
  198. }
  199. });
  200. });
  201. asyncTest("Message event with custom data type", function() {
  202. $.stream("stream?message=true&dataType=csv", {
  203. dataType: "csv",
  204. converters: {
  205. csv: function(data) {
  206. return data.split(",");
  207. }
  208. },
  209. message: function(event, stream) {
  210. ok(event.data);
  211. ok($.isArray(event.data));
  212. equal(event.data[0], "data1");
  213. start();
  214. }
  215. });
  216. });
  217. asyncTest("Error event", function() {
  218. $.stream("stream?error=true", {
  219. error: function(event, stream) {
  220. ok(true);
  221. equal(stream.readyState, 3);
  222. start();
  223. },
  224. close: function(event, stream) {
  225. ok(true);
  226. equal(stream.readyState, 3);
  227. start();
  228. }
  229. });
  230. });
  231. asyncTest("Close event", function() {
  232. $.stream("stream?close=true", {
  233. reconnect: false,
  234. close: function(event, stream) {
  235. ok(true);
  236. equal(stream.readyState, 3);
  237. start();
  238. }
  239. });
  240. });
  241. asyncTest("Stream.close()", function() {
  242. $.stream("stream", {
  243. open: function(event, stream) {
  244. ok(true);
  245. stream.close();
  246. },
  247. close: function() {
  248. ok(true);
  249. start();
  250. }
  251. });
  252. });
  253. asyncTest("Reconnection", 2, function() {
  254. var i = 0;
  255. $.stream("stream?close=true", {
  256. open: function(event, stream) {
  257. i++;
  258. ok(true);
  259. if (i > 1) {
  260. stream.close();
  261. start();
  262. }
  263. }
  264. });
  265. });
  266. asyncTest("Stream.send() - query string data", function() {
  267. $.stream("stream", {
  268. open: function(event, stream) {
  269. stream.send("message=" + encodeURIComponent("Hollow Jan"));
  270. },
  271. message: function(event) {
  272. equal(event.data, "Hollow Jan");
  273. start();
  274. }
  275. });
  276. });
  277. asyncTest("Stream.send() - object data", function() {
  278. $.stream("stream", {
  279. open: function(event, stream) {
  280. stream.send({message: "Hollow Jan"});
  281. },
  282. message: function(event) {
  283. equal(event.data, "Hollow Jan");
  284. start();
  285. }
  286. });
  287. });
  288. asyncTest("openData - string data", function() {
  289. $.stream("stream", {
  290. openData: "message=true&dataType=json",
  291. dataType: "json",
  292. message: function(event) {
  293. equal(event.data.data, "data");
  294. start();
  295. }
  296. });
  297. });
  298. asyncTest("openData - object data", function() {
  299. $.stream("stream", {
  300. openData: {
  301. message: true,
  302. dataType: function() {
  303. return "json";
  304. }
  305. },
  306. dataType: "json",
  307. message: function(event) {
  308. equal(event.data.data, "data");
  309. start();
  310. }
  311. });
  312. });
  313. if (type === "ws") {
  314. asyncTest("Subprotocol", function() {
  315. var protocol = "test";
  316. $.stream("stream", {
  317. protocols: protocol,
  318. message: function(event, stream) {
  319. equal(event.data, protocol);
  320. start();
  321. }
  322. });
  323. });
  324. }
  325. if (type === "http") {
  326. asyncTest("Rewriting URL for XDomainRequest", function() {
  327. $.stream("stream?message=true", {
  328. rewriteURL: function(url) {
  329. ok(!!window.XDomainRequest);
  330. return url + "&dataType=json";
  331. },
  332. message: function(event) {
  333. equal(event.data, window.XDomainRequest ? "{\"data\":\"data\"}" : "data");
  334. start();
  335. }
  336. });
  337. });
  338. asyncTest("handleOpen", 3, function() {
  339. $.stream.setup({
  340. handleOpen: function(text, message, stream) {
  341. stream.id = text.substring(0, text.indexOf("\r\n"));
  342. message.index = text.indexOf("\r\n", stream.id.length + "\r\n".length) + "\r\n".length;
  343. if (text.indexOf("OPEN", message.index) < 0) {
  344. ok(true);
  345. return false;
  346. }
  347. ok(true);
  348. }
  349. });
  350. $.stream("stream", {
  351. openData: {differentFormat: true, delayOpen: true},
  352. open: function(event, stream) {
  353. ok(true);
  354. start();
  355. }
  356. });
  357. });
  358. asyncTest("handleMessage - text/plain", function() {
  359. $.stream.setup({
  360. handleOpen: function(text, message, stream) {
  361. stream.id = text.substring(0, text.indexOf("\r\n"));
  362. message.index = text.indexOf("\r\n", stream.id.length + "\r\n".length) + "\r\n".length;
  363. },
  364. handleMessage: function(text, message, stream) {
  365. var end = text.indexOf("\r\n", message.index);
  366. if (end < 0) {
  367. return false;
  368. }
  369. message.data = $.trim(text.substring(message.index, end));
  370. message.index = end + "\r\n".length;
  371. }
  372. });
  373. $.stream("stream", {
  374. openData: {differentFormat: true, dataType: "json", message: true},
  375. dataType: "json",
  376. message: function(event, stream) {
  377. equal(event.data.data, "data");
  378. start();
  379. }
  380. });
  381. });
  382. asyncTest("handleMessage - text/html", function() {
  383. $.stream.setup({
  384. handleOpen: function(text, message) {
  385. message.index = text.length;
  386. },
  387. handleMessage: function(text, message, stream) {
  388. if (!message.start) {
  389. var start = text.indexOf("<script>app.handle('", message.index);
  390. if (start < 0) {
  391. return false;
  392. }
  393. message.index = start + "<script>app.handle('".length;
  394. message.start = true;
  395. }
  396. var end = text.indexOf("')</script>", message.index);
  397. if (end < 0) {
  398. return false;
  399. }
  400. message.data = text.substring(message.index, end);
  401. message.index = end + "')</script>".length;
  402. delete message.start;
  403. }
  404. });
  405. $.stream("stream", {
  406. openData: {htmlContent: true, message: true},
  407. message: function(event, stream) {
  408. equal(event.data, "Hello World");
  409. start();
  410. }
  411. });
  412. });
  413. asyncTest("handleSend", 4, function() {
  414. var echo = "";
  415. $.stream.setup({
  416. handleSend: function(type, options, stream) {
  417. switch (type) {
  418. case "close":
  419. options.data = {"metadata.type": type, "metadata.id": stream.id};
  420. break;
  421. default:
  422. if (options.data.message % 2) {
  423. return false;
  424. }
  425. $.extend(true, options, {
  426. data: {"metadata.type": type, "metadata.id": stream.id},
  427. success: function() {
  428. ok(true);
  429. }
  430. });
  431. break;
  432. }
  433. }
  434. });
  435. $.stream("stream", {
  436. dataType: "json",
  437. open: function(event, stream) {
  438. for (var i = 0; i < 5; i++) {
  439. stream.send({message: i});
  440. }
  441. },
  442. message: function(event, stream) {
  443. echo += event.data;
  444. if (echo.length === 3) {
  445. equal(echo, "024");
  446. start();
  447. }
  448. }
  449. });
  450. });
  451. asyncTest("text/html with comment padding", function() {
  452. $.stream("stream", {
  453. openData: {
  454. htmlContent: true,
  455. onlyCommentPadding: true
  456. },
  457. handleOpen: $.noop,
  458. open: function() {
  459. ok(true);
  460. start();
  461. }
  462. });
  463. });
  464. asyncTest("should close connection - invalid message with NaN size", function() {
  465. var ts = new Date().getTime();
  466. $.stream("stream", {
  467. openData: {invalidMessage1: true},
  468. open: function() {
  469. ok(true);
  470. },
  471. close: function() {
  472. ok(new Date().getTime() - ts < 3000);
  473. start();
  474. }
  475. });
  476. });
  477. asyncTest("should close connection - invalid message with wrong size", function() {
  478. var ts = new Date().getTime();
  479. $.stream("stream", {
  480. openData: {invalidMessage2: true},
  481. open: function() {
  482. ok(true);
  483. },
  484. close: function() {
  485. ok(new Date().getTime() - ts < 3000);
  486. start();
  487. }
  488. });
  489. });
  490. }
  491. });