Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX]: set Content-Type for non GET requests only #6341

Merged
merged 5 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import DS from 'ember-data';

let Person, Place, store, adapter, env;

module('unit/adapters/json-api-adapter/ajax-options - building requests', function(hooks) {
module('unit/adapters/json-api-adapter/ajax-options - building requests with fetch', function(hooks) {
hooks.beforeEach(function() {
Person = { modelName: 'person' };
Place = { modelName: 'place' };
Expand Down Expand Up @@ -89,4 +89,58 @@ module('unit/adapters/json-api-adapter/ajax-options - building requests', functi
'headers assigned, Accept header not overwritten'
);
});

test('ajaxOptions() headers are set POST', function(assert) {
adapter.headers = {};
let url = 'example.com';
let type = 'POST';
let ajaxOptions = adapter.ajaxOptions(url, type, { data: { type: 'post' } });
let receivedHeaders = ajaxOptions.headers;

assert.deepEqual(
receivedHeaders,
{
Accept: 'application/vnd.api+json',
'content-type': 'application/vnd.api+json',
},
'headers assigned on POST'
);
});

test('ajaxOptions() does not override with existing headers["Content-Type"] POST', function(assert) {
adapter.headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
let url = 'example.com';
let type = 'POST';
let ajaxOptions = adapter.ajaxOptions(url, type, { data: { type: 'post' } });
let receivedHeaders = ajaxOptions.headers;

assert.deepEqual(
receivedHeaders,
{
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/vnd.api+json',
},
'content-type header not overwritten'
);
});

test('ajaxOptions() can override with options.contentType POST', function(assert) {
adapter.headers = {};
let url = 'example.com';
let type = 'POST';
let ajaxOptions = adapter.ajaxOptions(url, type, {
contentType: 'application/x-www-form-urlencoded',
data: { type: 'post' },
});
let receivedHeaders = ajaxOptions.headers;

assert.deepEqual(
receivedHeaders,
{
'content-type': 'application/x-www-form-urlencoded',
Accept: 'application/vnd.api+json',
},
'content-type header overwritten'
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import DS from 'ember-data';

var Person, Place, store, adapter, env;

module('unit/adapters/rest-adapter/ajax-options - building requests', function(hooks) {
module('unit/adapters/rest-adapter/ajax-options - building requests with fetch', function(hooks) {
hooks.beforeEach(function() {
Person = { modelName: 'person' };
Place = { modelName: 'place' };
Expand Down Expand Up @@ -82,7 +82,6 @@ module('unit/adapters/rest-adapter/ajax-options - building requests', function(h
let url = 'example.com';
let type = 'GET';
let ajaxOptions = adapter.ajaxOptions(url, type, { data: { key: 'value' } });
delete ajaxOptions.beforeSend;

assert.deepEqual(ajaxOptions, {
credentials: 'same-origin',
Expand All @@ -100,7 +99,6 @@ module('unit/adapters/rest-adapter/ajax-options - building requests', function(h
let url = 'example.com';
let type = 'POST';
let ajaxOptions = adapter.ajaxOptions(url, type, { data: { key: 'value' } });
delete ajaxOptions.beforeSend;

assert.deepEqual(ajaxOptions, {
credentials: 'same-origin',
Expand All @@ -115,11 +113,55 @@ module('unit/adapters/rest-adapter/ajax-options - building requests', function(h
});
});

test('ajaxOptions() can provide own headers["Content-Type"]', function(assert) {
let url = 'example.com';
let type = 'POST';
let ajaxOptions = adapter.ajaxOptions(url, type, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data: { key: 'value' },
});

assert.deepEqual(ajaxOptions, {
credentials: 'same-origin',
data: { key: 'value' },
body: '{"key":"value"}',
type: 'POST',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
url: 'example.com',
});
});

test('ajaxOptions() can provide own contentType in options', function(assert) {
let url = 'example.com';
let type = 'POST';
let ajaxOptions = adapter.ajaxOptions(url, type, {
contentType: 'application/x-www-form-urlencoded',
data: { key: 'value' },
});

assert.deepEqual(ajaxOptions, {
contentType: 'application/x-www-form-urlencoded',
credentials: 'same-origin',
data: { key: 'value' },
body: '{"key":"value"}',
type: 'POST',
method: 'POST',
headers: {
'content-type': 'application/x-www-form-urlencoded',
},
url: 'example.com',
});
});

test('ajaxOptions() empty data', function(assert) {
let url = 'example.com';
let type = 'POST';
let ajaxOptions = adapter.ajaxOptions(url, type, {});
delete ajaxOptions.beforeSend;

assert.deepEqual(ajaxOptions, {
credentials: 'same-origin',
Expand All @@ -145,4 +187,47 @@ module('unit/adapters/rest-adapter/ajax-options - building requests', function(h
return fetchPlacePromise;
});
});

module('ajax-options - ajax', function(hooks) {
hooks.beforeEach(function() {
adapter.set('useFetch', false);
});

hooks.afterEach(function() {
run(() => {
store.destroy();
env.container.destroy();
});
});

test('ajaxOptions() Content-Type is not set with ajax GET', function(assert) {
adapter.headers = {};

let url = 'example.com';
let type = 'GET';
let ajaxOptions = adapter.ajaxOptions(url, type, {});

assert.notOk(ajaxOptions.contentType, 'contentType not set with GET');
});

test('ajaxOptions() Content-Type is not set with ajax POST no data', function(assert) {
adapter.headers = {};

let url = 'example.com';
let type = 'POST';
let ajaxOptions = adapter.ajaxOptions(url, type, {});

assert.notOk(ajaxOptions.contentType, 'contentType not set with POST no data');
});

test('ajaxOptions() Content-Type is set with ajax POST with data', function(assert) {
adapter.headers = {};

let url = 'example.com';
let type = 'POST';
let ajaxOptions = adapter.ajaxOptions(url, type, { data: { type: 'post' } });

assert.equal(ajaxOptions.contentType, 'application/json; charset=utf-8', 'contentType is set with POST');
});
});
});
5 changes: 3 additions & 2 deletions packages/adapter/addon/json-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ import { pluralize } from 'ember-inflector';
const JSONAPIAdapter = RESTAdapter.extend({
defaultSerializer: '-json-api',

_defaultContentType: 'application/vnd.api+json',

/**
@method ajaxOptions
@private
Expand All @@ -152,9 +154,8 @@ const JSONAPIAdapter = RESTAdapter.extend({
@return {Object}
*/
ajaxOptions(url, type, options = {}) {
options.contentType = options.contentType || 'application/vnd.api+json';

let hash = this._super(url, type, options);

hash.headers['Accept'] = hash.headers['Accept'] || 'application/vnd.api+json';

return hash;
Expand Down
18 changes: 13 additions & 5 deletions packages/adapter/addon/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ const hasNajax = typeof najax !== 'undefined';
const RESTAdapter = Adapter.extend(BuildURLMixin, {
defaultSerializer: '-rest',

_defaultContentType: 'application/json; charset=utf-8',

fastboot: computed(function() {
return getOwner(this).lookup('service:fastboot');
}),
Expand Down Expand Up @@ -1093,14 +1095,21 @@ const RESTAdapter = Adapter.extend(BuildURLMixin, {
options.headers = {};
}

if (options.data && options.type !== 'GET') {
let contentType = options.contentType || 'application/json; charset=utf-8';
options.headers['content-type'] = contentType;
}
let contentType = options.contentType || this._defaultContentType;

if (get(this, 'useFetch')) {
if (options.data && options.type !== 'GET') {
if (!options.headers['Content-Type'] && !options.headers['content-type']) {
options.headers['content-type'] = contentType;
}
}
options = fetchOptions(options, this);
} else {
// GET requests without a body should not have a content-type header
// and may be unexpected by a server
if (options.data && options.type !== 'GET') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A GET request with a non-simple content-type will result in a preflight request. One consequence of adding the Content-Type header on GET (where we used to not) is the backend server perhaps doesn't expect this and won't return the right a-c-a-o header.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we leave this explanation of this conditional as a comment in the code?

options = assign(options, { contentType });
}
options = ajaxOptions(options, this);
}

Expand Down Expand Up @@ -1370,7 +1379,6 @@ function ajaxOptions(options, adapter) {

if (options.data && options.type !== 'GET') {
options.data = JSON.stringify(options.data);
options.contentType = 'application/json; charset=utf-8';
}

options.beforeSend = function(xhr) {
Expand Down