PageRenderTime 82ms CodeModel.GetById 16ms app.highlight 61ms RepoModel.GetById 1ms app.codeStats 0ms

/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	
  8	this.teardown = function() {
  9		$(window).trigger("unload.stream");
 10		$.globalEval(script);
 11	};
 12})();
 13
 14module("jQuery.stream", {
 15	setup: function() {
 16		$.stream.setup({
 17			enableXDR: true
 18		});
 19	},
 20	teardown: teardown
 21});
 22
 23test("Finding the first stream object", function() {
 24	ok($.stream("stream", {}) == $.stream());
 25	ok($.stream("stream?second", {}) != $.stream());
 26});
 27
 28test("Finding a stream object by url", function() {
 29	ok($.stream("stream", {}) == $.stream("stream"));
 30});
 31
 32test("Finding a stream object by alias", function() {
 33	ok($.stream("stream", {alias: "s"}) == $.stream("s"));
 34});
 35
 36asyncTest("Setting defaut options", function() {
 37	$.stream.setup({
 38		context: $("#undertow")
 39	});
 40	
 41	$.stream("stream", {
 42		open: function() {
 43			ok(true);
 44			equal(this.attr("id"), "undertow");
 45			start();
 46		}
 47	});
 48});
 49
 50asyncTest("Global stream event handlers", 6, function() {
 51	var url = "stream?message=true&close=true";
 52	
 53	$("#undertow")
 54	.streamOpen(function(e, event, stream) {
 55		if (stream.url === url) {
 56			ok(true);
 57		}
 58	})
 59	.streamMessage(function(e, event, stream) {
 60		if (stream.url === url) {
 61			equal(event.data, "data");
 62		}
 63	})
 64	.streamClose(function(e, event, stream) {
 65		if (stream.url === url) {
 66			ok(true);
 67			start();
 68		}
 69	});
 70	
 71	$.stream(url, {
 72		reconnect: false,
 73		open: function() {
 74			ok(true);
 75		},
 76		message: function(event, stream) {
 77			equal(event.data, "data");
 78		},
 79		close: function() {
 80			ok(true);
 81		}
 82	});
 83});
 84
 85asyncTest("Only local stream event handlers", 3, function() {
 86	var url = "stream?message=true&close=true";
 87	
 88	$("#undertow")
 89	.streamOpen(function(e, event, stream) {
 90		if (stream.url === url) {
 91			ok(false);
 92		}
 93	})
 94	.streamMessage(function(e, event, stream) {
 95		if (stream.url === url) {
 96			ok(false);
 97		}
 98	})
 99	.streamClose(function(e, event, stream) {
100		if (stream.url === url) {
101			ok(false);
102		}
103	});
104	
105	$.stream(url, {
106		reconnect: false,
107		global: false,
108		open: function() {
109			ok(true);
110		},
111		message: function(event, stream) {
112			equal(event.data, "data");
113		},
114		close: function() {
115			ok(true);
116			start();
117		}
118	});
119});
120
121$.each({http: "HTTP Streaming", ws: "WebSocket"}, function(type, moduleName) {
122	
123	if (type === "ws" && !window.WebSocket) {
124		return;
125	}
126	
127	module(moduleName, {
128		setup: function() {
129			$.stream.setup({
130				type: type,
131				enableXDR: true
132			});
133		}, 
134		teardown: teardown
135	});
136	
137	asyncTest("Open event", function() {
138		$.stream("stream", {
139			open: function(event, stream) {
140				ok(true);
141				equal(event.type, "open");
142				ok(stream == $.stream());
143				equal(stream.readyState, 1);
144				start();
145			}
146		});
147	});
148
149	asyncTest("Multiple event handlers", function() {
150		var text = "";
151		
152		$.stream("stream", {
153			open: [
154				function() {
155					text += "A";
156				},
157				function() {
158					text += "B";
159				},
160				function() {
161					text += "C";
162					equal(text, "ABC");
163				},
164				start
165			]
166		});
167	});
168
169	asyncTest("Events with context", function() {
170		var ts = new Date().getTime();
171		
172		$.stream("stream", {
173			context: {ts: ts},
174			open: function(event, stream) {
175				equal(this.ts, ts);
176				start();
177			}
178		});
179	});
180
181	asyncTest("Message event", function() {
182		$.stream("stream?message=true", {
183			message: function(event, stream) {
184				ok(event.data);
185				equal(event.data, "data");
186				start();
187			}
188		});
189	});
190
191	asyncTest("Message event with text data", function() {
192		$.stream("stream?message=true&dataType=text", {
193			dataType: "text",
194			message: function(event, stream) {
195				ok(event.data);
196				equal(typeof event.data, "string");
197				equal(event.data, "data");
198				start();
199			}
200		});
201	});
202
203	asyncTest("Message event with json data", function() {
204		$.stream("stream?message=true&dataType=json", {
205			dataType: "json",
206			message: function(event, stream) {
207				ok(event.data);
208				ok($.isPlainObject(event.data));
209				equal(event.data.data, "data");
210				start();
211			}
212		});
213	});
214
215	asyncTest("Message event with xml data", function() {
216		$.stream("stream?message=true&dataType=xml", {
217			dataType: "xml",
218			message: function(event, stream) {
219				ok(event.data);
220				ok($.isXMLDoc(event.data));
221				equal($("data", event.data).text(), "data");
222				start();
223			}
224		});
225	});
226
227	asyncTest("Message event with custom data type", function() {
228		$.stream("stream?message=true&dataType=csv", {
229			dataType: "csv",
230			converters: {
231				csv: function(data) {
232					return data.split(",");
233				}
234			},
235			message: function(event, stream) {
236				ok(event.data);
237				ok($.isArray(event.data));
238				equal(event.data[0], "data1");
239				start();
240			}
241		});
242	});
243
244	asyncTest("Error event", function() {
245		$.stream("stream?error=true", {
246			error: function(event, stream) {
247				ok(true);
248				equal(stream.readyState, 3);
249				start();
250			},
251			close: function(event, stream) {
252				ok(true);
253				equal(stream.readyState, 3);
254				start();
255			}
256		});
257	});
258
259	asyncTest("Close event", function() {
260		$.stream("stream?close=true", {
261			reconnect: false,
262			close: function(event, stream) {
263				ok(true);
264				equal(stream.readyState, 3);
265				start();
266			}
267		});
268	});
269	
270	asyncTest("Stream.close()", function() {
271		$.stream("stream", {
272			open: function(event, stream) {
273				ok(true);
274				stream.close();
275			},
276			close: function() {
277				ok(true);
278				start();
279			}
280		});
281	});
282
283	asyncTest("Reconnection", 2, function() {
284		var i = 0;
285		$.stream("stream?close=true", {
286			open: function(event, stream) {
287				i++;
288				ok(true);
289				
290				if (i > 1) {
291					stream.close();
292					start();
293				}
294			}
295		});
296	});
297
298	asyncTest("Stream.send() - query string data",  function() {
299		$.stream("stream", {
300			open: function(event, stream) {
301				stream.send("message=" + encodeURIComponent("Hollow Jan"));
302			},
303			message: function(event) {
304				equal(event.data, "Hollow Jan");
305				start();
306			}
307		});
308	});
309
310	asyncTest("Stream.send() - object data",  function() {
311		$.stream("stream", {
312			open: function(event, stream) {
313				stream.send({message: "Hollow Jan"});
314			},
315			message: function(event) {
316				equal(event.data, "Hollow Jan");
317				start();
318			}
319		});
320	});
321
322	asyncTest("openData - string data",  function() {
323		$.stream("stream", {
324			openData: "message=true&dataType=json",
325			dataType: "json",
326			message: function(event) {
327				equal(event.data.data, "data");
328				start();
329			}
330		});
331	});
332	
333	asyncTest("openData - object data",  function() {
334		$.stream("stream", {
335			openData: {
336				message: true,
337				dataType: function() {
338					return "json";
339				}
340			},
341			dataType: "json",
342			message: function(event) {
343				equal(event.data.data, "data");
344				start();
345			}
346		});
347	});
348	
349	if (type === "ws") {
350		asyncTest("Subprotocol", function() {
351			var protocol = "test";
352			
353			$.stream("stream", {
354				protocols: protocol,
355				message: function(event, stream) {
356					equal(event.data, protocol);
357					start();
358				}
359			});
360		});
361	}
362	
363	if (type === "http") {
364		asyncTest("Rewriting URL for XDomainRequest", function() {
365			$.stream("stream?message=true", {
366				rewriteURL: function(url) {
367					ok(!!window.XDomainRequest);
368					return url + "&dataType=json";
369				},
370				message: function(event) {
371					equal(event.data, window.XDomainRequest ? "{\"data\":\"data\"}" : "data");
372					start();
373				}
374			});
375		});
376		
377		asyncTest("handleOpen", 3, function() {
378			$.stream.setup({
379				handleOpen: function(text, message, stream) {
380					stream.id = text.substring(0, text.indexOf("\r\n"));
381					message.index = text.indexOf("\r\n", stream.id.length + "\r\n".length) + "\r\n".length;
382					
383					if (text.indexOf("OPEN", message.index) < 0) {
384						ok(true);
385						return false;
386					}
387					
388					ok(true);
389				}
390			});
391			
392			$.stream("stream", {
393				openData: {differentFormat: true, delayOpen: true},
394				open: function(event, stream) {
395					ok(true);
396					start();
397				}
398			});
399		});
400		
401		asyncTest("handleMessage - text/plain", function() {
402			$.stream.setup({
403				handleOpen: function(text, message, stream) {
404					stream.id = text.substring(0, text.indexOf("\r\n"));
405					message.index = text.indexOf("\r\n", stream.id.length + "\r\n".length) + "\r\n".length;
406				},
407				handleMessage: function(text, message, stream) {
408					var end = text.indexOf("\r\n", message.index);
409					if (end < 0) {
410						return false;
411					}
412					
413					message.data = $.trim(text.substring(message.index, end));
414					message.index = end + "\r\n".length;
415				}
416			});
417			
418			$.stream("stream", {
419				openData: {differentFormat: true, dataType: "json", message: true},
420				dataType: "json",
421				message: function(event, stream) {
422					equal(event.data.data, "data");
423					start();
424				}
425			});
426		});
427		
428		asyncTest("handleMessage - text/html", function() {
429			$.stream.setup({
430				handleOpen: function(text, message) {
431					message.index = text.length;
432				},
433				handleMessage: function(text, message, stream) {
434					if (!message.start) {
435						var start = text.indexOf("<script>app.handle('", message.index);
436						if (start < 0) {
437							return false;
438						}
439						
440						message.index = start + "<script>app.handle('".length;
441						message.start = true;
442					}
443					
444					var end = text.indexOf("')</script>", message.index);
445					if (end < 0) {
446						return false;
447					}
448					
449					message.data = text.substring(message.index, end);
450					message.index = end + "')</script>".length;
451					delete message.start;
452				}
453			});
454			
455			$.stream("stream", {
456				openData: {htmlContent: true, message: true},
457				message: function(event, stream) {
458					equal(event.data, "Hello World");
459					start();
460				}
461			});
462		});
463		
464		asyncTest("handleSend", 4, function() {
465			var echo = "";
466
467			$.stream.setup({
468				handleSend: function(type, options, stream) {
469					switch (type) {
470					case "close":
471						options.headers = {
472							"x-jquery-stream-id": stream.id,
473							"x-jquery-stream-type": type
474						};
475						break;
476					default:
477						if (options.data.message % 2) {
478							return false;
479						}
480					
481						$.extend(true, options, {
482							headers: {
483								"x-jquery-stream-id": stream.id,
484								"x-jquery-stream-type": type
485							},
486							success: function() {
487								ok(true);
488							}
489						});
490						break;
491					}
492				}
493			});
494			
495			$.stream("stream", {
496				dataType: "json",
497				open: function(event, stream) {
498					for (var i = 0; i < 5; i++) {
499						stream.send({message: i});
500					}
501				},
502				message: function(event, stream) {
503					echo += event.data;
504					if (echo.length === 3) {
505						equal(echo, "024");						
506						start();
507					}
508				}
509			});
510		});
511		
512		asyncTest("text/html with comment padding", function() {
513			$.stream("stream", {
514				openData: {
515					htmlContent: true,
516					onlyCommentPadding: true
517				},
518				handleOpen: $.noop,
519				open: function() {
520					ok(true);
521					start();
522				}
523			});
524		});
525
526		test("Choosing transport", 2, function() {
527			$.stream.transport("mock", function(stream) {
528				ok(true);
529				stream.options.handleSend = function() {
530					return false;
531				};
532				
533				return {
534					open: function() {},
535					close: function() {}
536				};
537			});
538			
539			$.stream("1", {
540				type: "mock"
541			});
542			$.stream("2", {
543				type: function() {
544					return "mock";
545				}
546			});
547		});
548
549		asyncTest("Adding transport", function() {
550			var closed = false;
551			
552			$.stream.transport("mock", function(stream, eh) {
553				ok(true);
554				stream.options.handleSend = function() {
555					return false;
556				};
557
558				return {
559					open: function() {
560						var text = "";
561						eh.onread(text += "id;padding;");
562						eh.onread(text += "5;Hello;");
563					},
564					close: function() {
565						closed = true;
566						eh.onclose();
567					}
568				};
569			});
570			
571			$.stream("1", {
572				type: "mock",
573				open: function(event, stream) {
574					equal(stream.id, "id");
575				},
576				message: function(event, stream) {
577					equal(event.data, "Hello");
578					stream.close();
579				},
580				close: function() {
581					ok(closed);
582					start();
583				}
584			});
585		});
586		
587		asyncTest("Adding transport using eh.onopen and eh.onmessage", function() {
588			var closed = false;
589			
590			$.stream.transport("mock", function(stream, eh) {
591				ok(true);
592				stream.options.handleSend = function() {
593					return false;
594				};
595
596				return {
597					open: function() {
598						eh.onopen();
599						eh.onmessage({data: "Hello"});
600					},
601					close: function() {
602						closed = true;
603						eh.onclose();
604					}
605				};
606			});
607						
608			$.stream("1", {
609				type: "mock",
610				open: function(event, stream) {
611					ok(true);
612				},
613				message: function(event, stream) {
614					equal(event.data, "Hello");
615					stream.close();
616				},
617				close: function() {
618					ok(closed);
619					start();
620				}
621			});
622		});
623
624		asyncTest("Parsing message - invalid open", function() {
625			$.stream.transport("test", function(stream, eh) {
626				ok(true);
627				stream.options.handleSend = function() {
628					return false;
629				};
630
631				return {
632					open: function() {
633						eh.onread("id;     ");
634					},
635					close: function() {
636						ok(true);
637						eh.onclose();
638					}
639				};
640			});
641			
642			$.stream("stream", {
643				type: "test",
644				close: start
645			});
646		});
647		
648		asyncTest("Parsing message - invalid message with NaN size", function() {
649			$.stream.transport("test", function(stream, eh) {
650				ok(true);
651				stream.options.handleSend = function() {
652					return false;
653				};
654
655				return {
656					open: function() {
657						var text = "";
658						eh.onread(text += "id;padding;");
659						eh.onread(text += "char;hello;");
660					},
661					close: function() {
662						ok(true);
663						eh.onclose();
664					}
665				};
666			});
667			
668			$.stream("stream", {
669				type: "test",
670				open: function() {
671					ok(true);
672				},
673				close: start
674			});
675		});
676		
677		asyncTest("Parsing message - invalid message with wrong size", function() {
678			$.stream.transport("test", function(stream, eh) {
679				ok(true);
680				stream.options.handleSend = function() {
681					return false;
682				};
683
684				return {
685					open: function() {
686						var text = "";
687						eh.onread(text += "id;padding;");
688						eh.onread(text += "3;black;");
689					},
690					close: function() {
691						ok(true);
692						eh.onclose();
693					}
694				};
695			});
696			
697			$.stream("stream", {
698				type: "test",
699				open: function() {
700					ok(true);
701				},
702				close: start
703			});
704		});
705	}
706	
707});