Commit fc0e7413 authored by Alain Takoudjou's avatar Alain Takoudjou Committed by Romain Courteaud

httpstorage: add option for request timeout with default value to 0

parent 9a18dfde
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
} else { } else {
this._catch_error = false; this._catch_error = false;
} }
// If timeout not set, use 0 for no timeout value
this._timeout = spec.timeout || 0;
} }
HttpStorage.prototype.get = function (id) { HttpStorage.prototype.get = function (id) {
...@@ -17,7 +19,8 @@ ...@@ -17,7 +19,8 @@
.push(function () { .push(function () {
return jIO.util.ajax({ return jIO.util.ajax({
type: 'HEAD', type: 'HEAD',
url: id url: id,
timeout: context._timeout
}); });
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
...@@ -67,7 +70,8 @@ ...@@ -67,7 +70,8 @@
return jIO.util.ajax({ return jIO.util.ajax({
type: 'GET', type: 'GET',
url: id, url: id,
dataType: "blob" dataType: "blob",
timeout: context._timeout
}); });
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
equal(jio.__type, "http"); equal(jio.__type, "http");
deepEqual(jio.__storage._catch_error, false); deepEqual(jio.__storage._catch_error, false);
deepEqual(jio.__storage._timeout, 0);
}); });
test("Storage store catch_error", function () { test("Storage store catch_error", function () {
...@@ -34,8 +35,18 @@ ...@@ -34,8 +35,18 @@
equal(jio.__type, "http"); equal(jio.__type, "http");
deepEqual(jio.__storage._catch_error, true); deepEqual(jio.__storage._catch_error, true);
deepEqual(jio.__storage._timeout, 0);
}); });
test("Storage with timeout", function () {
var jio = jIO.createJIO({
type: "http",
timeout: 1000
});
equal(jio.__type, "http");
deepEqual(jio.__storage._timeout, 1000);
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// httpStorage.get // httpStorage.get
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -333,4 +344,80 @@ ...@@ -333,4 +344,80 @@
}); });
}); });
/////////////////////////////////////////////////////////////////
// httpStorage timeout set
/////////////////////////////////////////////////////////////////
module("httpStorage.timeout", {
setup: function () {
this.server = sinon.fakeServer.create();
this.server.autoRespond = true;
this.server.autoRespondAfter = 5;
this.jio = jIO.createJIO({
type: "http",
timeout: 1000
});
},
teardown: function () {
this.server.restore();
delete this.server;
}
});
test("get document with timeout set", function () {
var id = domain + "/id1/";
this.server.respondWith("HEAD", id, [200, {
"Content-Type": "text/xml-foo"
}, '']);
stop();
expect(1);
this.jio.get(id)
.then(function (result) {
deepEqual(result, {
"Content-Type": "text/xml-foo",
"Status": 200
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// httpStorage request timeout
/////////////////////////////////////////////////////////////////
module("httpStorage.requesttimeout", {
setup: function () {
this.jio = jIO.createJIO({
type: "http",
timeout: 1
});
}
});
test("get document will timeout", function () {
var id = domain + "/id1/";
stop();
expect(3);
this.jio.get(id)
.then(function (result) {
ok(false, result);
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Gateway Timeout");
equal(error.status_code, 504);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob, sinon)); }(jIO, QUnit, Blob, sinon));
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