[Pkg-javascript-commits] [sockjs-client] 104/350: Add some tests for xhr receiver

tonnerre at ancient-solutions.com tonnerre at ancient-solutions.com
Fri Aug 5 01:03:48 UTC 2016


This is an automated email from the git hooks/post-receive script.

tonnerre-guest pushed a commit to branch upstream
in repository sockjs-client.

commit 6ca6f26f1973141310a6c847fe82a05a1dbcec80
Author: Bryce Kahle <bkahle at gmail.com>
Date:   Thu Oct 9 21:07:54 2014 -0400

    Add some tests for xhr receiver
---
 .eslintrc                        |  5 +++-
 lib/transport/receiver/xhr.js    | 35 +++++++++++++-------------
 lib/transport/sender/xhr-fake.js | 10 ++++++--
 tests/receivers.js               | 53 ++++++++++++++++++++++++++++++++++++++--
 4 files changed, 80 insertions(+), 23 deletions(-)

diff --git a/.eslintrc b/.eslintrc
index f7c6371..d4a02a7 100644
--- a/.eslintrc
+++ b/.eslintrc
@@ -13,6 +13,9 @@
     "XDomainRequest": true,
     "XMLHttpRequest": true,
     "EventSource": true,
-    "ActiveXObject": true
+    "ActiveXObject": true,
+    "describe": true,
+    "before": true,
+    "it": true
   }
 }
diff --git a/lib/transport/receiver/xhr.js b/lib/transport/receiver/xhr.js
index 44f3049..3f3afb8 100644
--- a/lib/transport/receiver/xhr.js
+++ b/lib/transport/receiver/xhr.js
@@ -8,27 +8,11 @@ var util = require('util')
 function XhrReceiver(url, AjaxObject) {
   EventTarget.call(this);
   var self = this;
-  var bufPos = 0;
 
   this.xo = new AjaxObject('POST', url, null);
-  function chunkHandler(status, text) {
-    if (status !== 200) {
-      return;
-    }
-    for(;;) {
-      var buf = text.slice(bufPos);
-      var p = buf.indexOf('\n');
-      if (p === -1) {
-        break;
-      }
-      bufPos += p + 1;
-      var msg = buf.slice(0, p);
-      self.dispatchEvent(new SimpleEvent('message', {data: msg}));
-    }
-  }
-  this.xo.on('chunk', chunkHandler);
+  this.xo.on('chunk', this._chunkHandler.bind(this));
   this.xo.on('finish', function(status, text) {
-    chunkHandler(status, text);
+    self._chunkHandler(status, text);
     self.xo = null;
     var reason = status === 200 ? 'network' : 'permanent';
     self.dispatchEvent(new SimpleEvent('close', {reason: reason}));
@@ -37,6 +21,21 @@ function XhrReceiver(url, AjaxObject) {
 
 util.inherits(XhrReceiver, EventTarget);
 
+XhrReceiver.prototype._chunkHandler = function (status, text) {
+  if (status !== 200 || !text) {
+    return;
+  }
+
+  var messages = text.split('\n');
+  for (var i = 0; i < messages.length; i++) {
+    var msg = messages[i];
+    if (!msg) {
+      continue;
+    }
+    this.dispatchEvent(new SimpleEvent('message', {data: msg}));
+  }
+};
+
 XhrReceiver.prototype.abort = function() {
   if (this.xo) {
     this.xo.close();
diff --git a/lib/transport/sender/xhr-fake.js b/lib/transport/sender/xhr-fake.js
index beabdfc..6d32462 100644
--- a/lib/transport/sender/xhr-fake.js
+++ b/lib/transport/sender/xhr-fake.js
@@ -8,11 +8,17 @@ function XHRFake(method, url, payload, opts) {
   var self = this;
   EventEmitter.call(this);
 
-  setTimeout(function() {
+  this.to = setTimeout(function() {
     self.emit('finish', 200, '{}');
-  }, 2000);
+  }, XHRFake.timeout);
 }
 
 util.inherits(XHRFake, EventEmitter);
 
+XHRFake.prototype.close = function () {
+  clearTimeout(this.to);
+};
+
+XHRFake.timeout = 2000;
+
 module.exports = XHRFake;
diff --git a/tests/receivers.js b/tests/receivers.js
index d501cc6..09a2457 100644
--- a/tests/receivers.js
+++ b/tests/receivers.js
@@ -2,6 +2,8 @@
 
 var expect = require('expect.js')
   , JsonpReceiver = require('../lib/transport/receiver/jsonp')
+  , XhrReceiver = require('../lib/transport/receiver/xhr')
+  , XhrFake = require('../lib/transport/sender/xhr-fake')
   , utils = require('../lib/utils/iframe')
   ;
 
@@ -48,7 +50,7 @@ describe('Receivers', function () {
       };
     });
 
-    it('will abort without sending a message', function (done) {
+    it('aborts without sending a message', function (done) {
       JsonpReceiver.prototype._createScript = function () {
         var self = this;
         setTimeout(function () {
@@ -112,8 +114,55 @@ describe('Receivers', function () {
 
       // simulate script error
       setTimeout(function () {
-        jpr._scriptError();  
+        jpr._scriptError();
       }, 150);
     });
   });
+
+  describe('xhr', function () {
+    before(function () {
+      XhrFake.timeout = 100;
+    });
+
+    it('emits multiple messages for multi-line response', function (done) {
+      var xhr = new XhrReceiver('test', XhrFake);
+      var i = 0, responses = ['test', 'multiple', 'lines', '{}'];
+      xhr.onmessage = function (e) {
+        expect(e.data).to.be.eql(responses[i]);
+        i++;
+      };
+      xhr.onclose = function (e) {
+        expect(e.reason).to.be.eql('network');
+        done();
+      };
+      xhr._chunkHandler(200, 'test\nmultiple\nlines');
+    });
+
+    it('emits no messages for an empty string response', function (done) {
+      var xhr = new XhrReceiver('test', XhrFake);
+      var i = 0, responses = ['{}'];
+      xhr.onmessage = function (e) {
+        expect(i).to.be.lessThan(responses.length);
+        expect(e.data).to.be.eql(responses[i]);
+        i++;
+      };
+      xhr.onclose = function (e) {
+        expect(e.reason).to.be.eql('network');
+        done();
+      };
+      xhr._chunkHandler(200, '');
+    });
+
+    it('aborts without sending a message', function (done) {
+      var xhr = new XhrReceiver('test', XhrFake);
+      xhr.onmessage = function () {
+        expect().fail();
+      };
+      xhr.onclose = function (e) {
+        expect(e.reason).to.be.eql('user');
+        done();
+      };
+      xhr.abort();
+    });
+  });
 });

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/sockjs-client.git



More information about the Pkg-javascript-commits mailing list