fix issue 1919

This commit is contained in:
tkilliansc 2017-08-03 13:48:19 -07:00 committed by tskillian
parent 1da69da4dd
commit b4856a9ecd
4 changed files with 22 additions and 3 deletions

View file

@ -24,6 +24,7 @@
#### Bug fixes #### Bug fixes
- API: `m.route.set()` causes all mount points to be redrawn ([#1592](https://github.com/MithrilJS/mithril.js/pull/1592)) - API: `m.route.set()` causes all mount points to be redrawn ([#1592](https://github.com/MithrilJS/mithril.js/pull/1592))
- API: If a user sets the Content-Type header within a request's options, that value will be the entire header value rather than being appended to the default value ([#1924](https://github.com/MithrilJS/mithril.js/pull/1924))
--- ---

View file

@ -67,10 +67,10 @@ module.exports = function($window, Promise) {
xhr.open(args.method, args.url, typeof args.async === "boolean" ? args.async : true, typeof args.user === "string" ? args.user : undefined, typeof args.password === "string" ? args.password : undefined) xhr.open(args.method, args.url, typeof args.async === "boolean" ? args.async : true, typeof args.user === "string" ? args.user : undefined, typeof args.password === "string" ? args.password : undefined)
if (args.serialize === JSON.stringify && useBody) { if (args.serialize === JSON.stringify && useBody && !(args.headers && args.headers.hasOwnProperty("Content-Type"))) {
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8") xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8")
} }
if (args.deserialize === deserialize) { if (args.deserialize === deserialize && !(args.headers && args.headers.hasOwnProperty("Accept"))) {
xhr.setRequestHeader("Accept", "application/json, text/*") xhr.setRequestHeader("Accept", "application/json, text/*")
} }
if (args.withCredentials) xhr.withCredentials = args.withCredentials if (args.withCredentials) xhr.withCredentials = args.withCredentials

View file

@ -72,6 +72,14 @@ o.spec("xhrMock", function() {
} }
xhr.send("a=b") xhr.send("a=b")
}) })
o("Setting a header twice merges the header", function() {
// Source: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader
var xhr = new $window.XMLHttpRequest()
xhr.open("POST", "/test")
xhr.setRequestHeader("Content-Type", "foo")
xhr.setRequestHeader("Content-Type", "bar")
o(xhr.getRequestHeader("Content-Type")).equals("foo, bar")
})
}) })
o.spec("jsonp", function() { o.spec("jsonp", function() {
o("works", function(done) { o("works", function(done) {

View file

@ -17,8 +17,18 @@ module.exports = function() {
var headers = {} var headers = {}
var aborted = false var aborted = false
this.setRequestHeader = function(header, value) { this.setRequestHeader = function(header, value) {
/*
the behavior of setHeader is not your expected setX API.
If the header is already set, it'll merge with whatever you add
rather than overwrite
Source: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/setRequestHeader
*/
if (headers[header]) {
headers[header] += ", " + value;
} else {
headers[header] = value headers[header] = value
} }
}
this.getRequestHeader = function(header) { this.getRequestHeader = function(header) {
return headers[header] return headers[header]
} }