Skip to content

Commit

Permalink
Deprecate the response function on fake request.
Browse files Browse the repository at this point in the history
Starting on #49 #60 #89
  • Loading branch information
slackersoft committed Nov 17, 2014
1 parent e540fcf commit 0d14f57
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 31 deletions.
9 changes: 8 additions & 1 deletion lib/mock-ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ getJasmineRequireObj().AjaxFakeRequest = function() {

var stub = stubTracker.findStub(this.url, data, this.method);
if (stub) {
this.response(stub);
this.respondWith(stub);
}
},

Expand Down Expand Up @@ -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");
}
Expand Down
44 changes: 22 additions & 22 deletions spec/fakeRequestSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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('');
Expand All @@ -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');
Expand Down Expand Up @@ -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');
});
Expand All @@ -472,7 +472,7 @@ describe('FakeRequest', function() {
request.open();
request.send();

request.response({ status: 200 });
request.respondWith({ status: 200 });

expect(request.responseText).toBe('');
});
Expand All @@ -482,7 +482,7 @@ describe('FakeRequest', function() {
request.open();
request.send();

request.response({
request.respondWith({
status: 200,
responseHeaders: {
'X-Header': 'foo'
Expand All @@ -497,7 +497,7 @@ describe('FakeRequest', function() {
request.open();
request.send();

request.response({
request.respondWith({
status: 200,
responseHeaders: {
'X-Header': 'foo'
Expand All @@ -512,7 +512,7 @@ describe('FakeRequest', function() {
request.open();
request.send();

request.response({
request.respondWith({
status: 200,
responseHeaders: [
{ name: 'X-Header', value: 'foo' },
Expand All @@ -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');
Expand All @@ -544,7 +544,7 @@ describe('FakeRequest', function() {
request.open();
request.send();

request.response({
request.respondWith({
status: 200,
responseHeaders: [
{ name: 'X-Header-1', value: 'foo' },
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -583,7 +583,7 @@ describe('FakeRequest', function() {
request.open();
request.send();

request.response({ status: 200 });
request.respondWith({ status: 200 });

expect(request.responseXML).toBeNull();
});
Expand All @@ -593,7 +593,7 @@ describe('FakeRequest', function() {
request.open();
request.send();

request.response({ status: 200, contentType: 'text/xml', responseText: '<dom><stuff/></dom>' });
request.respondWith({ status: 200, contentType: 'text/xml', responseText: '<dom><stuff/></dom>' });

if (typeof window.Document !== 'undefined') {
expect(request.responseXML instanceof window.Document).toBe(true);
Expand All @@ -608,7 +608,7 @@ describe('FakeRequest', function() {
request.open();
request.send();

request.response({ status: 200, contentType: 'application/xml', responseText: '<dom><stuff/></dom>' });
request.respondWith({ status: 200, contentType: 'application/xml', responseText: '<dom><stuff/></dom>' });

if (typeof window.Document !== 'undefined') {
expect(request.responseXML instanceof window.Document).toBe(true);
Expand All @@ -623,7 +623,7 @@ describe('FakeRequest', function() {
request.open();
request.send();

request.response({ status: 200, contentType: 'application/text+xml', responseText: '<dom><stuff/></dom>' });
request.respondWith({ status: 200, contentType: 'application/text+xml', responseText: '<dom><stuff/></dom>' });

if (typeof window.Document !== 'undefined') {
expect(request.responseXML instanceof window.Document).toBe(true);
Expand Down
14 changes: 7 additions & 7 deletions spec/mock-ajax-toplevel-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
});

Expand Down Expand Up @@ -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];
});

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 8 additions & 1 deletion src/fakeRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ getJasmineRequireObj().AjaxFakeRequest = function() {

var stub = stubTracker.findStub(this.url, data, this.method);
if (stub) {
this.response(stub);
this.respondWith(stub);
}
},

Expand Down Expand Up @@ -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");
}
Expand Down

0 comments on commit 0d14f57

Please sign in to comment.