From eab9b74ff7a46113e01daa8f8ce30cee85a65c33 Mon Sep 17 00:00:00 2001
From: Karishma Chadha <kchadha@media.mit.edu>
Date: Fri, 16 Nov 2018 18:15:04 -0500
Subject: [PATCH] Fix issue where regular cloud variable messages were throwing
 uncaught Json parse errors because of a newline being at the end of the
 message. Add test that fails without the bugfix.

---
 src/lib/cloud-provider.js             |  6 ++++--
 test/unit/util/cloud-provider.test.js | 10 ++++++++++
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/src/lib/cloud-provider.js b/src/lib/cloud-provider.js
index 8116813eb..ce189e60d 100644
--- a/src/lib/cloud-provider.js
+++ b/src/lib/cloud-provider.js
@@ -51,8 +51,10 @@ class CloudProvider {
         log.info(`Received websocket message: ${messageString}`);
         // Multiple commands can be received, newline separated
         messageString.split('\n').forEach(message => {
-            const parsedData = this.parseMessage(JSON.parse(message));
-            this.vm.postIOData('cloud', parsedData);
+            if (message) { // .split can also contain '' in the array it returns
+                const parsedData = this.parseMessage(JSON.parse(message));
+                this.vm.postIOData('cloud', parsedData);
+            }
         });
     }
 
diff --git a/test/unit/util/cloud-provider.test.js b/test/unit/util/cloud-provider.test.js
index c81588dde..4d247d3fd 100644
--- a/test/unit/util/cloud-provider.test.js
+++ b/test/unit/util/cloud-provider.test.js
@@ -70,6 +70,16 @@ describe('CloudProvider', () => {
         expect(vmIOData[0].varUpdate.value).toEqual('value');
     });
 
+    test('onMessage with newline at the end', () => {
+        const msg1 = JSON.stringify({
+            method: 'set',
+            name: 'name1',
+            value: 'value'
+        });
+        cloudProvider.onMessage({data: `${msg1}\n`});
+        expect(vmIOData[0].varUpdate.name).toEqual('name1');
+    });
+
     test('onMessage with multiple commands', () => {
         const msg1 = JSON.stringify({
             method: 'set',
-- 
GitLab