Skip to content

Commit

Permalink
Merge pull request #281 from zettajs/multiplexed-ws-protocol-errors
Browse files Browse the repository at this point in the history
Multiplexed ws protocol errors
  • Loading branch information
AdamMagaluk committed Jan 15, 2016
2 parents c634e90 + f7e12eb commit 4217a12
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/event_socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var EventSocket = module.exports = function(ws, query, streamEnabled) {
this._parser.on('error', function(err, original) {
var msg = {
type: 'error',
code: 400,
code: (err.name === 'InvalidTypeError') ? 405 : 400,
timestamp: new Date().getTime(),
topic: (typeof original === 'object') ? original.topic : null,
message: err.message
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"ws": "^0.4.31",
"zetta-auto-scout": "^0.9.0",
"zetta-device": "^0.17.0",
"zetta-events-stream-protocol": "^4.0.0",
"zetta-events-stream-protocol": "^5.0.0",
"zetta-http-device": "^0.4.0",
"zetta-rels": "^0.5.0",
"zetta-scientist": "^0.5.0",
Expand Down
22 changes: 11 additions & 11 deletions test/test_event_stream_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,67 @@ describe('Event Stream Parser', function() {
it('validates subscribe messages correctly', function() {
var message = { type: 'subscribe', topic: 'Detroit/led/1234/state' };
var parser = new EventStreamParser();
assert(parser.validate(message));
assert(parser.validate(message) === true);
});

it('invalidates subscribe messages correctly', function() {
var message = { type: 'subscribe'};
var parser = new EventStreamParser();
assert(!parser.validate(message));
assert(parser.validate(message) !== true);
});

it('validates unsubscribe messages correctly', function() {
var message = { type: 'unsubscribe', subscriptionId: 1 };
var parser = new EventStreamParser();
assert(parser.validate(message));
assert(parser.validate(message) === true);
});

it('invalidates unsubscribe messages correctly', function() {
var message = { type: 'unsubscribe' };
var parser = new EventStreamParser();
assert(!parser.validate(message));
assert(parser.validate(message) !== true);
});

it('validates unsubscribe-ack messages correctly', function() {
var message = { type: 'unsubscribe-ack', subscriptionId: 1, timestamp: 1 };
var parser = new EventStreamParser();
assert(parser.validate(message));
assert(parser.validate(message) === true);
});

it('invalidates unsubscribe-ack messages correctly no subscriptionId', function() {
var message = { type: 'unsubscribe-ack', timestamp: 1 };
var parser = new EventStreamParser();
assert(!parser.validate(message));
assert(parser.validate(message) !== true);
});

it('invalidates unsubscribe-ack messages correctly no timestamp no subscriptionId', function() {
var message = { type: 'unsubscribe-ack' };
var parser = new EventStreamParser();
assert(!parser.validate(message));
assert(parser.validate(message) !== true);
});

it('invalidates unsubscribe-ack messages correctly no timestamp', function() {
var message = { type: 'unsubscribe-ack', timestamp: 1 };
var parser = new EventStreamParser();
assert(!parser.validate(message));
assert(parser.validate(message) !== true);
});

it('validates subscribe-ack messages correctly', function() {
var message = { type: 'unsubscribe-ack', timestamp: 1, topic: 'Detroit/led/1234/state', subscriptionId: 1};
var parser = new EventStreamParser();
assert(parser.validate(message));
assert(parser.validate(message) === true);
});

it('validates error messages correctly', function() {
var message = { type: 'error', code: 1, timestamp: 1, topic: 'Detroit/led/1234/state' };
var parser = new EventStreamParser();
assert(parser.validate(message));
assert(parser.validate(message) === true);
});

it('validates event messages correctly', function() {
var message = { type: 'event', timestamp: 1, topic: 'Detroit/led/1234/state', subscriptionId: 1 };
var parser = new EventStreamParser();
assert(parser.validate(message));
assert(parser.validate(message) === true);
});

it('should emit event for message type when parsing buffer', function(done) {
Expand Down
21 changes: 20 additions & 1 deletion test/test_event_streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -1412,7 +1412,7 @@ describe('Event Streams', function() {
ws.on('error', done);
});

itBoth('unsubscribing from an invalid subscriptionId should result in a 400 error', function(idx, done){
itBoth('unsubscribing from an invalid subscriptionId should result in a 405 error', function(idx, done){
var endpoint = urls[idx];
var ws = new WebSocket('ws://' + endpoint + baseUrl);
var subscriptionId = null;
Expand All @@ -1431,6 +1431,25 @@ describe('Event Streams', function() {
ws.on('error', done);
});

itBoth('invalid type should result in a 405 error', function(idx, done){
var endpoint = urls[idx];
var ws = new WebSocket('ws://' + endpoint + baseUrl);
var subscriptionId = null;
var count = 0;
ws.on('open', function() {
var msg = { type: 'not-a-type', topic: '**' };
ws.send(JSON.stringify(msg));
ws.on('message', function(buffer) {
var json = JSON.parse(buffer);
assert(json.timestamp);
assert.equal(json.code, 405);
assert(json.message);
done();
});
});
ws.on('error', done);
});

itBoth('unsubscribing from a missing subscriptionId should result in a 400 error', function(idx, done){
var endpoint = urls[idx];
var ws = new WebSocket('ws://' + endpoint + baseUrl);
Expand Down

0 comments on commit 4217a12

Please sign in to comment.