From c788f132242a27c34b42243754f8c3c3da1f881e Mon Sep 17 00:00:00 2001 From: Don McCaughey and Greg Cobb Date: Mon, 2 Dec 2013 17:51:21 -0800 Subject: [PATCH] Create requestHeaders object in FakeXMLHttpRequest constructor instead of on the prototype --- lib/mock-ajax.js | 3 +- .../javascripts/fake-xml-http-request-spec.js | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index f81fffc..d4aaba9 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -98,12 +98,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. function fakeRequest(requestTracker, stubTracker) { function FakeXMLHttpRequest() { requestTracker.track(this); + this.requestHeaders = {}; } extend(FakeXMLHttpRequest.prototype, window.XMLHttpRequest); extend(FakeXMLHttpRequest.prototype, { - requestHeaders: {}, - open: function() { this.method = arguments[0]; this.url = arguments[1]; diff --git a/spec/javascripts/fake-xml-http-request-spec.js b/spec/javascripts/fake-xml-http-request-spec.js index d81ff2e..8476361 100644 --- a/spec/javascripts/fake-xml-http-request-spec.js +++ b/spec/javascripts/fake-xml-http-request-spec.js @@ -1,17 +1,46 @@ describe("FakeXMLHttpRequest", function() { var xhr; + var xhr2; beforeEach(function() { var realXMLHttpRequest = jasmine.createSpy('realRequest'), fakeGlobal = {XMLHttpRequest: realXMLHttpRequest}, mockAjax = new MockAjax(fakeGlobal); mockAjax.install(); xhr = new fakeGlobal.XMLHttpRequest(); + xhr2 = new fakeGlobal.XMLHttpRequest(); }); it("should have an initial readyState of 0 (uninitialized)", function() { expect(xhr.readyState).toEqual(0); }); + describe("when setting request headers", function() { + beforeEach(function() { + xhr.setRequestHeader('X-Header-1', 'one'); + }); + + it("should make the request headers available", function() { + expect(Object.keys(xhr.requestHeaders).length).toEqual(1); + expect(xhr.requestHeaders['X-Header-1']).toEqual('one'); + }); + + describe("when setting headers on another xhr object", function() { + beforeEach(function() { + xhr2.setRequestHeader('X-Header-2', 'two'); + }); + + it("should make the only its request headers available", function() { + expect(Object.keys(xhr2.requestHeaders).length).toEqual(1); + expect(xhr2.requestHeaders['X-Header-2']).toEqual('two'); + }); + + it("should not modify any other xhr objects", function() { + expect(Object.keys(xhr.requestHeaders).length).toEqual(1); + expect(xhr.requestHeaders['X-Header-1']).toEqual('one'); + }); + }); + }); + describe("when opened", function() { beforeEach(function() { xhr.open("GET", "http://example.com");