Add tests for abort functionality.

This commit is contained in:
Bryce Gibson 2017-02-12 16:38:45 +11:00
parent 9558c8e2e9
commit 6a617aeb87
2 changed files with 38 additions and 4 deletions

View file

@ -390,6 +390,22 @@ o.spec("xhr", function() {
o(xhr.getRequestHeader("Accept")).equals("application/json, text/*")
}
})
o("doesn't fail on abort", function(done) {
var s = new Date
mock.$defineRoutes({
"GET /item": function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
}
})
var failed = false
xhr({method: "GET", url: "/item", config: function (xhr) { setTimeout(function() { xhr.abort() }, 0) }}).catch(function() {
failed = true
}).then(function() {
o(failed).equals(false)
}).then(function() {
done()
})
})
/*o("data maintains after interpolate", function() {
mock.$defineRoutes({
"PUT /items/:x": function() {
@ -463,5 +479,15 @@ o.spec("xhr", function() {
})
})
})
o("rejects on cors-like error", function(done) {
mock.$defineRoutes({
"GET /item": function(request) {
return {status: 0}
}
})
xhr({method: "GET", url: "/item"}).catch(function(e) {
o(e instanceof Error).equals(true)
}).then(done)
})
})
})

View file

@ -15,6 +15,7 @@ module.exports = function() {
XMLHttpRequest: function XMLHttpRequest() {
var args = {}
var headers = {}
var aborted = false
this.setRequestHeader = function(header, value) {
headers[header] = value
}
@ -32,11 +33,15 @@ module.exports = function() {
}
this.send = function(body) {
var self = this
var handler = routes[args.method + " " + args.pathname] || serverErrorHandler.bind(null, args.pathname)
var data = handler({url: args.pathname, query: args.search || {}, body: body || null})
if(!aborted) {
var handler = routes[args.method + " " + args.pathname] || serverErrorHandler.bind(null, args.pathname)
var data = handler({url: args.pathname, query: args.search || {}, body: body || null})
self.status = data.status
self.responseText = data.responseText
} else {
self.status = 0
}
self.readyState = 4
self.status = data.status
self.responseText = data.responseText
if (args.async === true) {
var s = new Date
callAsync(function() {
@ -44,6 +49,9 @@ module.exports = function() {
})
}
}
this.abort = function() {
aborted = true
}
},
document: {
createElement: function(tag) {