Commit 9a18dfde authored by Alain Takoudjou's avatar Alain Takoudjou Committed by Romain Courteaud

jio ajax: allow to set a request timeout value

parent c915f90b
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
* @param {String} [param.dataType=""] The data type to retrieve * @param {String} [param.dataType=""] The data type to retrieve
* @param {String} param.url The url * @param {String} param.url The url
* @param {Any} [param.data] The data to send * @param {Any} [param.data] The data to send
* @param {Number} param.timeout The request timeout value
* @param {Function} [param.beforeSend] A function called just before the * @param {Function} [param.beforeSend] A function called just before the
* send request. The first parameter of this function is the XHR object. * send request. The first parameter of this function is the XHR object.
* @return {Promise} The promise * @return {Promise} The promise
...@@ -72,6 +73,12 @@ ...@@ -72,6 +73,12 @@
} }
} }
} }
if (param.timeout !== undefined && param.timeout !== 0) {
xhr.timeout = param.timeout;
xhr.ontimeout = function () {
return reject(new jIO.util.jIOError("Gateway Timeout", 504));
};
}
if (typeof param.beforeSend === 'function') { if (typeof param.beforeSend === 'function') {
param.beforeSend(xhr); param.beforeSend(xhr);
} }
......
(function (jIO, QUnit) { /*global setTimeout*/
(function (jIO, QUnit, setTimeout) {
"use strict"; "use strict";
var test = QUnit.test, var test = QUnit.test,
equal = QUnit.equal, equal = QUnit.equal,
stop = QUnit.stop,
start = QUnit.start,
expect = QUnit.expect,
module = QUnit.module; module = QUnit.module;
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -29,4 +33,47 @@ ...@@ -29,4 +33,47 @@
}); });
}(jIO, QUnit)); /////////////////////////////////////////////////////////////////
\ No newline at end of file // util.ajax
/////////////////////////////////////////////////////////////////
module("util.ajax", {
setup: function () {
var context = this;
this.jioerror = jIO.util.jIOError;
this.error_spy = {};
function fakejIOError(message, status_code) {
context.error_spy.message = message;
context.error_spy.status_code = status_code;
}
fakejIOError.prototype = new Error();
fakejIOError.prototype.constructor = fakejIOError;
jIO.util.jIOError = fakejIOError;
},
teardown: function () {
jIO.util.jIOError = this.jioerror;
}
});
test("ajax timeout", function () {
var timeout = 1,
context = this;
stop();
expect(2);
jIO.util.ajax({
type: 'GET',
url: "//www.foo/com/bar",
timeout: timeout
});
setTimeout(function () {
start();
equal(context.error_spy.message, "Gateway Timeout");
equal(context.error_spy.status_code, 504);
}, 10);
});
}(jIO, QUnit, setTimeout));
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment