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') {
expect(request.responseXML instanceof window.Document).toBe(true);
expect(request.response instanceof window.Document).toBe(true);
} else {
// IE 8
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') {
expect(request.responseXML instanceof window.Document).toBe(true);
expect(request.response instanceof window.Document).toBe(true);
} else {
// IE 8
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') {
expect(request.responseXML instanceof window.Document).toBe(true);
expect(request.response instanceof window.Document).toBe(true);
} else {
// IE 8
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");
onreadystatechange = function() {
if (this.readyState == (this.DONE || 4)) { // IE 8 doesn't support DONE
if (this.status == 200) {
if (this.readyState === (this.DONE || 4)) { // IE 8 doesn't support DONE
if (this.status === 200) {
success(this.responseText, this.textStatus, this);
} else {
error(this, this.textStatus, '');
@ -211,7 +211,7 @@ describe("Jasmine Mock Ajax (for toplevel)", function() {
client = new fakeGlobal.XMLHttpRequest();
client.onreadystatechange = onreadystatechange;
client.open("GET", "example.com/someApi");
client.setRequestHeader("Content-Type", "application/json")
client.setRequestHeader("Content-Type", "application/json");
client.send();
request = mockAjax.requests.mostRecent();
@ -462,7 +462,7 @@ function sharedAjaxResponseBehaviorForZepto_Success(context) {
});
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);
});
@ -492,7 +492,7 @@ function sharedAjaxResponseBehaviorForZepto_Failure(context) {
});
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);
});

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