/tags/1.3b1/src/main/webapp/test/core.js

http://jquery-stream.googlecode.com/ · JavaScript · 707 lines · 631 code · 76 blank · 0 comment · 25 complexity · 84659faa13ddc0932fee36561618d4ff 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.headers = {
  420. "x-jquery-stream-id": stream.id,
  421. "x-jquery-stream-type": type
  422. };
  423. break;
  424. default:
  425. if (options.data.message % 2) {
  426. return false;
  427. }
  428. $.extend(true, options, {
  429. headers: {
  430. "x-jquery-stream-id": stream.id,
  431. "x-jquery-stream-type": type
  432. },
  433. success: function() {
  434. ok(true);
  435. }
  436. });
  437. break;
  438. }
  439. }
  440. });
  441. $.stream("stream", {
  442. dataType: "json",
  443. open: function(event, stream) {
  444. for (var i = 0; i < 5; i++) {
  445. stream.send({message: i});
  446. }
  447. },
  448. message: function(event, stream) {
  449. echo += event.data;
  450. if (echo.length === 3) {
  451. equal(echo, "024");
  452. start();
  453. }
  454. }
  455. });
  456. });
  457. asyncTest("text/html with comment padding", function() {
  458. $.stream("stream", {
  459. openData: {
  460. htmlContent: true,
  461. onlyCommentPadding: true
  462. },
  463. handleOpen: $.noop,
  464. open: function() {
  465. ok(true);
  466. start();
  467. }
  468. });
  469. });
  470. test("Choosing transport", 2, function() {
  471. $.stream.transport("mock", function(stream) {
  472. ok(true);
  473. stream.options.handleSend = function() {
  474. return false;
  475. };
  476. return {
  477. open: function() {},
  478. close: function() {}
  479. };
  480. });
  481. $.stream("1", {
  482. type: "mock"
  483. });
  484. $.stream("2", {
  485. type: function() {
  486. return "mock";
  487. }
  488. });
  489. });
  490. asyncTest("Adding transport", function() {
  491. var closed = false;
  492. $.stream.transport("mock", function(stream, eh) {
  493. ok(true);
  494. stream.options.handleSend = function() {
  495. return false;
  496. };
  497. return {
  498. open: function() {
  499. var text = "";
  500. eh.onread(text += "id;padding;");
  501. eh.onread(text += "5;Hello;");
  502. },
  503. close: function() {
  504. closed = true;
  505. eh.onclose();
  506. }
  507. };
  508. });
  509. $.stream("1", {
  510. type: "mock",
  511. open: function(event, stream) {
  512. equal(stream.id, "id");
  513. },
  514. message: function(event, stream) {
  515. equal(event.data, "Hello");
  516. stream.close();
  517. },
  518. close: function() {
  519. ok(closed);
  520. start();
  521. }
  522. });
  523. });
  524. asyncTest("Adding transport using eh.onopen and eh.onmessage", function() {
  525. var closed = false;
  526. $.stream.transport("mock", function(stream, eh) {
  527. ok(true);
  528. stream.options.handleSend = function() {
  529. return false;
  530. };
  531. return {
  532. open: function() {
  533. eh.onopen();
  534. eh.onmessage({data: "Hello"});
  535. },
  536. close: function() {
  537. closed = true;
  538. eh.onclose();
  539. }
  540. };
  541. });
  542. $.stream("1", {
  543. type: "mock",
  544. open: function(event, stream) {
  545. ok(true);
  546. },
  547. message: function(event, stream) {
  548. equal(event.data, "Hello");
  549. stream.close();
  550. },
  551. close: function() {
  552. ok(closed);
  553. start();
  554. }
  555. });
  556. });
  557. asyncTest("Parsing message - invalid open", function() {
  558. $.stream.transport("test", function(stream, eh) {
  559. ok(true);
  560. stream.options.handleSend = function() {
  561. return false;
  562. };
  563. return {
  564. open: function() {
  565. eh.onread("id; ");
  566. },
  567. close: function() {
  568. ok(true);
  569. eh.onclose();
  570. }
  571. };
  572. });
  573. $.stream("stream", {
  574. type: "test",
  575. close: start
  576. });
  577. });
  578. asyncTest("Parsing message - invalid message with NaN size", function() {
  579. $.stream.transport("test", function(stream, eh) {
  580. ok(true);
  581. stream.options.handleSend = function() {
  582. return false;
  583. };
  584. return {
  585. open: function() {
  586. var text = "";
  587. eh.onread(text += "id;padding;");
  588. eh.onread(text += "char;hello;");
  589. },
  590. close: function() {
  591. ok(true);
  592. eh.onclose();
  593. }
  594. };
  595. });
  596. $.stream("stream", {
  597. type: "test",
  598. open: function() {
  599. ok(true);
  600. },
  601. close: start
  602. });
  603. });
  604. asyncTest("Parsing message - invalid message with wrong size", function() {
  605. $.stream.transport("test", function(stream, eh) {
  606. ok(true);
  607. stream.options.handleSend = function() {
  608. return false;
  609. };
  610. return {
  611. open: function() {
  612. var text = "";
  613. eh.onread(text += "id;padding;");
  614. eh.onread(text += "3;black;");
  615. },
  616. close: function() {
  617. ok(true);
  618. eh.onclose();
  619. }
  620. };
  621. });
  622. $.stream("stream", {
  623. type: "test",
  624. open: function() {
  625. ok(true);
  626. },
  627. close: start
  628. });
  629. });
  630. }
  631. });