From 1ec196e52526c413c9a21bf036c4f67f253fa251 Mon Sep 17 00:00:00 2001 From: slackersoft Date: Wed, 30 Jul 2014 18:46:02 -0700 Subject: [PATCH] refactor response headers normalization into it's own method --- lib/mock-ajax.js | 59 ++++++++++++--------- spec/javascripts/mock-ajax-toplevel-spec.js | 2 +- spec/javascripts/webmock-style-spec.js | 2 +- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index f359ec5..95bdad0 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -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(); diff --git a/spec/javascripts/mock-ajax-toplevel-spec.js b/spec/javascripts/mock-ajax-toplevel-spec.js index 33c2d89..dde7c31 100644 --- a/spec/javascripts/mock-ajax-toplevel-spec.js +++ b/spec/javascripts/mock-ajax-toplevel-spec.js @@ -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; diff --git a/spec/javascripts/webmock-style-spec.js b/spec/javascripts/webmock-style-spec.js index 26c09ce..57d5ecf 100644 --- a/spec/javascripts/webmock-style-spec.js +++ b/spec/javascripts/webmock-style-spec.js @@ -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() {