Set response attribute for document responses

This commit is contained in:
slackersoft 2014-11-26 21:25:49 -08:00
parent 6b5cae82d5
commit 7446f2a190
3 changed files with 58 additions and 21 deletions

View File

@ -597,9 +597,11 @@ describe('FakeRequest', function() {
if (typeof window.Document !== 'undefined') { if (typeof window.Document !== 'undefined') {
expect(request.responseXML instanceof window.Document).toBe(true); expect(request.responseXML instanceof window.Document).toBe(true);
expect(request.response instanceof window.Document).toBe(true);
} else { } else {
// IE 8 // IE 8
expect(request.responseXML instanceof window.ActiveXObject).toBe(true); expect(request.responseXML instanceof window.ActiveXObject).toBe(true);
expect(request.response instanceof window.ActiveXObject).toBe(true);
} }
}); });
@ -612,9 +614,11 @@ describe('FakeRequest', function() {
if (typeof window.Document !== 'undefined') { if (typeof window.Document !== 'undefined') {
expect(request.responseXML instanceof window.Document).toBe(true); expect(request.responseXML instanceof window.Document).toBe(true);
expect(request.response instanceof window.Document).toBe(true);
} else { } else {
// IE 8 // IE 8
expect(request.responseXML instanceof window.ActiveXObject).toBe(true); expect(request.responseXML instanceof window.ActiveXObject).toBe(true);
expect(request.response instanceof window.ActiveXObject).toBe(true);
} }
}); });
@ -627,9 +631,41 @@ describe('FakeRequest', function() {
if (typeof window.Document !== 'undefined') { if (typeof window.Document !== 'undefined') {
expect(request.responseXML instanceof window.Document).toBe(true); expect(request.responseXML instanceof window.Document).toBe(true);
expect(request.response instanceof window.Document).toBe(true);
} else { } else {
// IE 8 // IE 8
expect(request.responseXML instanceof window.ActiveXObject).toBe(true); expect(request.responseXML instanceof window.ActiveXObject).toBe(true);
expect(request.response instanceof window.ActiveXObject).toBe(true);
} }
}); });
it('defaults the response attribute to the responseText', function() {
var request = new this.FakeRequest();
request.open();
request.send();
request.respondWith({ status: 200, responseText: 'foo' });
expect(request.response).toEqual('foo');
});
it('has a text response when the responseType is blank', function() {
var request = new this.FakeRequest();
request.open();
request.send();
request.respondWith({ status: 200, responseText: 'foo', responseType: '' });
expect(request.response).toEqual('foo');
});
it('has a text response when the responseType is text', function() {
var request = new this.FakeRequest();
request.open();
request.send();
request.respondWith({ status: 200, responseText: 'foo', responseType: 'text' });
expect(request.response).toEqual('foo');
});
}); });

View File

@ -22,8 +22,8 @@ describe("Jasmine Mock Ajax (for toplevel)", function() {
complete = jasmine.createSpy("onComplete"); complete = jasmine.createSpy("onComplete");
onreadystatechange = function() { onreadystatechange = function() {
if (this.readyState == (this.DONE || 4)) { // IE 8 doesn't support DONE if (this.readyState === (this.DONE || 4)) { // IE 8 doesn't support DONE
if (this.status == 200) { if (this.status === 200) {
success(this.responseText, this.textStatus, this); success(this.responseText, this.textStatus, this);
} else { } else {
error(this, this.textStatus, ''); error(this, this.textStatus, '');
@ -211,7 +211,7 @@ describe("Jasmine Mock Ajax (for toplevel)", function() {
client = new fakeGlobal.XMLHttpRequest(); client = new fakeGlobal.XMLHttpRequest();
client.onreadystatechange = onreadystatechange; client.onreadystatechange = onreadystatechange;
client.open("GET", "example.com/someApi"); client.open("GET", "example.com/someApi");
client.setRequestHeader("Content-Type", "application/json") client.setRequestHeader("Content-Type", "application/json");
client.send(); client.send();
request = mockAjax.requests.mostRecent(); request = mockAjax.requests.mostRecent();
@ -462,7 +462,7 @@ function sharedAjaxResponseBehaviorForZepto_Success(context) {
}); });
it("should have the expected xhr2 response", function() { it("should have the expected xhr2 response", function() {
var expected = context.response || context.responseType == 'json' ? JSON.parse(context.responseText) : context.responseText; var expected = context.response || context.responseType === 'json' ? JSON.parse(context.responseText) : context.responseText;
expect(xhr.response).toEqual(expected); expect(xhr.response).toEqual(expected);
}); });
@ -492,7 +492,7 @@ function sharedAjaxResponseBehaviorForZepto_Failure(context) {
}); });
it("should have the expected xhr2 response", function() { it("should have the expected xhr2 response", function() {
var expected = context.response || xhr.responseType == 'json' ? JSON.parse(xhr.responseText) : xhr.responseText; var expected = context.response || xhr.responseType === 'json' ? JSON.parse(xhr.responseText) : xhr.responseText;
expect(xhr.response).toEqual(expected); expect(xhr.response).toEqual(expected);
}); });

View File

@ -38,6 +38,16 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
}; };
} }
function unconvertibleResponseTypeMessage(type) {
var msg = [
"Can't build XHR.response for XHR.responseType of '",
type,
"'.",
"XHR.response must be explicitly stubbed"
];
return msg.join(' ');
}
function fakeRequest(global, requestTracker, stubTracker, paramParser) { function fakeRequest(global, requestTracker, stubTracker, paramParser) {
function FakeXMLHttpRequest() { function FakeXMLHttpRequest() {
requestTracker.track(this); requestTracker.track(this);
@ -218,23 +228,11 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
case "json": case "json":
return JSON.parse(this.responseText); return JSON.parse(this.responseText);
case "arraybuffer": case "arraybuffer":
var msg = [ throw unconvertibleResponseTypeMessage('arraybuffer');
"Can't build XHR.response for XHR.responseType of 'arraybuffer'.",
"XHR.response must be explicitly stubbed"
];
throw msg.join(' ');
case "blob": case "blob":
var msg = [ throw unconvertibleResponseTypeMessage('blob');
"Can't build XHR.response for XHR.responseType of 'blob'.",
"XHR.response must be explicitly stubbed"
];
throw msg.join(' ');
case "document": case "document":
var msg = [ return this.responseXML;
"Can't build XHR.response for XHR.responseType of 'document'.",
"XHR.response must be explicitly stubbed"
];
throw msg.join(' ');
} }
}, },
@ -249,13 +247,16 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
this.responseType = response.responseType || ""; this.responseType = response.responseType || "";
this.readyState = 4; this.readyState = 4;
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType); this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType);
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || '');
if (this.responseXML) {
this.responseType = 'document';
}
if ('response' in response) { if ('response' in response) {
this.response = response.response; this.response = response.response;
} else { } else {
this.response = this.responseValue(); this.response = this.responseValue();
} }
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || '');
this.onreadystatechange(); this.onreadystatechange();
this.events.progress(); this.events.progress();