From 5bd0c10e0a04fe147a3cfda56b668987bc9fed39 Mon Sep 17 00:00:00 2001 From: Adam Magaluk Date: Wed, 13 Jan 2016 15:14:55 -0700 Subject: [PATCH 1/3] Updated test title to match actual test --- test/test_event_streams.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_event_streams.js b/test/test_event_streams.js index d974ca3..23e405c 100644 --- a/test/test_event_streams.js +++ b/test/test_event_streams.js @@ -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; From c501e89ca0c7b1570f83f24be047d20efd80a569 Mon Sep 17 00:00:00 2001 From: Adam Magaluk Date: Wed, 13 Jan 2016 16:12:18 -0700 Subject: [PATCH 2/3] Multiplex ws stream returns 405 when invalid type is given during a subscription. Uses updates from zetta-events-stream-protocol --- lib/event_socket.js | 2 +- test/test_event_stream_parser.js | 22 +++++++++++----------- test/test_event_streams.js | 19 +++++++++++++++++++ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/event_socket.js b/lib/event_socket.js index f323dd2..ba3e775 100644 --- a/lib/event_socket.js +++ b/lib/event_socket.js @@ -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 diff --git a/test/test_event_stream_parser.js b/test/test_event_stream_parser.js index a5976f2..8a9383e 100644 --- a/test/test_event_stream_parser.js +++ b/test/test_event_stream_parser.js @@ -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) { diff --git a/test/test_event_streams.js b/test/test_event_streams.js index 23e405c..338f925 100644 --- a/test/test_event_streams.js +++ b/test/test_event_streams.js @@ -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); From f7e12ebbcf0968c9a0c5fc6a3f65bb87d9646e28 Mon Sep 17 00:00:00 2001 From: Adam Magaluk Date: Fri, 15 Jan 2016 11:30:06 -0700 Subject: [PATCH 3/3] Use latest from zetta-events-stream-protocol --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 89e090a..81c3a6e 100644 --- a/package.json +++ b/package.json @@ -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",