refactor response headers normalization into it's own method

master
slackersoft 10 years ago
parent 2790018e76
commit 1ec196e525

@ -182,6 +182,26 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}
}
function normalizeHeaders(rawHeaders, contentType) {
var headers = [];
if (rawHeaders) {
if (rawHeaders instanceof Array) {
headers = rawHeaders;
} else {
for (var headerName in rawHeaders) {
if (rawHeaders.hasOwnProperty(headerName)) {
headers.push({ name: headerName, value: rawHeaders[headerName] });
}
}
}
} else {
headers.push({ name: "Content-Type", value: contentType || "application/json" });
}
return headers;
}
var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout'];
extend(FakeXMLHttpRequest.prototype, new window.XMLHttpRequest(), iePropertiesThatCannotBeCopied);
extend(FakeXMLHttpRequest.prototype, {
@ -247,7 +267,19 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
},
getResponseHeader: function(name) {
return findHeader(name, this.responseHeaders);
name = name.toLowerCase();
var resultHeader;
for(var i = 0; i < this.responseHeaders.length; i++) {
var header = this.responseHeaders[i];
if (name === header.name.toLowerCase()) {
if (resultHeader) {
resultHeader = [resultHeader, header.value].join(', ');
} else {
resultHeader = header.value;
}
}
}
return resultHeader;
},
getAllResponseHeaders: function() {
@ -269,30 +301,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
this.statusText = response.statusText || "";
this.responseText = response.responseText || "";
this.readyState = 4;
this.responseHeaders = [];
var i;
if (response.responseHeaders) {
if (response.responseHeaders instanceof Array) {
this.responseHeaders = response.responseHeaders;
} else {
for (i in response.responseHeaders) {
if (response.responseHeaders.hasOwnProperty(i)) {
this.responseHeaders.push({ name: i, value: response.responseHeaders[i] });
}
}
}
} else {
this.responseHeaders.push({ name: "Content-Type", value: response.contentType || "application/json" });
}
for (i = 0; i < this.responseHeaders.length; i++) {
if (typeof(this.responseHeaders[this.responseHeaders[i].name]) === 'undefined') {
this.responseHeaders[this.responseHeaders[i].name] = this.responseHeaders[i].value;
} else {
this.responseHeaders[this.responseHeaders[i].name] += ', ' + this.responseHeaders[i].value;
}
}
this.responseHeaders = normalizeHeaders(response.responseHeaders, response.contentType);
this.onload();
this.onreadystatechange();

@ -20,7 +20,7 @@ describe("Jasmine Mock Ajax (for toplevel)", function() {
onreadystatechange = function() {
if (this.readyState === (this.DONE || 4)) { // IE 8 doesn't support DONE
if (this.status === 200) {
if (this.responseHeaders['Content-Type'] === 'application/json') {
if (this.getResponseHeader('Content-Type') === 'application/json') {
this.response = JSON.parse(this.responseText);
} else {
this.response = this.responseText;

@ -38,7 +38,7 @@ describe("Webmock style mocking", function() {
it("should set the contentType", function() {
sendRequest(fakeGlobal);
expect(response.responseHeaders['Content-Type']).toEqual('application/json');
expect(response.getResponseHeader('Content-Type')).toEqual('application/json');
});
it("should set the responseText", function() {

Loading…
Cancel
Save