diff --git a/request/tests/test-request.js b/request/tests/test-request.js index ade0465c..d903a4ed 100644 --- a/request/tests/test-request.js +++ b/request/tests/test-request.js @@ -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) + }) }) }) diff --git a/test-utils/xhrMock.js b/test-utils/xhrMock.js index 37d48e92..57c2887f 100644 --- a/test-utils/xhrMock.js +++ b/test-utils/xhrMock.js @@ -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) {