Skip to content

Commit

Permalink
[BUGFIX fetch] prevent Accept & ContentType header overwriting (#6062)
Browse files Browse the repository at this point in the history
[BUGFIX fetch] prevent Accept & ContentType header overwriting
  • Loading branch information
nikhilsane authored and runspired committed May 1, 2019
1 parent 2db279a commit 81af0fb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,22 @@ test('ajaxOptions() adds Accept header to existing computed properties headers',
'headers assigned'
);
});

test('ajaxOptions() does not overwrite Accept header if it is provided', function(assert) {
let url = 'example.com';
let type = 'GET';
adapter.headers = { Accept: 'application/json' };
let ajaxOptions = adapter.ajaxOptions(url, type, {});
let receivedHeaders = [];
let fakeXHR = {
setRequestHeader(key, value) {
receivedHeaders.push([key, value]);
},
};
ajaxOptions.beforeSend(fakeXHR);
assert.deepEqual(
alphabetize(receivedHeaders),
[['Accept', 'application/json']],
'Accept header is not overwritten'
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,20 @@ test('ajaxOptions() adds Accept header to existing computed properties headers',
'headers assigned'
);
});

test('ajaxOptions() does not overwrite passed value of Accept headers', function(assert) {
adapter.headers = { 'Other-Key': 'Other Value', Accept: 'application/json' };
let url = 'example.com';
let type = 'GET';
let ajaxOptions = adapter.ajaxOptions(url, type, {});
let receivedHeaders = ajaxOptions.headers;

assert.deepEqual(
receivedHeaders,
{
Accept: 'application/json',
'Other-Key': 'Other Value',
},
'headers assigned, Accept header not overwritten'
);
});
5 changes: 3 additions & 2 deletions packages/adapter/addon/json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,11 @@ const JSONAPIAdapter = RESTAdapter.extend({
@return {Object}
*/
ajaxOptions(url, type, options = {}) {
options.contentType = 'application/vnd.api+json';
options.contentType = options.contentType || 'application/vnd.api+json';

let hash = this._super(url, type, options);
hash.headers['Accept'] = 'application/vnd.api+json';
hash.headers['Accept'] = hash.headers['Accept'] || 'application/vnd.api+json';

return hash;
},

Expand Down

0 comments on commit 81af0fb

Please sign in to comment.