Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reject request on XHR timeout #2581

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions request/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ module.exports = function($window, Promise, oncompletion) {
}
}

xhr.ontimeout = function (ev) {
if (aborted) return;
var error = new Error("Request timeout");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's be a little clearer on the error message

Suggested change
var error = new Error("Request timeout");
var error = new Error("Request timed timeout");

error.timeout = ev.target.timeout;
reject(error);
}
Comment on lines +158 to +163
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you verified the onreadystatechange callback isn't also executed? If it is, you'll need to set aborted to true to keep anything else from firing.


if (typeof args.config === "function") {
xhr = args.config(xhr, args, url) || xhr

Expand Down
32 changes: 30 additions & 2 deletions request/tests/test-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ o.spec("request", function() {
o("works via GET", function(done) {
mock.$defineRoutes({
"GET /item": function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
return new Promise(function (resolve) {
resolve({status: 200, responseText: JSON.stringify({a: 1})})
})
Comment on lines +21 to +23
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this.

}
})
request({method: "GET", url: "/item"}).then(function(data) {
Expand Down Expand Up @@ -694,7 +696,9 @@ o.spec("request", function() {
o("rejects on error in extract", function(done) {
mock.$defineRoutes({
"GET /item": function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
return new Promise(function (resolve) {
resolve({status: 200, responseText: JSON.stringify({a: 1})})
})
Comment on lines +699 to +701
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert this.

}
})
request({
Expand All @@ -707,6 +711,30 @@ o.spec("request", function() {
done()
})
})
o("rejects on timeout", function(done) {
var timeout = 50
var gotTimeoutError = false
mock.$defineRoutes({
"GET /item": function() {
return new Promise(function (resolve) {
setTimeout(function () {
resolve({status: 200, responseText: JSON.stringify({a: 1})})
}, timeout*2)
})
}
})
request(
{method: "GET", url: "/item", timeout: timeout}
).catch(function(e) {
gotTimeoutError = true
o(e instanceof Error).equals(true)
o(e.message).equals("Request timeout")
o(e.timeout).equals(timeout)
}).then(function () {
o(gotTimeoutError).equals(true)
done()
})
})
})
o.spec("json header", function() {
function checkUnset(method) {
Expand Down
57 changes: 41 additions & 16 deletions test-utils/xhrMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module.exports = function() {
}
this.responseType = ""
this.response = null
this.timeout = 0
Object.defineProperty(this, "responseText", {get: function() {
if (this.responseType === "" || this.responseType === "text") {
return this.response
Expand All @@ -55,25 +56,49 @@ module.exports = function() {
}})
this.send = function(body) {
var self = this
if(!aborted) {
var handler = routes[args.method + " " + args.pathname] || serverErrorHandler.bind(null, args.pathname)
var data = handler({rawUrl: args.rawUrl, url: args.pathname, query: args.search || {}, body: body || null})
self.status = data.status
// Match spec
if (self.responseType === "json") {
try { self.response = JSON.parse(data.responseText) }
catch (e) { /* ignore */ }

var completeResponse = function (data) {
self._responseCompleted = true
if(!aborted) {
self.status = data.status
// Match spec
if (self.responseType === "json") {
try { self.response = JSON.parse(data.responseText) }
catch (e) { /* ignore */ }
} else {
self.response = data.responseText
}
} else {
self.response = data.responseText
self.status = 0
}
self.readyState = 4
if (args.async === true) {
callAsync(function() {
if (typeof self.onreadystatechange === "function") self.onreadystatechange({target: self})
})
}
} else {
self.status = 0
}
self.readyState = 4
if (args.async === true) {
callAsync(function() {
if (typeof self.onreadystatechange === "function") self.onreadystatechange({target: self})
})

var data
if (!aborted) {
var handler = routes[args.method + " " + args.pathname] || serverErrorHandler.bind(null, args.pathname)
data = handler({rawUrl: args.rawUrl, url: args.pathname, query: args.search || {}, body: body || null})
}

if (typeof self.timeout === "number" && self.timeout > 0) {
setTimeout(function () {
if (self._responseCompleted) {
return
}

if (typeof self.ontimeout === "function") self.ontimeout({target: self})
}, self.timeout)
}

if (data instanceof Promise) {
data.then(completeResponse)
} else {
completeResponse(data)
}
}
this.abort = function() {
Expand Down