From 0d14f5729d99e4cec3bcc3b089c7d7f12b2146d9 Mon Sep 17 00:00:00 2001 From: slackersoft Date: Mon, 17 Nov 2014 13:33:19 -0800 Subject: [PATCH] Deprecate the `response` function on fake request. Starting on #49 #60 #89 --- lib/mock-ajax.js | 9 ++++++- spec/fakeRequestSpec.js | 44 ++++++++++++++++----------------- spec/mock-ajax-toplevel-spec.js | 14 +++++------ src/fakeRequest.js | 9 ++++++- 4 files changed, 45 insertions(+), 31 deletions(-) diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index caf6b23..342f297 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -209,7 +209,7 @@ getJasmineRequireObj().AjaxFakeRequest = function() { var stub = stubTracker.findStub(this.url, data, this.method); if (stub) { - this.response(stub); + this.respondWith(stub); } }, @@ -253,6 +253,13 @@ getJasmineRequireObj().AjaxFakeRequest = function() { responseText: null, response: function(response) { + if (window.console && window.console.warn) { + window.console.warn("jasmine-ajax's response method is deprecated because it conflicts with XmlHTTPRequest 2 sytax. It will be removed in a later version. Please use respondWith"); + } + this.respondWith(response); + }, + + respondWith: function(response) { if (this.readyState === 4) { throw new Error("FakeXMLHttpRequest already completed"); } diff --git a/spec/fakeRequestSpec.js b/spec/fakeRequestSpec.js index de5e666..0740b94 100644 --- a/spec/fakeRequestSpec.js +++ b/spec/fakeRequestSpec.js @@ -157,7 +157,7 @@ describe('FakeRequest', function() { this.request.send(); this.request.onreadystatechange.calls.reset(); - this.request.response({}); + this.request.respondWith({}); expect(this.request.readyState).toBe(4); expect(this.request.onreadystatechange).toHaveBeenCalled(); @@ -166,7 +166,7 @@ describe('FakeRequest', function() { it('throws an error when timing out a request that has completed', function() { this.request.open(); this.request.send(); - this.request.response({}); + this.request.respondWith({}); var request = this.request; expect(function() { @@ -177,18 +177,18 @@ describe('FakeRequest', function() { it('throws an error when responding to a request that has completed', function() { this.request.open(); this.request.send(); - this.request.response({}); + this.request.respondWith({}); var request = this.request; expect(function() { - request.response({}); + request.respondWith({}); }).toThrowError('FakeXMLHttpRequest already completed'); }); it('throws an error when erroring a request that has completed', function() { this.request.open(); this.request.send(); - this.request.response({}); + this.request.respondWith({}); var request = this.request; expect(function() { @@ -368,7 +368,7 @@ describe('FakeRequest', function() { this.resetEvents(); - this.request.response({ status: 200 }); + this.request.respondWith({ status: 200 }); expect(this.request).not.toHaveTriggeredEvent('loadstart'); expect(this.request).toHaveTriggeredEvent('progress'); @@ -413,7 +413,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ status: 200 }); + request.respondWith({ status: 200 }); expect(request.status).toBe(200); expect(request.statusText).toBe(''); @@ -424,7 +424,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ status: 200, statusText: 'OK' }); + request.respondWith({ status: 200, statusText: 'OK' }); expect(request.status).toBe(200); expect(request.statusText).toBe('OK'); @@ -462,7 +462,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ status: 200, responseText: 'foobar' }); + request.respondWith({ status: 200, responseText: 'foobar' }); expect(request.responseText).toBe('foobar'); }); @@ -472,7 +472,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ status: 200 }); + request.respondWith({ status: 200 }); expect(request.responseText).toBe(''); }); @@ -482,7 +482,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ + request.respondWith({ status: 200, responseHeaders: { 'X-Header': 'foo' @@ -497,7 +497,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ + request.respondWith({ status: 200, responseHeaders: { 'X-Header': 'foo' @@ -512,7 +512,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ + request.respondWith({ status: 200, responseHeaders: [ { name: 'X-Header', value: 'foo' }, @@ -532,8 +532,8 @@ describe('FakeRequest', function() { request2.open(); request2.send(); - request1.response({ status: 200, responseHeaders: { 'X-Foo': 'bar' } }); - request2.response({ status: 200, responseHeaders: { 'X-Baz': 'quux' } }); + request1.respondWith({ status: 200, responseHeaders: { 'X-Foo': 'bar' } }); + request2.respondWith({ status: 200, responseHeaders: { 'X-Baz': 'quux' } }); expect(request1.getAllResponseHeaders()).toBe('X-Foo: bar'); expect(request2.getAllResponseHeaders()).toBe('X-Baz: quux'); @@ -544,7 +544,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ + request.respondWith({ status: 200, responseHeaders: [ { name: 'X-Header-1', value: 'foo' }, @@ -561,7 +561,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ status: 200, contentType: 'text/plain' }); + request.respondWith({ status: 200, contentType: 'text/plain' }); expect(request.getResponseHeader('content-type')).toBe('text/plain'); expect(request.getAllResponseHeaders()).toBe('Content-Type: text/plain'); @@ -572,7 +572,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ status: 200 }); + request.respondWith({ status: 200 }); expect(request.getResponseHeader('content-type')).toBe('application/json'); expect(request.getAllResponseHeaders()).toBe('Content-Type: application/json'); @@ -583,7 +583,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ status: 200 }); + request.respondWith({ status: 200 }); expect(request.responseXML).toBeNull(); }); @@ -593,7 +593,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ status: 200, contentType: 'text/xml', responseText: '' }); + request.respondWith({ status: 200, contentType: 'text/xml', responseText: '' }); if (typeof window.Document !== 'undefined') { expect(request.responseXML instanceof window.Document).toBe(true); @@ -608,7 +608,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ status: 200, contentType: 'application/xml', responseText: '' }); + request.respondWith({ status: 200, contentType: 'application/xml', responseText: '' }); if (typeof window.Document !== 'undefined') { expect(request.responseXML instanceof window.Document).toBe(true); @@ -623,7 +623,7 @@ describe('FakeRequest', function() { request.open(); request.send(); - request.response({ status: 200, contentType: 'application/text+xml', responseText: '' }); + request.respondWith({ status: 200, contentType: 'application/text+xml', responseText: '' }); if (typeof window.Document !== 'undefined') { expect(request.responseXML instanceof window.Document).toBe(true); diff --git a/spec/mock-ajax-toplevel-spec.js b/spec/mock-ajax-toplevel-spec.js index 88e9102..bb73691 100644 --- a/spec/mock-ajax-toplevel-spec.js +++ b/spec/mock-ajax-toplevel-spec.js @@ -140,7 +140,7 @@ describe("Jasmine Mock Ajax (for toplevel)", function() { request = mockAjax.requests.mostRecent(); response = {status: 200, statusText: "OK", contentType: "text/html", responseText: "OK!"}; - request.response(response); + request.respondWith(response); sharedContext.responseCallback = success; sharedContext.status = response.status; @@ -175,7 +175,7 @@ describe("Jasmine Mock Ajax (for toplevel)", function() { request = mockAjax.requests.mostRecent(); var responseObject = {status: 200, statusText: "OK", contentType: "application/json", responseText: '{"foo":"bar"}'}; - request.response(responseObject); + request.respondWith(responseObject); sharedContext.responseCallback = success; sharedContext.status = responseObject.status; @@ -218,7 +218,7 @@ describe("Jasmine Mock Ajax (for toplevel)", function() { 'X-Header2': 'header 2 value', 'X-Header3': 'header 3 value' }}; - request.response(responseObject); + request.respondWith(responseObject); response = success.calls.mostRecent().args[2]; }); @@ -249,7 +249,7 @@ describe("Jasmine Mock Ajax (for toplevel)", function() { { name: 'X-Header', value: 'header value 1' }, { name: 'X-Header', value: 'header value 2' } ]}; - request.response(responseObject); + request.respondWith(responseObject); response = success.calls.mostRecent().args[2]; }); @@ -275,7 +275,7 @@ describe("Jasmine Mock Ajax (for toplevel)", function() { request = mockAjax.requests.mostRecent(); response = {status: 200, statusText: "OK", responseText: '{"foo": "valid JSON, dammit."}'}; - request.response(response); + request.respondWith(response); sharedContext.responseCallback = success; sharedContext.status = response.status; @@ -309,7 +309,7 @@ describe("Jasmine Mock Ajax (for toplevel)", function() { request = mockAjax.requests.mostRecent(); response = {status: 0, statusText: "ABORT", responseText: '{"foo": "whoops!"}'}; - request.response(response); + request.respondWith(response); sharedContext.responseCallback = error; sharedContext.status = 0; @@ -344,7 +344,7 @@ describe("Jasmine Mock Ajax (for toplevel)", function() { request = mockAjax.requests.mostRecent(); response = {status: 500, statusText: "SERVER ERROR", contentType: "text/html", responseText: "(._){"}; - request.response(response); + request.respondWith(response); sharedContext.responseCallback = error; sharedContext.status = response.status; diff --git a/src/fakeRequest.js b/src/fakeRequest.js index 37db0fa..9eff21c 100644 --- a/src/fakeRequest.js +++ b/src/fakeRequest.js @@ -164,7 +164,7 @@ getJasmineRequireObj().AjaxFakeRequest = function() { var stub = stubTracker.findStub(this.url, data, this.method); if (stub) { - this.response(stub); + this.respondWith(stub); } }, @@ -208,6 +208,13 @@ getJasmineRequireObj().AjaxFakeRequest = function() { responseText: null, response: function(response) { + if (window.console && window.console.warn) { + window.console.warn("jasmine-ajax's response method is deprecated because it conflicts with XmlHTTPRequest 2 sytax. It will be removed in a later version. Please use respondWith"); + } + this.respondWith(response); + }, + + respondWith: function(response) { if (this.readyState === 4) { throw new Error("FakeXMLHttpRequest already completed"); }