PageRenderTime 46ms CodeModel.GetById 24ms app.highlight 18ms RepoModel.GetById 0ms app.codeStats 1ms

/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	
  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.data = {"metadata.type": type, "metadata.id": stream.id};
472						break;
473					default:
474						if (options.data.message % 2) {
475							return false;
476						}
477					
478						$.extend(true, options, {
479							data: {"metadata.type": type, "metadata.id": stream.id},
480							success: function() {
481								ok(true);
482							}
483						});
484						break;
485					}
486				}
487			});
488			
489			$.stream("stream", {
490				dataType: "json",
491				open: function(event, stream) {
492					for (var i = 0; i < 5; i++) {
493						stream.send({message: i});
494					}
495				},
496				message: function(event, stream) {
497					echo += event.data;
498					if (echo.length === 3) {
499						equal(echo, "024");						
500						start();
501					}
502				}
503			});
504		});
505		
506		asyncTest("text/html with comment padding", function() {
507			$.stream("stream", {
508				openData: {
509					htmlContent: true,
510					onlyCommentPadding: true
511				},
512				handleOpen: $.noop,
513				open: function() {
514					ok(true);
515					start();
516				}
517			});
518		});
519		
520		asyncTest("should close connection - invalid message with NaN size", function() {
521			var ts = new Date().getTime();
522			$.stream("stream", {
523				openData: {invalidMessage1: true},
524				open: function() {
525					ok(true);
526				},
527				close: function() {
528					ok(new Date().getTime() - ts < 3000);
529					start();
530				}
531			});
532		});
533		
534		asyncTest("should close connection - invalid message with wrong size", function() {
535			var ts = new Date().getTime();
536			$.stream("stream", {
537				openData: {invalidMessage2: true},
538				open: function() {
539					ok(true);
540				},
541				close: function() {
542					ok(new Date().getTime() - ts < 3000);
543					start();
544				}
545			});
546		});
547	}
548	
549});