From 8942ee6fb733c9b9ea796b34a4313e2fddb17d76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Sta=C5=9Bkiewicz?= Date: Mon, 4 Nov 2019 22:18:35 +0100 Subject: [PATCH] [BUGFIX adapter]: Fix problem with headers precedence #6588 (#6650) --- .../rest-adapter/ajax-options-test.js | 27 +++++++++++++++++++ packages/adapter/addon/rest.js | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/-ember-data/tests/unit/adapters/rest-adapter/ajax-options-test.js b/packages/-ember-data/tests/unit/adapters/rest-adapter/ajax-options-test.js index 39ed014c433..44d7a7edab5 100644 --- a/packages/-ember-data/tests/unit/adapters/rest-adapter/ajax-options-test.js +++ b/packages/-ember-data/tests/unit/adapters/rest-adapter/ajax-options-test.js @@ -194,6 +194,33 @@ module('unit/adapters/rest-adapter/ajax-options - building requests', function(h }); }); + test('ajaxOptions() headers take precedence over adapter headers', function(assert) { + let store = this.owner.lookup('service:store'); + let adapter = store.adapterFor('application'); + + adapter.headers = { + 'Content-Type': 'application/x-www-form-urlencoded', + }; + + let url = 'example.com'; + let type = 'POST'; + let ajaxOptions = adapter.ajaxOptions(url, type, { + headers: { + 'Content-Type': 'application/json', + }, + }); + + assert.deepEqual(ajaxOptions, { + credentials: 'same-origin', + type: 'POST', + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + url: 'example.com', + }); + }); + test('_fetchRequest() returns a promise', function(assert) { let store = this.owner.lookup('service:store'); let adapter = store.adapterFor('application'); diff --git a/packages/adapter/addon/rest.js b/packages/adapter/addon/rest.js index 4e67d528354..f4428faf141 100644 --- a/packages/adapter/addon/rest.js +++ b/packages/adapter/addon/rest.js @@ -1086,7 +1086,7 @@ const RESTAdapter = Adapter.extend(BuildURLMixin, { let headers = get(this, 'headers'); if (headers !== undefined) { - options.headers = assign({}, options.headers, headers); + options.headers = assign({}, headers, options.headers); } else if (!options.headers) { options.headers = {}; }