Throw an error if response() is called more than once on requests

Closes #71 Fixes #70
This commit is contained in:
Kevin Montag 2014-07-11 00:23:13 -07:00 committed by slackersoft
parent 199324e720
commit 9908439d81
2 changed files with 22 additions and 0 deletions

View File

@ -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');

View File

@ -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() {