From 62bc8a5481b16ab0d67fdfb1c11b866ee8d56ab3 Mon Sep 17 00:00:00 2001 From: tkilliansc Date: Thu, 3 Aug 2017 13:48:19 -0700 Subject: [PATCH] fix issue 1919 --- docs/change-log.md | 11 +++++++++-- request/request.js | 4 ++-- test-utils/tests/test-xhrMock.js | 8 ++++++++ test-utils/xhrMock.js | 12 +++++++++++- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/docs/change-log.md b/docs/change-log.md index 74531135..89cbe17c 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -1,5 +1,7 @@ # Change log +- [v1.1.5](#v115) +- [v1.1.4](#v114) - [v1.1.3](#v113) - [v1.1.2](#v112) - [v1.1.1](#v111) @@ -10,11 +12,16 @@ --- +### v1.1.5 + +- 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)) + +--- + ### v1.1.4 #### Bug fixes: -- Fix IE bug where active element is null causing render function to throw error. ([1943](https://github.com/MithrilJS/mithril.js/pull/1943)) - +- Fix IE bug where active element is null causing render function to throw error. ([1943] --- ### v1.1.3 diff --git a/request/request.js b/request/request.js index 76e218c9..84f5e4ce 100644 --- a/request/request.js +++ b/request/request.js @@ -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) - 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") } - if (args.deserialize === deserialize) { + if (args.deserialize === deserialize && !(args.headers && args.headers.hasOwnProperty("Accept"))) { xhr.setRequestHeader("Accept", "application/json, text/*") } if (args.withCredentials) xhr.withCredentials = args.withCredentials diff --git a/test-utils/tests/test-xhrMock.js b/test-utils/tests/test-xhrMock.js index 8bdfcf21..8c64a308 100644 --- a/test-utils/tests/test-xhrMock.js +++ b/test-utils/tests/test-xhrMock.js @@ -72,6 +72,14 @@ o.spec("xhrMock", function() { } 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("works", function(done) { diff --git a/test-utils/xhrMock.js b/test-utils/xhrMock.js index d3bbf5b8..379bce28 100644 --- a/test-utils/xhrMock.js +++ b/test-utils/xhrMock.js @@ -17,7 +17,17 @@ module.exports = function() { var headers = {} var aborted = false this.setRequestHeader = function(header, value) { - headers[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 + } } this.getRequestHeader = function(header) { return headers[header]