From b90ca36b9dc1c7e308c98696926c43512ba4c3e6 Mon Sep 17 00:00:00 2001 From: "Nicholas H.Tollervey" Date: Mon, 6 Feb 2012 16:04:14 +0000 Subject: [PATCH] Added onerror event handler to XHR, removed the (incorrect) assumption that status code 0 = fail and updated the affected test. Writing a test to exercise this change has proven to be a bit of a time sink but as far as I can tell, we're now doing the 'right thing' (tm) --- fluidinfo.js | 7 ++++++- spec/fluidinfoSpec.js | 30 ------------------------------ 2 files changed, 6 insertions(+), 31 deletions(-) diff --git a/fluidinfo.js b/fluidinfo.js index 53126ff..9f26d50 100644 --- a/fluidinfo.js +++ b/fluidinfo.js @@ -397,11 +397,16 @@ var fluidinfo = function(options) { options.data = JSON.stringify(options.data); } } + xhr.onerror = function() { + // handle stuff nicely + var result = createNiceResult(xhr); + options.onError(result); + }; xhr.onreadystatechange = function() { if (xhr.readyState != 4) return; var result = createNiceResult(xhr); // call the event handlers - if (xhr.status > 0 && (xhr.status < 300 || xhr.status == 304)) { + if (xhr.status < 300 || xhr.status == 304) { if (options.onSuccess){ options.onSuccess(result); } diff --git a/spec/fluidinfoSpec.js b/spec/fluidinfoSpec.js index d08f2b1..42a95be 100644 --- a/spec/fluidinfoSpec.js +++ b/spec/fluidinfoSpec.js @@ -408,36 +408,6 @@ describe("Fluidinfo.js", function() { expect(spy.calledOnce).toBeTruthy(); }); - it("should provide a simple response object for onError when the " + - "response status is zero", function() { - var options = new Object(); - options.path = "namespaces/test"; - var payload = {name: "foo", description: "bar"}; - options.data = payload; - var spy = sinon.spy(); - options.onError = function(result) { - expect(typeof(result)).toEqual("object"); - expect(result.status).toEqual(0); - expect(typeof(result.headers)).toEqual("object"); - expect(result.data).toEqual(""); - expect(result.rawData).toEqual(""); - // original XHR: - expect(typeof(result.request)).toEqual("object"); - spy(); // to prove the function was called - }; - this.fi.api.post(options); - var responseStatus = 0; - var responseHeaders = { - "Content-Type": "text/html", - "Location": "http://fluiddb.fluidinfo.com/" + - "namespaces/test/foo", - "Date": "Mon, 02 Aug 2010 12:40:41 GMT"}; - var responseText = ''; - this.server.requests[0].respond(responseStatus, responseHeaders, - responseText); - expect(spy.calledOnce).toBeTruthy(); - }); - it("should return a simple response object for onSuccess when " + "async=False", function() { var options = new Object();