diff --git a/lib/info-receiver.js b/lib/info-receiver.js index afefea5c..d5de7df8 100644 --- a/lib/info-receiver.js +++ b/lib/info-receiver.js @@ -16,13 +16,13 @@ if (process.env.NODE_ENV !== 'production') { debug = require('debug')('sockjs-client:info-receiver'); } -function InfoReceiver(baseUrl, urlInfo) { +function InfoReceiver(baseUrl, urlInfo, timeout) { debug(baseUrl); var self = this; EventEmitter.call(this); setTimeout(function() { - self.doXhr(baseUrl, urlInfo); + self.doXhr(baseUrl, urlInfo, timeout); }, 0); } @@ -47,7 +47,7 @@ InfoReceiver._getReceiver = function(baseUrl, url, urlInfo) { return new InfoAjax(url, XHRFake); }; -InfoReceiver.prototype.doXhr = function(baseUrl, urlInfo) { +InfoReceiver.prototype.doXhr = function(baseUrl, urlInfo, timeout) { var self = this , url = urlUtils.addPath(baseUrl, '/info') ; @@ -55,11 +55,14 @@ InfoReceiver.prototype.doXhr = function(baseUrl, urlInfo) { this.xo = InfoReceiver._getReceiver(baseUrl, url, urlInfo); + if (timeout === undefined) { + timeout = InfoReceiver.timeout + } this.timeoutRef = setTimeout(function() { debug('timeout'); self._cleanup(false); self.emit('finish'); - }, InfoReceiver.timeout); + }, timeout); this.xo.once('finish', function(info, rtt) { debug('finish', info, rtt); diff --git a/lib/main.js b/lib/main.js index 90dc0428..48fe2847 100644 --- a/lib/main.js +++ b/lib/main.js @@ -121,7 +121,7 @@ function SockJS(url, protocols, options) { , sameScheme: urlUtils.isSchemeEqual(this.url, loc.href) }; - this._ir = new InfoReceiver(this.url, this._urlInfo); + this._ir = new InfoReceiver(this.url, this._urlInfo, options.timeout); this._ir.once('finish', this._receiveInfo.bind(this)); } diff --git a/tests/lib/receivers.js b/tests/lib/receivers.js index 823c9698..eef0ffa4 100644 --- a/tests/lib/receivers.js +++ b/tests/lib/receivers.js @@ -5,7 +5,8 @@ var expect = require('expect.js') , XhrReceiver = require('../../lib/transport/receiver/xhr') , XhrFake = require('../../lib/transport/sender/xhr-fake') , utils = require('../../lib/utils/iframe') - ; + , InfoAjax = require('../../lib/info-ajax') + , InfoReceiver = require('../../lib/info-receiver'); describe('Receivers', function () { describe('jsonp', function () { @@ -288,4 +289,43 @@ describe('Receivers', function () { xhr.abort(); }); }); + describe('info', function () { + it('will timeout - default timeout', function (done) { + this.timeout(10000); + InfoReceiver.prototype._getReceiver = function(baseUrl, url) { + return new InfoAjax(url, XhrFake); + }; + + var expectedWasClean = true; + InfoReceiver.prototype._cleanup = function(wasClean) { + expect(wasClean).to.equal(expectedWasClean); + if (expectedWasClean === false) { + // Cleanup was called because of a timeout + done(); + } + expectedWasClean = false; + }; + + new InfoReceiver('test', {}); + }); + + it('will timeout - configured timeout', function (done) { + this.timeout(2000); + InfoReceiver.prototype._getReceiver = function(baseUrl, url) { + return new InfoAjax(url, XhrFake); + }; + + var expectedWasClean = true; + InfoReceiver.prototype._cleanup = function(wasClean) { + expect(wasClean).to.equal(expectedWasClean); + if (expectedWasClean === false) { + // Cleanup was called because of a timeout + done(); + } + expectedWasClean = false; + }; + + new InfoReceiver('test', {}, 1000); + }); + }); });