Skip to content

Commit 0ce3aa0

Browse files
authored
* Fix MithrilJS#2419 * Update the changelog
1 parent 0306401 commit 0ce3aa0

File tree

3 files changed

+72
-14
lines changed

3 files changed

+72
-14
lines changed

docs/change-log.md

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
- route, request: Interpolated arguments are *not* appended to the query string. This means `m.request({url: "/api/user/:id/get", params: {id: user.id}})` would result in a request like `GET /api/user/1/get`, not one like `GET /api/user/1/get?id=1`. If you really need it in both places, pass the same value via two separate parameters with the non-query-string parameter renamed, like in `m.request({url: "/api/user/:urlID/get", params: {id: user.id, urlID: user.id}})`. ([#2361](https://github.com/MithrilJS/mithril.js/pull/2361))
4141
- route, request: `m.route.set`, `m.request`, and `m.jsonp` all use the same path template syntax now, and vary only in how they receive their parameters. Furthermore, declared routes in `m.route` shares the same syntax and semantics, but acts in reverse as if via pattern matching. ([#2361](https://github.com/MithrilJS/mithril.js/pull/2361))
4242
- request: `options.responseType` now defaults to `"json"` if `extract` is absent, and `deserialize` receives the parsed response, not the raw string. If you want the old behavior, [use `responseType: "text"`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/responseType). ([#2335](https://github.com/MithrilJS/mithril.js/pull/2335))
43+
- request: set `Content-Type: application/json; charset=utf-8` for all XHR methods by default, provided they have a body that's `!= null` ([#2361](https://github.com/MithrilJS/mithril.js/pull/2361), [#2421](https://github.com/MithrilJS/mithril.js/pull/2421))
44+
- This can cause CORS issues when issuing `GET` with bodies, but you can address them through configuring CORS appropriately.
45+
- Previously, it was only set for all non-`GET` methods and only when `useBody: true` was passed (the default), and it was always set for them. Now it's automatically omitted when no body is present, so the hole is slightly broadened.
4346

4447
#### News
4548

request/request.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ module.exports = function($window, Promise) {
7171

7272
xhr.open(method, url, args.async !== false, typeof args.user === "string" ? args.user : undefined, typeof args.password === "string" ? args.password : undefined)
7373

74-
if (assumeJSON && !hasHeader(args, /^content-type$/i)) {
74+
if (assumeJSON && body != null && !hasHeader(args, /^content-type$/i)) {
7575
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8")
7676
}
7777
if (typeof args.deserialize !== "function" && !hasHeader(args, /^accept$/i)) {

request/tests/test-request.js

+68-13
Original file line numberDiff line numberDiff line change
@@ -434,19 +434,6 @@ o.spec("xhr", function() {
434434
o(xhr.getRequestHeader("Content-Type")).equals("Value")
435435
}
436436
})
437-
o("json headers are set to the correct default value", function(done) {
438-
mock.$defineRoutes({
439-
"POST /item": function() {
440-
return {status: 200, responseText: ""}
441-
}
442-
})
443-
xhr({method: "POST", url: "/item", config: config}).then(done)
444-
445-
function config(xhr) {
446-
o(xhr.getRequestHeader("Content-Type")).equals("application/json; charset=utf-8")
447-
o(xhr.getRequestHeader("Accept")).equals("application/json, text/*")
448-
}
449-
})
450437
o("doesn't fail on abort", function(done) {
451438
mock.$defineRoutes({
452439
"GET /item": function() {
@@ -640,4 +627,72 @@ o.spec("xhr", function() {
640627
})
641628
})
642629
})
630+
o.spec("json header", function() {
631+
function checkUnset(method) {
632+
o("doesn't set header on " + method + " without body", function(done) {
633+
var routes = {}
634+
routes[method + " /item"] = function() {
635+
return {status: 200, responseText: JSON.stringify({a: 1})}
636+
}
637+
mock.$defineRoutes(routes)
638+
xhr({
639+
method: method, url: "/item",
640+
config: function(xhr) {
641+
var header = xhr.getRequestHeader("Content-Type")
642+
o(header).equals(undefined)
643+
header = xhr.getRequestHeader("Accept")
644+
o(header).equals("application/json, text/*")
645+
}
646+
}).then(function(result) {
647+
o(result).deepEquals({a: 1})
648+
done()
649+
}).catch(function(e) {
650+
done(e)
651+
})
652+
})
653+
}
654+
655+
function checkSet(method, body) {
656+
o("sets header on " + method + " with body", function(done) {
657+
var routes = {}
658+
routes[method + " /item"] = function(response) {
659+
return {
660+
status: 200,
661+
responseText: JSON.stringify({body: JSON.parse(response.body)}),
662+
}
663+
}
664+
mock.$defineRoutes(routes)
665+
xhr({
666+
method: method, url: "/item", body: body,
667+
config: function(xhr) {
668+
var header = xhr.getRequestHeader("Content-Type")
669+
o(header).equals("application/json; charset=utf-8")
670+
header = xhr.getRequestHeader("Accept")
671+
o(header).equals("application/json, text/*")
672+
}
673+
}).then(function(result) {
674+
o(result).deepEquals({body: body})
675+
done()
676+
}).catch(function(e) {
677+
done(e)
678+
})
679+
})
680+
}
681+
682+
checkUnset("GET")
683+
checkUnset("HEAD")
684+
checkUnset("OPTIONS")
685+
checkUnset("POST")
686+
checkUnset("PUT")
687+
checkUnset("DELETE")
688+
checkUnset("PATCH")
689+
690+
checkSet("GET", {foo: "bar"})
691+
checkSet("HEAD", {foo: "bar"})
692+
checkSet("OPTIONS", {foo: "bar"})
693+
checkSet("POST", {foo: "bar"})
694+
checkSet("PUT", {foo: "bar"})
695+
checkSet("DELETE", {foo: "bar"})
696+
checkSet("PATCH", {foo: "bar"})
697+
})
643698
})

0 commit comments

Comments
 (0)