Skip to content

Commit

Permalink
Fix non GET/HEAD requests not having body set
Browse files Browse the repository at this point in the history
  • Loading branch information
nlfurniss committed Oct 16, 2017
1 parent fe405fa commit fc5167b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
14 changes: 8 additions & 6 deletions addon/mixins/adapter-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,14 @@ export function mungOptionsForFetch(_options, adapter) {

if (options.data) {
// GET and HEAD requests can't have a `body`
// If no options are passed, Ember Data sets `data` to an empty object, which we test for.
if ((options.method === 'GET' || options.method === 'HEAD') && Object.keys(options.data).length) {
// Test if there are already query params in the url (mimics jQuey.ajax).
const queryParamDelimiter = options.url.indexOf('?') > -1 ? '&' : '?';
options.url += `${queryParamDelimiter}${serializeQueryParams(options.data)}`;
} else if (options.method === 'POST') {
if ((options.method === 'GET' || options.method === 'HEAD')) {
// If no options are passed, Ember Data sets `data` to an empty object, which we test for.
if (Object.keys(options.data).length) {
// Test if there are already query params in the url (mimics jQuey.ajax).
const queryParamDelimiter = options.url.indexOf('?') > -1 ? '&' : '?';
options.url += `${queryParamDelimiter}${serializeQueryParams(options.data)}`;
}
} else {
// NOTE: a request's body cannot be an object, so we stringify it if it is.
// JSON.stringify removes keys with values of `undefined` (mimics jQuery.ajax).
options.body = JSON.stringify(options.data);
Expand Down
30 changes: 27 additions & 3 deletions tests/unit/mixins/adapter-fetch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,30 @@ test('mungOptionsForFetch removes undefined query params when method is POST and
assert.deepEqual(options.body, "{\"a\":1,\"c\":3,\"d\":null,\"e\":0,\"f\":false}");
});

test('mungOptionsForFetch sets the request body correctly when the method is not GET or HEAD', function(assert) {
assert.expect(3);

const baseOptions = {
url: '/',
type: 'POST',
data: { a: 1 }
};

// Tests POST method.
let options = mungOptionsForFetch(baseOptions, this.basicAdapter);
assert.equal(options.body, JSON.stringify(baseOptions.data), 'POST request body correctly set');

// Tests PUT method.
baseOptions.type = 'PUT';
options = mungOptionsForFetch(baseOptions, this.basicAdapter);
assert.equal(options.body, JSON.stringify(baseOptions.data), 'PUT request body correctly set');

// Tests DELETE method.
baseOptions.type = 'DELETE';
options = mungOptionsForFetch(baseOptions, this.basicAdapter);
assert.equal(options.body, JSON.stringify(baseOptions.data), 'DELETE request has the correct body');
});

test('mungOptionsForFetch sets the request body correctly when the method is POST and \'data\' is a string', function(assert) {
assert.expect(2);

Expand Down Expand Up @@ -258,22 +282,22 @@ test('mungOptionsForFetch does not set a request body when the method is GET or

const baseOptions = {
url: '/',
method: 'GET',
type: 'GET',
data: { a: 1 }
};

let options = mungOptionsForFetch(baseOptions, this.basicAdapter);
assert.equal(options.body, undefined, 'GET request does not have a request body');

baseOptions.method = 'HEAD';
baseOptions.type = 'HEAD';
options = mungOptionsForFetch(baseOptions, this.basicAdapter);
assert.equal(options.body, undefined, 'HEAD request does not have a request body');

baseOptions.data = {};
options = mungOptionsForFetch(baseOptions, this.basicAdapter);
assert.equal(options.body, undefined, 'HEAD request does not have a request body when `data` is an empty object');

baseOptions.method = 'GET';
baseOptions.type = 'GET';
options = mungOptionsForFetch(baseOptions, this.basicAdapter);
assert.equal(options.body, undefined, 'GET request does not have a request body when `data` is an empty object');
});
Expand Down

0 comments on commit fc5167b

Please sign in to comment.