Commit 28d810db authored by Wandenberg's avatar Wandenberg

wrap some javascript methods on a Utils class to make tests easier

parent 7283136d
......@@ -43,9 +43,7 @@ begin
destiny_path = File.dirname(file).gsub(/.*\/js\/?/, File.expand_path('pushstream/js', Dir.tmpdir))
FileUtils.mkdir_p(destiny_path)
content = File.read file
content.gsub!('(function (window, document, undefined) {', '')
content.gsub!('if (window.PushStream) { return; }', '')
content.gsub!('})(window, document);', '')
content.gsub!('window.PushStream = PushStream;', "window.PushStream = PushStream;\nwindow.Utils = Utils;")
File.open(File.join(destiny_path, File.basename(file)), 'w') {|f| f.write content }
end
end
......
......@@ -30,6 +30,8 @@
/* prevent duplicate declaration */
if (window.PushStream) { return; }
var Utils = {};
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
......@@ -37,7 +39,7 @@
return ((value < 10) ? '0' : '') + value;
};
var dateToUTCString = function (date) {
Utils.dateToUTCString = function (date) {
var time = valueToTwoDigits(date.getUTCHours()) + ':' + valueToTwoDigits(date.getUTCMinutes()) + ':' + valueToTwoDigits(date.getUTCSeconds());
return days[date.getUTCDay()] + ', ' + valueToTwoDigits(date.getUTCDate()) + ' ' + months[date.getUTCMonth()] + ' ' + date.getUTCFullYear() + ' ' + time + ' GMT';
};
......@@ -64,7 +66,7 @@
return value.replace(/^\s*/, "").replace(/\s*$/, "");
};
var parseJSON = function(data) {
Utils.parseJSON = function(data) {
if (!data || !isString(data)) {
return null;
}
......@@ -310,10 +312,10 @@
return (text) ? window.unescape(text) : '';
};
var parseMessage = function(messageText, keys) {
Utils.parseMessage = function(messageText, keys) {
var msg = messageText;
if (isString(messageText)) {
msg = parseJSON(messageText);
msg = Utils.parseJSON(messageText);
}
var message = {
......@@ -377,7 +379,7 @@
return url;
};
var extract_xss_domain = function(domain) {
Utils.extract_xss_domain = function(domain) {
// if domain is an ip address return it, else return ate least the last two parts of it
if (domain.match(/^(\d{1,3}\.){3}\d{1,3}$/)) {
return domain;
......@@ -407,7 +409,7 @@
/* common callbacks */
var onmessageCallback = function(event) {
Log4js.info("[" + this.type + "] message received", arguments);
var message = parseMessage(event.data, this.pushstream);
var message = Utils.parseMessage(event.data, this.pushstream);
this.pushstream._onmessage(message.data, message.id, message.channel, message.eventid, true);
};
......@@ -526,7 +528,7 @@
StreamWrapper.prototype = {
connect: function() {
this._closeCurrentConnection();
var domain = extract_xss_domain(this.pushstream.host);
var domain = Utils.extract_xss_domain(this.pushstream.host);
try {
document.domain = domain;
} catch(e) {
......@@ -657,8 +659,8 @@
this._closeCurrentConnection();
this.connectionEnabled = true;
this.xhrSettings.url = getSubscriberUrl(this.pushstream, this.pushstream.urlPrefixLongpolling);
var domain = extract_xss_domain(this.pushstream.host);
var currentDomain = extract_xss_domain(window.location.hostname);
var domain = Utils.extract_xss_domain(this.pushstream.host);
var currentDomain = Utils.extract_xss_domain(window.location.hostname);
var port = this.pushstream.port;
var currentPort = window.location.port || (this.pushstream.useSSL ? 443 : 80);
this.useJSONP = (domain !== currentDomain) || (port !== currentPort) || this.pushstream.longPollingUseJSONP;
......@@ -711,7 +713,7 @@
if (this.lastModified === null) {
var date = new Date();
if (this.pushstream.secondsAgo) { date.setTime(date.getTime() - (this.pushstream.secondsAgo * 1000)); }
this.lastModified = dateToUTCString(date);
this.lastModified = Utils.dateToUTCString(date);
}
if (!this.pushstream.longPollingByHeaders) {
......@@ -757,7 +759,7 @@
var messages = isArray(responseText) ? responseText : responseText.split("\r\n");
for (var i = 0; i < messages.length; i++) {
if (messages[i]) {
lastMessage = parseMessage(messages[i], this.pushstream);
lastMessage = Utils.parseMessage(messages[i], this.pushstream);
this.messagesQueue.push(lastMessage);
if (!this.pushstream.longPollingByHeaders && lastMessage.time) {
this.etag = lastMessage.tag;
......@@ -820,7 +822,7 @@
this.wrappers = [];
this.wrapper = null;
this.onopen = null;
this.onchanneldeleted = null;
this.onmessage = null;
this.onerror = null;
this.onstatuschange = null;
......
......@@ -19,6 +19,12 @@ describe("PushStream", function() {
it("should define status code constants", function() {
expect(PushStream.CLOSED).toBeDefined();
expect(PushStream.CONNECTING).toBeDefined();
expect(PushStream.OPEN).toBeDefined();
});
it("should has a PushStream instances manager", function() {
expect(PushStreamManager).toBeDefined();
expect(PushStreamManager instanceof Array).toBeTruthy();
});
});
......@@ -45,20 +51,84 @@ describe("PushStream", function() {
expect(pushstream.readyState).toBe(PushStream.CLOSED);
});
it("should use '/sub' as url prefix for stream", function() {
expect(pushstream.urlPrefixStream).toBe('/sub');
describe("for operation timeouts", function() {
it("should has a connection timeout", function() {
expect(pushstream.timeout).toBe(15000);
});
it("should has a ping message timeout", function() {
expect(pushstream.pingtimeout).toBe(30000);
});
it("should has a reconnect interval, to be used when a timeout happens", function() {
expect(pushstream.reconnecttimeout).toBe(3000);
});
it("should has a reconnect interval, to be used when a channel is unavailable", function() {
expect(pushstream.checkChannelAvailabilityInterval).toBe(60000);
});
});
it("should use '/ev' as url prefix for event source", function() {
expect(pushstream.urlPrefixEventsource).toBe('/ev');
describe("for url prefix", function() {
it("should use '/pub' for publish message", function() {
expect(pushstream.urlPrefixPublisher).toBe('/pub');
});
it("should use '/sub' for stream", function() {
expect(pushstream.urlPrefixStream).toBe('/sub');
});
it("should use '/ev' for event source", function() {
expect(pushstream.urlPrefixEventsource).toBe('/ev');
});
it("should use '/lp' for long-polling", function() {
expect(pushstream.urlPrefixLongpolling).toBe('/lp');
});
it("should use '/ws' for websocket", function() {
expect(pushstream.urlPrefixWebsocket).toBe('/ws');
});
});
it("should use '/lp' as url prefix for long-polling", function() {
expect(pushstream.urlPrefixLongpolling).toBe('/lp');
describe("for json keys", function() {
it("should has a key for 'id'", function() {
expect(pushstream.jsonIdKey).toBe('id');
});
it("should has a key for 'channel'", function() {
expect(pushstream.jsonChannelKey).toBe('channel');
});
it("should has a key for 'text'", function() {
expect(pushstream.jsonDataKey).toBe('text');
});
it("should has a key for 'tag'", function() {
expect(pushstream.jsonTagKey).toBe('tag');
});
it("should has a key for 'time'", function() {
expect(pushstream.jsonTimeKey).toBe('time');
});
it("should has a key for 'eventid'", function() {
expect(pushstream.jsonEventIdKey).toBe('eventid');
});
});
it("should use '/ws' as url prefix for websocket", function() {
expect(pushstream.urlPrefixWebsocket).toBe('/ws');
describe("for arguments names", function() {
it("should has a argument for 'tag'", function() {
expect(pushstream.longPollingTagArgument).toBe('tag');
});
it("should has a argument for 'time'", function() {
expect(pushstream.longPollingTimeArgument).toBe('time');
});
it("should has a argument for 'channels'", function() {
expect(pushstream.channelsArgument).toBe('channels');
});
});
it("should has all modes availables", function() {
......@@ -66,7 +136,7 @@ describe("PushStream", function() {
});
it("should define callbacks attributes", function() {
expect(pushstream.onopen).toBeDefined();
expect(pushstream.onchanneldeleted).toBeDefined();
expect(pushstream.onmessage).toBeDefined();
expect(pushstream.onerror).toBeDefined();
expect(pushstream.onstatuschange).toBeDefined();
......@@ -147,5 +217,4 @@ describe("PushStream", function() {
});
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment