Find response case-insensitively

This commit is contained in:
slackersoft 2014-07-29 08:08:03 -07:00
parent 0b2b4a4a2e
commit 864c8df82b
2 changed files with 26 additions and 7 deletions

View File

@ -172,6 +172,15 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
this.requestHeaders = {}; 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']; var iePropertiesThatCannotBeCopied = ['responseBody', 'responseText', 'responseXML', 'status', 'statusText', 'responseTimeout'];
extend(FakeXMLHttpRequest.prototype, new window.XMLHttpRequest(), iePropertiesThatCannotBeCopied); extend(FakeXMLHttpRequest.prototype, new window.XMLHttpRequest(), iePropertiesThatCannotBeCopied);
extend(FakeXMLHttpRequest.prototype, { extend(FakeXMLHttpRequest.prototype, {
@ -221,11 +230,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}, },
contentType: function() { contentType: function() {
for (var header in this.requestHeaders) { return findHeader('content-type', this.requestHeaders);
if (header.toLowerCase() === 'content-type') {
return this.requestHeaders[header];
}
}
}, },
data: function() { data: function() {
@ -237,7 +242,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}, },
getResponseHeader: function(name) { getResponseHeader: function(name) {
return this.responseHeaders[name]; return findHeader(name, this.responseHeaders);
}, },
getAllResponseHeaders: function() { getAllResponseHeaders: function() {

View File

@ -219,12 +219,26 @@ describe("FakeXMLHttpRequest", function() {
expect(xhr.contentType()).toEqual('something'); 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'); xhr.setRequestHeader('content-Type', 'some other thing');
expect(xhr.contentType()).toEqual('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() { describe("when a fake XMLHttpRequest is created", function() {
it("inherits the properties of the real XMLHttpRequest object", function() { it("inherits the properties of the real XMLHttpRequest object", function() {
expect(xhr.someOtherProperty).toBe('someValue'); expect(xhr.someOtherProperty).toBe('someValue');