Parse responseText
into a responseXML
if the contentType is xml-ish
closes #55
This commit is contained in:
parent
5665351ab2
commit
4751eaafbb
@ -99,6 +99,28 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
function parseXml(xmlText, contentType) {
|
||||
if (global.DOMParser) {
|
||||
return (new global.DOMParser()).parseFromString(xmlText, contentType);
|
||||
} else {
|
||||
var xml = new global.ActiveXObject("Microsoft.XMLDOM");
|
||||
xml.async = "false";
|
||||
xml.loadXML(xmlText);
|
||||
return xml;
|
||||
}
|
||||
}
|
||||
|
||||
var xmlParsables = ['text/html', 'text/xml', 'application/xml'];
|
||||
|
||||
function getResponseXml(responseText, contentType) {
|
||||
if (xmlParsables.indexOf(contentType.toLowerCase()) >= 0) {
|
||||
return parseXml(responseText, contentType);
|
||||
} else if (contentType.match(/\+xml$/)) {
|
||||
return parseXml(responseText, 'text/xml');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout'];
|
||||
extend(FakeXMLHttpRequest.prototype, new global.XMLHttpRequest(), iePropertiesThatCannotBeCopied);
|
||||
extend(FakeXMLHttpRequest.prototype, {
|
||||
@ -199,6 +221,7 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
|
||||
this.responseText = response.responseText || "";
|
||||
this.readyState = 4;
|
||||
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType);
|
||||
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || '');
|
||||
|
||||
this.onload();
|
||||
this.onreadystatechange();
|
||||
|
@ -7,7 +7,9 @@ describe('FakeRequest', function() {
|
||||
this.fakeGlobal = {
|
||||
XMLHttpRequest: function() {
|
||||
this.extraAttribute = 'my cool attribute';
|
||||
}
|
||||
},
|
||||
DOMParser: window.DOMParser,
|
||||
ActiveXObject: window.ActiveXObject
|
||||
};
|
||||
this.FakeRequest = getJasmineRequireObj().AjaxFakeRequest()(this.fakeGlobal, this.requestTracker, this.stubTracker, this.paramParser);
|
||||
});
|
||||
@ -372,4 +374,74 @@ describe('FakeRequest', function() {
|
||||
expect(request.getResponseHeader('content-type')).toBe('application/json');
|
||||
expect(request.getAllResponseHeaders()).toBe('Content-Type: application/json');
|
||||
});
|
||||
|
||||
it('has no responseXML by default', function() {
|
||||
var request = new this.FakeRequest();
|
||||
request.open();
|
||||
request.send();
|
||||
|
||||
request.response({ status: 200 });
|
||||
|
||||
expect(request.responseXML).toBeNull();
|
||||
});
|
||||
|
||||
it('parses a text/xml document into responseXML', function() {
|
||||
var request = new this.FakeRequest();
|
||||
request.open();
|
||||
request.send();
|
||||
|
||||
request.response({ status: 200, contentType: 'text/xml', responseText: '<dom><stuff/></dom>' });
|
||||
|
||||
if (typeof window.Document !== 'undefined') {
|
||||
expect(request.responseXML).toEqual(jasmine.any(window.Document));
|
||||
} else {
|
||||
// IE 8
|
||||
expect(request.responseXML).toEqual(jasmine.any(window.ActiveXObject));
|
||||
}
|
||||
});
|
||||
|
||||
it('parses an application/xml document into responseXML', function() {
|
||||
var request = new this.FakeRequest();
|
||||
request.open();
|
||||
request.send();
|
||||
|
||||
request.response({ status: 200, contentType: 'application/xml', responseText: '<dom><stuff/></dom>' });
|
||||
|
||||
if (typeof window.Document !== 'undefined') {
|
||||
expect(request.responseXML).toEqual(jasmine.any(window.Document));
|
||||
} else {
|
||||
// IE 8
|
||||
expect(request.responseXML).toEqual(jasmine.any(window.ActiveXObject));
|
||||
}
|
||||
});
|
||||
|
||||
it('parses a text/html document into responseXML', function() {
|
||||
var request = new this.FakeRequest();
|
||||
request.open();
|
||||
request.send();
|
||||
|
||||
request.response({ status: 200, contentType: 'text/html', responseText: '<dom><stuff/></dom>' });
|
||||
|
||||
if (typeof window.Document !== 'undefined') {
|
||||
expect(request.responseXML).toEqual(jasmine.any(window.Document));
|
||||
} else {
|
||||
// IE 8
|
||||
expect(request.responseXML).toEqual(jasmine.any(window.ActiveXObject));
|
||||
}
|
||||
});
|
||||
|
||||
it('parses a custom blah+xml document into responseXML', function() {
|
||||
var request = new this.FakeRequest();
|
||||
request.open();
|
||||
request.send();
|
||||
|
||||
request.response({ status: 200, contentType: 'application/text+xml', responseText: '<dom><stuff/></dom>' });
|
||||
|
||||
if (typeof window.Document !== 'undefined') {
|
||||
expect(request.responseXML).toEqual(jasmine.any(window.Document));
|
||||
} else {
|
||||
// IE 8
|
||||
expect(request.responseXML).toEqual(jasmine.any(window.ActiveXObject));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -9,7 +9,11 @@ describe("Jasmine Mock Ajax (for toplevel)", function() {
|
||||
|
||||
beforeEach(function() {
|
||||
var fakeXMLHttpRequest = jasmine.createSpy('realFakeXMLHttpRequest');
|
||||
fakeGlobal = {XMLHttpRequest: fakeXMLHttpRequest};
|
||||
fakeGlobal = {
|
||||
XMLHttpRequest: fakeXMLHttpRequest,
|
||||
DOMParser: window.DOMParser,
|
||||
ActiveXObject: window.ActiveXObject
|
||||
};
|
||||
mockAjax = new window.MockAjax(fakeGlobal);
|
||||
mockAjax.install();
|
||||
|
||||
|
@ -54,6 +54,28 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
function parseXml(xmlText, contentType) {
|
||||
if (global.DOMParser) {
|
||||
return (new global.DOMParser()).parseFromString(xmlText, contentType);
|
||||
} else {
|
||||
var xml = new global.ActiveXObject("Microsoft.XMLDOM");
|
||||
xml.async = "false";
|
||||
xml.loadXML(xmlText);
|
||||
return xml;
|
||||
}
|
||||
}
|
||||
|
||||
var xmlParsables = ['text/html', 'text/xml', 'application/xml'];
|
||||
|
||||
function getResponseXml(responseText, contentType) {
|
||||
if (xmlParsables.indexOf(contentType.toLowerCase()) >= 0) {
|
||||
return parseXml(responseText, contentType);
|
||||
} else if (contentType.match(/\+xml$/)) {
|
||||
return parseXml(responseText, 'text/xml');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout'];
|
||||
extend(FakeXMLHttpRequest.prototype, new global.XMLHttpRequest(), iePropertiesThatCannotBeCopied);
|
||||
extend(FakeXMLHttpRequest.prototype, {
|
||||
@ -154,6 +176,7 @@ getJasmineRequireObj().AjaxFakeRequest = function() {
|
||||
this.responseText = response.responseText || "";
|
||||
this.readyState = 4;
|
||||
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType);
|
||||
this.responseXML = getResponseXml(response.responseText, this.getResponseHeader('content-type') || '');
|
||||
|
||||
this.onload();
|
||||
this.onreadystatechange();
|
||||
|
Loading…
Reference in New Issue
Block a user