Skip to content

Commit

Permalink
Merge pull request #2 from cibernox/fix-headers-of-ED-mixin
Browse files Browse the repository at this point in the history
Fix headers of Ember-Data Mixin
  • Loading branch information
nlfurniss authored Jun 13, 2017
2 parents d0e600b + 5f48c57 commit c5396e9
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 15 deletions.
12 changes: 8 additions & 4 deletions addon/mixins/adapter-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,20 @@ export function headersToObject(headers) {
* Helper function that translates the options passed to `jQuery.ajax` into a format that `fetch` expects.
* @param {Object} options
*/
export function mungOptionsForFetch(_options) {
export function mungOptionsForFetch(_options, adapter) {
const options = (assign || merge)({
credentials: 'same-origin',
}, _options);

let adapterHeaders = adapter.get('headers');
if (adapterHeaders) {
options.headers = assign({}, options.headers || {}, adapterHeaders);
}
options.method = options.type;

// Mimics the default behavior in Ember Data's `ajaxOptions`
if (options.headers && (!options.headers['Content-Type'] || !options.headers['content-type'])) {
if (options.headers === undefined || !(options.headers['Content-Type'] || options.headers['content-type'])) {
options.headers = options.headers || {};
options.headers['Content-Type'] = 'application/json; charset=utf-8';
}

Expand Down Expand Up @@ -137,8 +142,7 @@ export default Ember.Mixin.create({
* @override
*/
_ajaxRequest(options) {
const _options = mungOptionsForFetch(options);

const _options = mungOptionsForFetch(options, this);
return fetch(_options.url, _options);
},

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"ember-cli-release": "^0.2.9",
"ember-cli-shims": "^1.1.0",
"ember-cli-uglify": "^1.2.0",
"ember-data": "^2.13.1",
"ember-load-initializers": "^1.0.0",
"ember-resolver": "^4.0.0",
"ember-source": "~2.13.0",
Expand Down
89 changes: 83 additions & 6 deletions tests/unit/mixins/adapter-fetch-test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import Ember from 'ember';
import DS from 'ember-data';
import { module, test } from 'qunit';
import { Headers } from 'fetch';
import AdapterFetchMixin, {
mungOptionsForFetch,
headersToObject,
serialiazeQueryParams
} from 'ember-fetch/mixins/adapter-fetch';
const { JSONAPIAdapter } = DS;


module('Unit | Mixin | adapter-fetch', {
beforeEach() {
this.subject = Ember.Object.extend(AdapterFetchMixin).create();
this.JSONAPIAdapter = JSONAPIAdapter.extend(AdapterFetchMixin, {
headers: {
'custom-header' : 'foo',
'Content-Type': 'application/vnd.api+json'
}
}).create();
this.basicAdapter = Ember.Object.extend(AdapterFetchMixin).create();
}
});

Expand All @@ -20,15 +29,16 @@ test('mungOptionsForFetch transforms jQuery-style options into fetch compatible
url: 'https://emberjs.com',
type: 'GET',
headers: {
'x-fake-header': 13
'x-fake-header': 13,
"Content-Type": 'application/vnd.api+json'
},
data: {
a: 1,
b: 2
}
};

const fetchGetOptions = mungOptionsForFetch(jQueryGetOptions);
const fetchGetOptions = mungOptionsForFetch(jQueryGetOptions, this.JSONAPIAdapter);

assert.deepEqual(fetchGetOptions, {
credentials: 'same-origin',
Expand All @@ -37,7 +47,8 @@ test('mungOptionsForFetch transforms jQuery-style options into fetch compatible
type: 'GET',
headers: {
'x-fake-header': 13,
"Content-Type": "application/json; charset=utf-8"
'custom-header' : 'foo',
'Content-Type': 'application/vnd.api+json'
},
data: {
a: 1,
Expand All @@ -51,13 +62,16 @@ test('mungOptionsForFetch transforms jQuery-style options into fetch compatible
data: { a: 1 }
};

const fetchPostOptions = mungOptionsForFetch(jqueryPostOptions);

const fetchPostOptions = mungOptionsForFetch(jqueryPostOptions, this.JSONAPIAdapter);
assert.deepEqual(fetchPostOptions, {
credentials: 'same-origin',
url: 'https://emberjs.com',
method: 'POST',
type: 'POST',
headers: {
'custom-header' : 'foo',
'Content-Type': 'application/vnd.api+json'
},
body: {
a: 1
},
Expand All @@ -67,6 +81,69 @@ test('mungOptionsForFetch transforms jQuery-style options into fetch compatible
}, 'POST call\'s options are correct');
});

test('mungOptionsForFetch adds a default "Content-Type" header if none is present', function(assert) {
assert.expect(2);
const optionsNoHeaders = {
url: 'https://emberjs.com',
type: 'POST',
data: { a: 1 }
};
const optionsHeadersWithoutContentType = {
url: 'https://emberjs.com',
type: 'POST',
headers: {
'X-foo': 'bar'
},
data: { a: 1 }
};
let options = mungOptionsForFetch(optionsNoHeaders, this.basicAdapter);
assert.deepEqual(options.headers, {
'Content-Type': 'application/json; charset=utf-8'
}, 'POST call\'s options are correct');

options = mungOptionsForFetch(optionsHeadersWithoutContentType, this.basicAdapter);
assert.deepEqual(options.headers, {
'Content-Type': 'application/json; charset=utf-8',
'X-foo': 'bar'
}, 'POST call\'s options are correct');
});

test('mungOptionsForFetch respects the "Content-Type" if present', function(assert) {
assert.expect(1);
const optionsHeadersWithContentType = {
url: 'https://emberjs.com',
type: 'POST',
headers: {
'Content-Type': 'application/special-type',
},
data: { a: 1 }
};

let options = mungOptionsForFetch(optionsHeadersWithContentType, this.basicAdapter);
assert.deepEqual(options.headers, {
'Content-Type': 'application/special-type',
}, 'POST call\'s options are correct');
});

test('mungOptionsForFetch takes the headers from the adapter if present', function(assert) {
assert.expect(1);
const optionsHeadersWithoutContentType = {
url: 'https://emberjs.com',
type: 'POST',
headers: {
'X-foo': 'bar'
},
data: { a: 1 }
};

let options = mungOptionsForFetch(optionsHeadersWithoutContentType, this.JSONAPIAdapter);
assert.deepEqual(options.headers, {
'Content-Type': 'application/vnd.api+json',
'X-foo': 'bar',
'custom-header' : 'foo',
}, 'POST call\'s options are correct');
});

test('headersToObject turns an Headers instance into an object', function (assert) {
assert.expect(1);

Expand Down
98 changes: 93 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ align-text@^0.1.1, align-text@^0.1.3:
longest "^1.0.1"
repeat-string "^1.5.2"

[email protected]:
version "0.0.5"
resolved "https://registry.yarnpkg.com/amd-name-resolver/-/amd-name-resolver-0.0.5.tgz#76962dac876ed3311b05d29c6a58c14e1ef3304b"
dependencies:
ensure-posix-path "^1.0.1"

[email protected]:
version "0.0.6"
resolved "https://registry.yarnpkg.com/amd-name-resolver/-/amd-name-resolver-0.0.6.tgz#d3e4ba2dfcaab1d820c1be9de947c67828cfe595"
Expand Down Expand Up @@ -464,6 +470,14 @@ babel-plugin-debug-macros@^0.1.1, babel-plugin-debug-macros@^0.1.6:
dependencies:
semver "^5.3.0"

babel-plugin-feature-flags@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/babel-plugin-feature-flags/-/babel-plugin-feature-flags-0.3.1.tgz#9c827cf9a4eb9a19f725ccb239e85cab02036fc1"

babel-plugin-filter-imports@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/babel-plugin-filter-imports/-/babel-plugin-filter-imports-0.3.1.tgz#e7859b56886b175dd2616425d277b219e209ea8b"

babel-plugin-syntax-async-functions@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95"
Expand Down Expand Up @@ -768,6 +782,14 @@ babel-types@^6.19.0, babel-types@^6.24.1:
lodash "^4.2.0"
to-fast-properties "^1.0.1"

babel6-plugin-strip-class-callcheck@^6.0.0:
version "6.0.0"
resolved "https://registry.yarnpkg.com/babel6-plugin-strip-class-callcheck/-/babel6-plugin-strip-class-callcheck-6.0.0.tgz#de841c1abebbd39f78de0affb2c9a52ee228fddf"

babel6-plugin-strip-heimdall@^6.0.1:
version "6.0.1"
resolved "https://registry.yarnpkg.com/babel6-plugin-strip-heimdall/-/babel6-plugin-strip-heimdall-6.0.1.tgz#35f80eddec1f7fffdc009811dfbd46d9965072b6"

babylon@^6.11.0, babylon@^6.15.0:
version "6.17.1"
resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.17.1.tgz#17f14fddf361b695981fe679385e4f1c01ebd86f"
Expand Down Expand Up @@ -989,6 +1011,17 @@ broccoli-debug@^0.6.1:
sanitize-filename "^1.6.1"
tree-sync "^1.2.2"

broccoli-file-creator@^1.0.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/broccoli-file-creator/-/broccoli-file-creator-1.1.1.tgz#1b35b67d215abdfadd8d49eeb69493c39e6c3450"
dependencies:
broccoli-kitchen-sink-helpers "~0.2.0"
broccoli-plugin "^1.1.0"
broccoli-writer "~0.1.1"
mkdirp "^0.5.1"
rsvp "~3.0.6"
symlink-or-copy "^1.0.1"

broccoli-filter@^0.1.11:
version "0.1.14"
resolved "https://registry.yarnpkg.com/broccoli-filter/-/broccoli-filter-0.1.14.tgz#23cae3891ff9ebb7b4d7db00c6dcf03535daf7ad"
Expand Down Expand Up @@ -1039,7 +1072,7 @@ broccoli-funnel@^1.0.0, broccoli-funnel@^1.0.1, broccoli-funnel@^1.0.6, broccoli
symlink-or-copy "^1.0.0"
walk-sync "^0.3.1"

broccoli-kitchen-sink-helpers@^0.2.5, broccoli-kitchen-sink-helpers@^0.2.6:
broccoli-kitchen-sink-helpers@^0.2.5, broccoli-kitchen-sink-helpers@^0.2.6, broccoli-kitchen-sink-helpers@~0.2.0:
version "0.2.9"
resolved "https://registry.yarnpkg.com/broccoli-kitchen-sink-helpers/-/broccoli-kitchen-sink-helpers-0.2.9.tgz#a5e0986ed8d76fb5984b68c3f0450d3a96e36ecc"
dependencies:
Expand Down Expand Up @@ -1119,7 +1152,7 @@ [email protected]:
rimraf "^2.3.4"
symlink-or-copy "^1.0.1"

broccoli-plugin@^1.0.0, broccoli-plugin@^1.2.0, broccoli-plugin@^1.2.1, broccoli-plugin@^1.3.0:
broccoli-plugin@^1.0.0, broccoli-plugin@^1.1.0, broccoli-plugin@^1.2.0, broccoli-plugin@^1.2.1, broccoli-plugin@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/broccoli-plugin/-/broccoli-plugin-1.3.0.tgz#bee704a8e42da08cb58e513aaa436efb7f0ef1ee"
dependencies:
Expand Down Expand Up @@ -1179,7 +1212,7 @@ broccoli-uglify-sourcemap@^1.0.0:
uglify-js "^2.7.0"
walk-sync "^0.1.3"

broccoli-writer@^0.1.1:
broccoli-writer@^0.1.1, broccoli-writer@~0.1.1:
version "0.1.1"
resolved "https://registry.yarnpkg.com/broccoli-writer/-/broccoli-writer-0.1.1.tgz#d4d71aa8f2afbc67a3866b91a2da79084b96ab2d"
dependencies:
Expand Down Expand Up @@ -1960,6 +1993,40 @@ [email protected]:
walk-sync "^0.3.0"
yam "0.0.22"

ember-data@^2.13.1:
version "2.13.1"
resolved "https://registry.yarnpkg.com/ember-data/-/ember-data-2.13.1.tgz#fd85daf3c4c7bfe6a0c2e42cedf72048979f94ae"
dependencies:
amd-name-resolver "0.0.5"
babel-plugin-feature-flags "^0.3.1"
babel-plugin-filter-imports "^0.3.1"
babel6-plugin-strip-class-callcheck "^6.0.0"
babel6-plugin-strip-heimdall "^6.0.1"
broccoli-babel-transpiler "^6.0.0"
broccoli-file-creator "^1.0.0"
broccoli-merge-trees "^1.0.0"
chalk "^1.1.1"
ember-cli-babel "^6.0.0-beta.7"
ember-cli-path-utils "^1.0.0"
ember-cli-string-utils "^1.0.0"
ember-cli-test-info "^1.0.0"
ember-cli-version-checker "^1.1.4"
ember-inflector "^2.0.0"
ember-runtime-enumerable-includes-polyfill "^2.0.0"
exists-sync "0.0.3"
git-repo-info "^1.1.2"
heimdalljs "^0.3.0"
inflection "^1.8.0"
npm-git-info "^1.0.0"
semver "^5.1.0"
silent-error "^1.0.0"

ember-inflector@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ember-inflector/-/ember-inflector-2.0.0.tgz#ac0870e87c0724bd42cf5ed7ef166c49a296ecfb"
dependencies:
ember-cli-babel "^6.0.0"

ember-load-initializers@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/ember-load-initializers/-/ember-load-initializers-1.0.0.tgz#4919eaf06f6dfeca7e134633d8c05a6c9921e6e7"
Expand Down Expand Up @@ -1990,6 +2057,13 @@ ember-router-generator@^1.0.0:
dependencies:
recast "^0.11.3"

ember-runtime-enumerable-includes-polyfill@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ember-runtime-enumerable-includes-polyfill/-/ember-runtime-enumerable-includes-polyfill-2.0.0.tgz#6e9ba118bc909d1d7762de1b03a550d8955308a9"
dependencies:
ember-cli-babel "^6.0.0"
ember-cli-version-checker "^1.1.6"

ember-source@~2.13.0:
version "2.13.2"
resolved "https://registry.yarnpkg.com/ember-source/-/ember-source-2.13.2.tgz#9fa9439a26515890981aa5d466f23da20adccff8"
Expand Down Expand Up @@ -2675,7 +2749,7 @@ getpass@^0.1.1:
dependencies:
assert-plus "^1.0.0"

git-repo-info@^1.4.1:
git-repo-info@^1.1.2, git-repo-info@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/git-repo-info/-/git-repo-info-1.4.1.tgz#2a072823254aaf62fcf0766007d7b6651bd41943"

Expand Down Expand Up @@ -2857,6 +2931,12 @@ heimdalljs@^0.2.0, heimdalljs@^0.2.1, heimdalljs@^0.2.3:
dependencies:
rsvp "~3.2.1"

heimdalljs@^0.3.0:
version "0.3.3"
resolved "https://registry.yarnpkg.com/heimdalljs/-/heimdalljs-0.3.3.tgz#e92d2c6f77fd46d5bf50b610d28ad31755054d0b"
dependencies:
rsvp "~3.2.1"

[email protected]:
version "2.16.3"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-2.16.3.tgz#20bb7403d3cea398e91dc4710a8ff1b8274a25ed"
Expand Down Expand Up @@ -2918,7 +2998,7 @@ [email protected]:
version "0.0.1"
resolved "https://registry.yarnpkg.com/indexof/-/indexof-0.0.1.tgz#82dc336d232b9062179d05ab3293a66059fd435d"

inflection@^1.7.0, inflection@^1.7.1:
inflection@^1.7.0, inflection@^1.7.1, inflection@^1.8.0:
version "1.12.0"
resolved "https://registry.yarnpkg.com/inflection/-/inflection-1.12.0.tgz#a200935656d6f5f6bc4dc7502e1aecb703228416"

Expand Down Expand Up @@ -3851,6 +3931,10 @@ normalize-path@^2.0.1:
dependencies:
remove-trailing-separator "^1.0.1"

npm-git-info@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/npm-git-info/-/npm-git-info-1.0.3.tgz#a933c42ec321e80d3646e0d6e844afe94630e1d5"

npm-package-arg@^4.1.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/npm-package-arg/-/npm-package-arg-4.2.1.tgz#593303fdea85f7c422775f17f9eb7670f680e3ec"
Expand Down Expand Up @@ -4393,6 +4477,10 @@ rsvp@^3.0.14, rsvp@^3.0.16, rsvp@^3.0.17, rsvp@^3.0.18, rsvp@^3.0.21, rsvp@^3.0.
version "3.5.0"
resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-3.5.0.tgz#a62c573a4ae4e1dfd0697ebc6242e79c681eaa34"

rsvp@~3.0.6:
version "3.0.21"
resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-3.0.21.tgz#49c588fe18ef293bcd0ab9f4e6756e6ac433359f"

rsvp@~3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/rsvp/-/rsvp-3.2.1.tgz#07cb4a5df25add9e826ebc67dcc9fd89db27d84a"
Expand Down

0 comments on commit c5396e9

Please sign in to comment.