diff --git a/lib/mock-ajax.js b/lib/mock-ajax.js index ae66b0d..07020aa 100644 --- a/lib/mock-ajax.js +++ b/lib/mock-ajax.js @@ -172,6 +172,15 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. this.requestHeaders = {}; } + function findHeader(name, headers) { + name = name.toLowerCase(); + for (var header in headers) { + if (header.toLowerCase() === name) { + return headers[header]; + } + } + } + var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout']; extend(FakeXMLHttpRequest.prototype, new window.XMLHttpRequest(), iePropertiesThatCannotBeCopied); extend(FakeXMLHttpRequest.prototype, { @@ -221,11 +230,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }, contentType: function() { - for (var header in this.requestHeaders) { - if (header.toLowerCase() === 'content-type') { - return this.requestHeaders[header]; - } - } + return findHeader('content-type', this.requestHeaders); }, data: function() { @@ -237,7 +242,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }, getResponseHeader: function(name) { - return this.responseHeaders[name]; + return findHeader(name, this.responseHeaders); }, getAllResponseHeaders: function() { diff --git a/spec/javascripts/fake-xml-http-request-spec.js b/spec/javascripts/fake-xml-http-request-spec.js index 8f863a3..9f07891 100644 --- a/spec/javascripts/fake-xml-http-request-spec.js +++ b/spec/javascripts/fake-xml-http-request-spec.js @@ -219,12 +219,26 @@ describe("FakeXMLHttpRequest", function() { expect(xhr.contentType()).toEqual('something'); }); - it("gets the content-type even when the casing is not to spec", function() { + it("gets the content-type case-insensitively", function() { xhr.setRequestHeader('content-Type', 'some other thing'); expect(xhr.contentType()).toEqual('some other thing'); }); }); + describe("getResponseHeader", function() { + it("gets a response header case-insensitively", function() { + xhr.send(); + xhr.response({ + status: 200, + responseHeaders: { + 'X-Foo': 'Bar' + } + }); + + expect(xhr.getResponseHeader('x-foo')).toBe('Bar'); + }); + }); + describe("when a fake XMLHttpRequest is created", function() { it("inherits the properties of the real XMLHttpRequest object", function() { expect(xhr.someOtherProperty).toBe('someValue');