Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw 411 error for unsupported data types #53

Merged
merged 5 commits into from
May 22, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions browser/lib/util/bufferutils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ var BufferUtils = (function() {
var ArrayBuffer = window.ArrayBuffer;
var TextDecoder = window.TextDecoder;

function isWordArray(ob) { return ob.sigBytes !== undefined; }
function isArrayBuffer(ob) { return ob.constructor === ArrayBuffer; }
function isWordArray(ob) { return ob !== null && ob !== undefined && ob.sigBytes !== undefined; }
function isArrayBuffer(ob) { return ob !== null && ob !== undefined && ob.constructor === ArrayBuffer; }

// https://gist.githubusercontent.com/jonleighton/958841/raw/f200e30dfe95212c0165ccf1ae000ca51e9de803/gistfile1.js
function arrayBufferToBase64(ArrayBuffer) {
Expand Down
25 changes: 19 additions & 6 deletions browser/static/ably.js
Original file line number Diff line number Diff line change
Expand Up @@ -2505,8 +2505,8 @@ var BufferUtils = (function() {
var ArrayBuffer = window.ArrayBuffer;
var TextDecoder = window.TextDecoder;

function isWordArray(ob) { return ob.sigBytes !== undefined; }
function isArrayBuffer(ob) { return ob.constructor === ArrayBuffer; }
function isWordArray(ob) { return ob !== null && ob !== undefined && ob.sigBytes !== undefined; }
function isArrayBuffer(ob) { return ob !== null && ob !== undefined && ob.constructor === ArrayBuffer; }

// https://gist.githubusercontent.com/jonleighton/958841/raw/f200e30dfe95212c0165ccf1ae000ca51e9de803/gistfile1.js
function arrayBufferToBase64(ArrayBuffer) {
Expand Down Expand Up @@ -2626,6 +2626,7 @@ var BufferUtils = (function() {

return BufferUtils;
})();

var Cookie = (function() {
var isBrowser = (typeof(window) == 'object');
function noop() {}
Expand Down Expand Up @@ -4143,6 +4144,11 @@ var Utils = (function() {
return Object.prototype.toString.call(ob) == '[object Array]';
};

/* ...Or an Object (in the narrow sense) */
Utils.isObject = function(ob) {
return Object.prototype.toString.call(ob) == '[object Object]';
};

/*
* Determine whether or not an object contains
* any enumerable properties.
Expand Down Expand Up @@ -4477,11 +4483,18 @@ var Message = (function() {
};

Message.encode = function(msg, options) {
var data = msg.data, encoding;
if(data !== null && data !== undefined && typeof(data) != 'string' && !BufferUtils.isBuffer(data)) {
msg.data = JSON.stringify(data);
msg.encoding = (encoding = msg.encoding) ? (encoding + '/json') : 'json';
var data = msg.data, encoding,
nativeDataType = typeof(data) == 'string' || BufferUtils.isBuffer(data) || data === null || data === undefined;

if (!nativeDataType) {
if (Utils.isObject(data) || Utils.isArray(data)) {
msg.data = JSON.stringify(data);
msg.encoding = (encoding = msg.encoding) ? (encoding + '/json') : 'json';
} else {
throw new ErrorInfo('Data type is unsupported', 40011, 400);
}
}

if(options != null && options.encrypted)
Message.encrypt(msg, options);
};
Expand Down
25 changes: 19 additions & 6 deletions browser/static/ably.noencryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -1157,8 +1157,8 @@ var BufferUtils = (function() {
var ArrayBuffer = window.ArrayBuffer;
var TextDecoder = window.TextDecoder;

function isWordArray(ob) { return ob.sigBytes !== undefined; }
function isArrayBuffer(ob) { return ob.constructor === ArrayBuffer; }
function isWordArray(ob) { return ob !== null && ob !== undefined && ob.sigBytes !== undefined; }
function isArrayBuffer(ob) { return ob !== null && ob !== undefined && ob.constructor === ArrayBuffer; }

// https://gist.githubusercontent.com/jonleighton/958841/raw/f200e30dfe95212c0165ccf1ae000ca51e9de803/gistfile1.js
function arrayBufferToBase64(ArrayBuffer) {
Expand Down Expand Up @@ -1278,6 +1278,7 @@ var BufferUtils = (function() {

return BufferUtils;
})();

var Cookie = (function() {
var isBrowser = (typeof(window) == 'object');
function noop() {}
Expand Down Expand Up @@ -2795,6 +2796,11 @@ var Utils = (function() {
return Object.prototype.toString.call(ob) == '[object Array]';
};

/* ...Or an Object (in the narrow sense) */
Utils.isObject = function(ob) {
return Object.prototype.toString.call(ob) == '[object Object]';
};

/*
* Determine whether or not an object contains
* any enumerable properties.
Expand Down Expand Up @@ -3129,11 +3135,18 @@ var Message = (function() {
};

Message.encode = function(msg, options) {
var data = msg.data, encoding;
if(data !== null && data !== undefined && typeof(data) != 'string' && !BufferUtils.isBuffer(data)) {
msg.data = JSON.stringify(data);
msg.encoding = (encoding = msg.encoding) ? (encoding + '/json') : 'json';
var data = msg.data, encoding,
nativeDataType = typeof(data) == 'string' || BufferUtils.isBuffer(data) || data === null || data === undefined;

if (!nativeDataType) {
if (Utils.isObject(data) || Utils.isArray(data)) {
msg.data = JSON.stringify(data);
msg.encoding = (encoding = msg.encoding) ? (encoding + '/json') : 'json';
} else {
throw new ErrorInfo('Data type is unsupported', 40011, 400);
}
}

if(options != null && options.encrypted)
Message.encrypt(msg, options);
};
Expand Down
5 changes: 5 additions & 0 deletions browser/static/compat-pusher.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ var Utils = (function() {
return Object.prototype.toString.call(ob) == '[object Array]';
};

/* ...Or an Object (in the narrow sense) */
Utils.isObject = function(ob) {
return Object.prototype.toString.call(ob) == '[object Object]';
};

/*
* Determine whether or not an object contains
* any enumerable properties.
Expand Down
5 changes: 5 additions & 0 deletions browser/static/iframe-0.8.0.html
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@
return Object.prototype.toString.call(ob) == '[object Array]';
};

/* ...Or an Object (in the narrow sense) */
Utils.isObject = function(ob) {
return Object.prototype.toString.call(ob) == '[object Object]';
};

/*
* Determine whether or not an object contains
* any enumerable properties.
Expand Down
5 changes: 5 additions & 0 deletions browser/static/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ var Utils = (function() {
return Object.prototype.toString.call(ob) == '[object Array]';
};

/* ...Or an Object (in the narrow sense) */
Utils.isObject = function(ob) {
return Object.prototype.toString.call(ob) == '[object Object]';
};

/*
* Determine whether or not an object contains
* any enumerable properties.
Expand Down
15 changes: 11 additions & 4 deletions common/lib/types/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,18 @@ var Message = (function() {
};

Message.encode = function(msg, options) {
var data = msg.data, encoding;
if(data !== null && data !== undefined && typeof(data) != 'string' && !BufferUtils.isBuffer(data)) {
msg.data = JSON.stringify(data);
msg.encoding = (encoding = msg.encoding) ? (encoding + '/json') : 'json';
var data = msg.data, encoding,
nativeDataType = typeof(data) == 'string' || BufferUtils.isBuffer(data) || data === null || data === undefined;

if (!nativeDataType) {
if (Utils.isObject(data) || Utils.isArray(data)) {
msg.data = JSON.stringify(data);
msg.encoding = (encoding = msg.encoding) ? (encoding + '/json') : 'json';
} else {
throw new ErrorInfo('Data type is unsupported', 40011, 400);
}
}

if(options != null && options.encrypted)
Message.encrypt(msg, options);
};
Expand Down
5 changes: 5 additions & 0 deletions common/lib/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ var Utils = (function() {
return Object.prototype.toString.call(ob) == '[object Array]';
};

/* ...Or an Object (in the narrow sense) */
Utils.isObject = function(ob) {
return Object.prototype.toString.call(ob) == '[object Object]';
};

/*
* Determine whether or not an object contains
* any enumerable properties.
Expand Down
62 changes: 60 additions & 2 deletions spec/realtime/message.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
[{name: 'objectWithNameAndNullData', data: null}],
[{name: 'objectWithNameAndUndefinedData', data: undefined}],
[{name: 'objectWithNameAndEmptyStringData', data: ''}],
[{name: 'objectWithNameAndFalseData', data: false}],
['nameAndNullData', null],
['nameAndUndefinedData', undefined],
['nameAndEmptyStringData', ''],
['nameAndFalseData', false],
['nameAndData', testData],
['nameAndDataAndCallback', testData, errorCallback],
[{name: 'objectWithNameAndData', data: testData}],
Expand Down Expand Up @@ -190,6 +188,66 @@ define(['ably', 'shared_helper'], function(Ably, helper) {
}
};

exports.publishDisallowed = function(test) {
var transport = 'binary';
var testData = 'Some data'
var testArguments = [
[{name: 'objectAndBoolData', data: false}],
['nameAndBoolData', false],
[{name: 'objectAndNumericData', data: 0}],
['nameAndNumericData', 0],
[{name: 'objectAndOtherObjectData', data: new Date()}],
['nameAndOtherObjectData', new Date()],
];

test.expect(testArguments.length * 2);
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('publishDisallowed');
rtChannel.attach(function(err) {
if(err) {
test.ok(false, 'Attach failed with error: ' + err);
test.done();
realtime.close();
return;
}

/* publish events */
var restChannel = rest.channels.get('publishDisallowed');
for(var i = 0; i < testArguments.length; i++) {
try {
restChannel.publish.apply(restChannel, testArguments[i]);
test.ok(false, "Exception was not raised");
} catch (e) {
test.ok(true, "Exception correctly raised");
test.equal(e.code, 40011, "Invalid data type exception raised");
}
}
test.done();
realtime.close();
});
});
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();
Expand Down
Loading