Create requestHeaders object in FakeXMLHttpRequest constructor instead of on the prototype

This commit is contained in:
Don McCaughey and Greg Cobb 2013-12-02 17:51:21 -08:00
parent b0d4d0d573
commit c788f13224
2 changed files with 30 additions and 2 deletions

View File

@ -98,12 +98,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
function fakeRequest(requestTracker, stubTracker) { function fakeRequest(requestTracker, stubTracker) {
function FakeXMLHttpRequest() { function FakeXMLHttpRequest() {
requestTracker.track(this); requestTracker.track(this);
this.requestHeaders = {};
} }
extend(FakeXMLHttpRequest.prototype, window.XMLHttpRequest); extend(FakeXMLHttpRequest.prototype, window.XMLHttpRequest);
extend(FakeXMLHttpRequest.prototype, { extend(FakeXMLHttpRequest.prototype, {
requestHeaders: {},
open: function() { open: function() {
this.method = arguments[0]; this.method = arguments[0];
this.url = arguments[1]; this.url = arguments[1];

View File

@ -1,17 +1,46 @@
describe("FakeXMLHttpRequest", function() { describe("FakeXMLHttpRequest", function() {
var xhr; var xhr;
var xhr2;
beforeEach(function() { beforeEach(function() {
var realXMLHttpRequest = jasmine.createSpy('realRequest'), var realXMLHttpRequest = jasmine.createSpy('realRequest'),
fakeGlobal = {XMLHttpRequest: realXMLHttpRequest}, fakeGlobal = {XMLHttpRequest: realXMLHttpRequest},
mockAjax = new MockAjax(fakeGlobal); mockAjax = new MockAjax(fakeGlobal);
mockAjax.install(); mockAjax.install();
xhr = new fakeGlobal.XMLHttpRequest(); xhr = new fakeGlobal.XMLHttpRequest();
xhr2 = new fakeGlobal.XMLHttpRequest();
}); });
it("should have an initial readyState of 0 (uninitialized)", function() { it("should have an initial readyState of 0 (uninitialized)", function() {
expect(xhr.readyState).toEqual(0); 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() { describe("when opened", function() {
beforeEach(function() { beforeEach(function() {
xhr.open("GET", "http://example.com"); xhr.open("GET", "http://example.com");