diff --git a/spec/realtime/message.test.js b/spec/realtime/message.test.js index 196d8a5a8f..ab6fb68346 100644 --- a/spec/realtime/message.test.js +++ b/spec/realtime/message.test.js @@ -66,6 +66,113 @@ define(['ably', 'shared_helper'], function(Ably, helper) { } }; + exports.publishVariations = function(test) { + var transport = 'binary'; + + test.expect(32); + try { + /* set up realtime */ + var realtime = helper.AblyRealtime(); + var rest = helper.AblyRest(); + + /* connect and attach */ + realtime.connection.on('connected', function() { + var rtChannel = realtime.channels.get('publishVariations'); + var testData = 'Some data' + var errorCallback = function(err){ + if(err) { + test.ok(false, 'Error received by publish callback ' + err); + test.done(); + realtime.close(); + return; + } + } + rtChannel.attach(function(err) { + if(err) { + test.ok(false, 'Attach failed with error: ' + err); + test.done(); + realtime.close(); + return; + } + + /* subscribe to different message types */ + var messagesReceived = 0 + rtChannel.subscribe(function(msg) { + test.ok(true, 'Received ' + msg.name); + ++messagesReceived; + switch(msg.name) { + case 'objectWithName': + case 'objectWithNameAndCallback': + case 'objectWithNameAndNullData': + case 'objectWithNameAndUndefinedData': + case 'nameAndNullData': + case 'nameAndUndefinedData': + test.equal(typeof(msg.data), 'undefined', 'Msg data was received where none expected'); + break; + case 'nameAndData': + case 'nameAndDataAndCallback': + case 'objectWithNameAndData': + case 'objectWithNameAndDataAndCallback': + test.equal(msg.data, testData, 'Msg data ' + msg.data + 'Unexpected message data received'); + break; + case undefined: + if (msg.data) { + // 3 messages: null name and data, null name and data and callback, object with null name and data + test.equal(msg.data, testData, 'Msg data ' + msg.data + 'Unexpected message data received'); + } else { + // 3 messages: null name and null data, object with null name and no data, object with null name and null data + test.equal(typeof(msg.data), 'undefined', 'Msg data was received where none expected'); + } + break; + default: + test.ok(false, 'Unexpected message ' + msg.name + 'received'); + test.done(); + realtime.close(); + } + + if (messagesReceived == 16) { + test.done(); + realtime.close(); + } + }); + + /* publish events */ + var restChannel = rest.channels.get('publishVariations'); + restChannel.publish({name: 'objectWithName'}); + restChannel.publish({name: 'objectWithNameAndCallback'}, errorCallback); + restChannel.publish({name: 'objectWithNameAndNullData', data: null}); + restChannel.publish({name: 'objectWithNameAndUndefinedData', data: undefined}); + restChannel.publish('nameAndNullData', null); + restChannel.publish('nameAndUndefinedData', undefined); + restChannel.publish('nameAndData', testData); + restChannel.publish('nameAndDataAndCallback', testData, errorCallback); + restChannel.publish({name: 'objectWithNameAndData', data: testData}); + restChannel.publish({name: 'objectWithNameAndDataAndCallback', data: testData}, errorCallback); + // 6 messages with null name: + restChannel.publish(null, testData); + restChannel.publish(null, testData, errorCallback); + restChannel.publish({name: null, data: testData}); + restChannel.publish(null, null); + restChannel.publish({name: null}); + restChannel.publish({name: null, data: null}); + }); + }); + var exitOnState = function(state) { + realtime.connection.on(state, function () { + test.ok(false, transport + ' connection to server failed'); + test.done(); + realtime.close(); + }); + }; + exitOnState('failed'); + exitOnState('suspended'); + } catch(e) { + test.ok(false, 'Channel attach failed with exception: ' + e.stack); + test.done(); + realtime.close(); + } + }; + exports.restpublish = function(test) { var count = 10; var rest = helper.AblyRest(); diff --git a/spec/realtime/presence.test.js b/spec/realtime/presence.test.js index d52f2dc765..f0257f3196 100644 --- a/spec/realtime/presence.test.js +++ b/spec/realtime/presence.test.js @@ -100,7 +100,7 @@ define(['ably', 'shared_helper', 'async'], function(Ably, helper, async) { }; /* - * Attach to channel, enter presence channel and await entered event + * Attach to channel, enter presence channel with data and await entered event */ exports.enter0 = function(test) { var clientRealtime; @@ -343,6 +343,178 @@ define(['ably', 'shared_helper', 'async'], function(Ably, helper, async) { } }; + /* + * Attach to channel, enter presence channel with a callback but no data and await entered event + */ + exports.enter4 = function(test) { + var clientRealtime; + var isDone = false, done = function() { + if(!isDone) { + isDone = true; + setTimeout(function() { + test.done(); clientRealtime.close(); + }, 3000); + } + }; + try { + var transport = 'web_socket'; + + test.expect(2); + /* listen for the enter event, test is complete when received */ + var presenceHandler = function() { + if(this.event == 'enter') { + test.ok(true, 'Presence event received'); + done(); + presenceChannel.presence.off(presenceHandler); + } + }; + presenceChannel.presence.on(presenceHandler); + + /* set up authenticated connection */ + clientRealtime = helper.AblyRealtime({ clientId: testClientId, authToken: authToken, transports: [transport] }); + clientRealtime.connection.on('connected', function() { + /* get channel, attach, and enter */ + var clientChannel = clientRealtime.channels.get('presence0'); + clientChannel.attach(function(err) { + if(err) { + test.ok(false, 'Attach failed with error: ' + err); + done(); + return; + } + clientChannel.presence.enter(function(err) { + if(err) { + test.ok(false, 'Enter failed with error: ' + err); + done(); + return; + } + test.ok(true, 'Presence event sent'); + }); + }); + }); + var exitOnState = function(state) { + clientRealtime.connection.on(state, function () { + test.ok(false, transport + ' connection to server failed'); + done(); + }); + }; + exitOnState('failed'); + exitOnState('suspended'); + } catch(e) { + test.ok(false, 'presence.enter4 failed with exception: ' + e.stack); + done(); + } + }; + + /* + * Attach to channel, enter presence channel with neither callback nor data and await entered event + */ + exports.enter5 = function(test) { + var clientRealtime; + var isDone = false, done = function() { + if(!isDone) { + isDone = true; + setTimeout(function() { + test.done(); clientRealtime.close(); + }, 3000); + } + }; + try { + var transport = 'web_socket'; + + test.expect(1); + /* listen for the enter event, test is complete when received */ + var presenceHandler = function() { + if(this.event == 'enter') { + test.ok(true, 'Presence event received'); + done(); + presenceChannel.presence.off(presenceHandler); + } + }; + presenceChannel.presence.on(presenceHandler); + + /* set up authenticated connection */ + clientRealtime = helper.AblyRealtime({ clientId: testClientId, authToken: authToken, transports: [transport] }); + clientRealtime.connection.on('connected', function() { + /* get channel, attach, and enter */ + var clientChannel = clientRealtime.channels.get('presence0'); + clientChannel.attach(function(err) { + if(err) { + test.ok(false, 'Attach failed with error: ' + err); + done(); + return; + } + clientChannel.presence.enter(); + }); + }); + var exitOnState = function(state) { + clientRealtime.connection.on(state, function () { + test.ok(false, transport + ' connection to server failed'); + done(); + }); + }; + exitOnState('failed'); + exitOnState('suspended'); + } catch(e) { + test.ok(false, 'presence.enter5 failed with exception: ' + e.stack); + done(); + } + }; + + /* + * Attach to channel, enter presence channel with data but no callback and await entered event + */ + exports.enter6 = function(test) { + var clientRealtime; + var isDone = false, done = function() { + if(!isDone) { + isDone = true; + setTimeout(function() { + test.done(); clientRealtime.close(); + }, 3000); + } + }; + try { + var transport = 'web_socket'; + + test.expect(1); + /* listen for the enter event, test is complete when received */ + var presenceHandler = function() { + if(this.event == 'enter') { + test.ok(true, 'Presence event received'); + done(); + presenceChannel.presence.off(presenceHandler); + } + }; + presenceChannel.presence.on(presenceHandler); + + /* set up authenticated connection */ + clientRealtime = helper.AblyRealtime({ clientId: testClientId, authToken: authToken, transports: [transport] }); + clientRealtime.connection.on('connected', function() { + /* get channel, attach, and enter */ + var clientChannel = clientRealtime.channels.get('presence0'); + clientChannel.attach(function(err) { + if(err) { + test.ok(false, 'Attach failed with error: ' + err); + done(); + return; + } + clientChannel.presence.enter('Test client data (enter6)'); + }); + }); + var exitOnState = function(state) { + clientRealtime.connection.on(state, function () { + test.ok(false, transport + ' connection to server failed'); + done(); + }); + }; + exitOnState('failed'); + exitOnState('suspended'); + } catch(e) { + test.ok(false, 'presence.enter6 failed with exception: ' + e.stack); + done(); + } + }; + /* * Attach to channel, enter+leave presence channel and await leave event */