Allow Request Headers to be set via config (#1464)

Fixes #1463.
This commit is contained in:
cetra3 2016-12-30 17:03:13 +10:30 committed by Isiah Meadows
parent b42c3d1ba0
commit 7fc0e0378c
3 changed files with 39 additions and 0 deletions

5
mithril.d.ts vendored
View file

@ -840,6 +840,11 @@ declare namespace Mithril {
*/
password?: string;
/**
* An optional object map of headers with their respective values
*/
headers?: {[key: string]: string};
/**
* An optional function to run between `open` and `send`, useful for
* adding request headers or using XHR2 features such as the `upload`

View file

@ -2171,6 +2171,14 @@
if (maybeXhr != null) xhr = maybeXhr
}
if (isObject(options.headers)) {
for (var header in options.headers) {
if (hasOwn.call(options.headers, header)) {
xhr.setRequestHeader(header, options.headers[header])
}
}
}
var data = options.method === "GET" || !options.data ? "" : options.data
if (data && !isString(data) && data.constructor !== global.FormData) {

View file

@ -146,6 +146,32 @@ describe("m.request()", function () {
expect(xhr.$headers).to.not.have.property("Content-Type")
})
it("sets xhr request headers as per the headers config", function () {
var error = m.prop()
m.request({
method: "POST",
url: "test",
headers: {
"Authorization" : "Bearer 12345abcd12345",
"CustomHeader" : "CustomValue"
}
}).then(null, error)
var xhr = mock.XMLHttpRequest.$instances.pop()
xhr.onreadystatechange()
expect(xhr.$headers).to.have.property(
"Authorization",
"Bearer 12345abcd12345")
expect(xhr.$headers).to.have.property(
"CustomHeader",
"CustomValue")
})
it("correctly sets initial value", function () {
var prop = m.request({
method: "POST",