diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index 224c13d..7cc57b5 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -249,6 +249,9 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. responseText: null, response: function(response) { + if (this.readyState === 4) { + throw new Error("FakeXMLHttpRequest already completed"); + } this.status = response.status; this.statusText = response.statusText || ""; this.responseText = response.responseText || ""; @@ -261,6 +264,9 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }, responseTimeout: function() { + if (this.readyState === 4) { + throw new Error("FakeXMLHttpRequest already completed"); + } this.readyState = 4; jasmine.clock().tick(30000); this.onreadystatechange('timeout'); diff --git a/spec/javascripts/fake-xml-http-request-spec.js b/spec/javascripts/fake-xml-http-request-spec.js index 197414f..8751a86 100644 --- a/spec/javascripts/fake-xml-http-request-spec.js +++ b/spec/javascripts/fake-xml-http-request-spec.js @@ -81,6 +81,22 @@ describe("FakeXMLHttpRequest", function() { expect(xhr.readyState).toEqual(4); expect(xhr.onreadystatechange).toHaveBeenCalled(); }); + + describe("when a second response comes in", function() { + it("should throw an error", function() { + xhr.onreadystatechange.calls.reset(); + xhr.response({status: 200}); + expect(function() { xhr.response({status: 200}); }).toThrowError('FakeXMLHttpRequest already completed'); + }); + }); + + describe("when a second response comes in as a timout", function() { + it("should throw an error", function() { + xhr.onreadystatechange.calls.reset(); + xhr.response({status: 200}); + expect(function() { xhr.responseTimeout(); }).toThrowError('FakeXMLHttpRequest already completed'); + }); + }); }); describe("when aborted", function() {