From 4c9ce6c8cf6c75a854479db82ebd21bf7c7bede4 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Sun, 25 Jan 2015 13:13:07 -0600 Subject: [PATCH 1/3] added request.setTimeout support --- lib/request.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/request.js b/lib/request.js index 7b8b733..6f420c0 100644 --- a/lib/request.js +++ b/lib/request.js @@ -154,6 +154,24 @@ Request.prototype.end = function (s) { } }; +Request.prototype.setTimeout = function(msecs, callback) { + if (callback) this.once('timeout', callback); + + var self = this; + function emitTimeout() { + self.emit('timeout'); + } + + // set and handle the timeout signals on xhr + if (this.xhr) { + if (this.timeoutCb) { + this.xhr.ontimeout = this.timeoutCb; + } + this.timeoutCb = emitTimeout; + this.xhr.timeout = msecs; + } +}; + // Taken from http://dxr.mozilla.org/mozilla/mozilla-central/content/base/src/nsXMLHttpRequest.cpp.html Request.unsafeHeaders = [ "accept-charset", From fa18e1f5ac707cbe3644abec2404225dedc90820 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Mon, 26 Jan 2015 21:45:11 -0600 Subject: [PATCH 2/3] code simplifications --- lib/request.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/lib/request.js b/lib/request.js index 6f420c0..655dc12 100644 --- a/lib/request.js +++ b/lib/request.js @@ -156,20 +156,8 @@ Request.prototype.end = function (s) { Request.prototype.setTimeout = function(msecs, callback) { if (callback) this.once('timeout', callback); - - var self = this; - function emitTimeout() { - self.emit('timeout'); - } - - // set and handle the timeout signals on xhr - if (this.xhr) { - if (this.timeoutCb) { - this.xhr.ontimeout = this.timeoutCb; - } - this.timeoutCb = emitTimeout; - this.xhr.timeout = msecs; - } + this.xhr.ontimeout = this.emit.bind(this, 'timeout'); + this.xhr.timeout = msecs; }; // Taken from http://dxr.mozilla.org/mozilla/mozilla-central/content/base/src/nsXMLHttpRequest.cpp.html From 626f305465f1630b70da0853642c18d62efe3564 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Tue, 27 Jan 2015 19:30:33 -0600 Subject: [PATCH 3/3] tests around request setTimeout --- test/request_url.js | 52 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/request_url.js b/test/request_url.js index 1f58f2d..60c6f2a 100644 --- a/test/request_url.js +++ b/test/request_url.js @@ -112,3 +112,55 @@ test('Test POST XHR2 types', function(t) { }; request.end(new global.FormData()); }); + +test('Test setTimeout sets xhr timeout and ontimeout', function(t) { + var url = '/api/foo'; + var request = http.request({ url: url, method: 'POST' }, noop); + + request.setTimeout( 999, function(){}); + + t.equal( request.xhr.timeout, 999); + t.ok( request.xhr.ontimeout, 'Make sure ontimeout is a function'); + t.end(); +}); + + +test('Test setTimeout will execute callback after timeout', function(t) { + var url = '/api/foo'; + var request = http.request({ url: url, method: 'POST' }, noop); + var abortSend; + + t.plan(1); + + // send query handler function + var onSend = function (data) { + t.fail('setTimeout should have cancelled the request'); + }; + + // timeout callback function + var onTimeout = function(){ + clearTimeout( abortSend); + t.pass('timeout called'); + }; + + // simulate a slow-executing query + request.xhr.send = function( data) { + + // execute the query after a delay + abortSend = setTimeout( + onSend.bind( this, data), 1000 + ); + + // simulate xhr ontimeout, normally this + // gets done somewhere in the browser + if (this.timeout && this.ontimeout) { + setTimeout( this.ontimeout, this.timeout); + } + } + + // setup a timeout that will abort before query fires + request.setTimeout( 200, onTimeout); + request.end(); +}); + +