-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX fetchOptions] fix fetchOptions to support POST body of all va…
…lid types (#6562)
- Loading branch information
Showing
2 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
148 changes: 148 additions & 0 deletions
148
packages/-ember-data/tests/unit/adapters/rest-adapter/fetch-options-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { module, test } from 'qunit'; | ||
import { fetchOptions } from '@ember-data/adapter/rest'; | ||
|
||
module('unit/adapters/rest-adapter/fetch-options', function(hooks) { | ||
test("fetchOptions removes undefined query params when method is POST and 'data' is an object", function(assert) { | ||
assert.expect(1); | ||
|
||
const dataAsObject = { | ||
a: 1, | ||
b: undefined, | ||
c: 3, | ||
d: null, | ||
e: 0, | ||
f: false, | ||
}; | ||
|
||
const undefinedQueryStringOptions = { | ||
url: 'https://emberjs.com', | ||
method: 'POST', | ||
data: dataAsObject, | ||
}; | ||
|
||
let options = fetchOptions(undefinedQueryStringOptions); | ||
assert.deepEqual(options.body, '{"a":1,"c":3,"d":null,"e":0,"f":false}'); | ||
}); | ||
|
||
test('fetchOptions sets the request body correctly when the method is not GET or HEAD', function(assert) { | ||
assert.expect(3); | ||
|
||
const baseOptions = { | ||
url: '/', | ||
method: 'POST', | ||
data: { a: 1 }, | ||
}; | ||
|
||
// Tests POST method. | ||
let options = fetchOptions(baseOptions); | ||
assert.equal(options.body, JSON.stringify(baseOptions.data), 'POST request body correctly set'); | ||
|
||
// Tests PUT method. | ||
baseOptions.method = 'PUT'; | ||
options = fetchOptions(baseOptions); | ||
assert.equal(options.body, JSON.stringify(baseOptions.data), 'PUT request body correctly set'); | ||
|
||
// Tests DELETE method. | ||
baseOptions.method = 'DELETE'; | ||
options = fetchOptions(baseOptions); | ||
assert.equal(options.body, JSON.stringify(baseOptions.data), 'DELETE request has the correct body'); | ||
}); | ||
|
||
test("fetchOptions sets the request body correctly when the method is POST and 'data' is a string", function(assert) { | ||
assert.expect(1); | ||
|
||
// Tests stringified objects. | ||
const stringifiedData = JSON.stringify({ a: 1, b: 2 }); | ||
const optionsWithStringData = { | ||
url: 'https://emberjs.com', | ||
method: 'POST', | ||
data: stringifiedData, | ||
}; | ||
|
||
let options = fetchOptions(optionsWithStringData); | ||
assert.equal(options.body, stringifiedData); | ||
}); | ||
|
||
test('fetchOptions does not set a request body when the method is GET or HEAD', function(assert) { | ||
assert.expect(4); | ||
|
||
const baseOptions = { | ||
url: '/', | ||
method: 'GET', | ||
data: { a: 1 }, | ||
}; | ||
|
||
let options = fetchOptions(baseOptions); | ||
assert.strictEqual(options.body, undefined, 'GET request does not have a request body'); | ||
|
||
baseOptions.method = 'HEAD'; | ||
options = fetchOptions(baseOptions); | ||
assert.strictEqual(options.body, undefined, 'HEAD request does not have a request body'); | ||
|
||
baseOptions.data = {}; | ||
options = fetchOptions(baseOptions); | ||
assert.strictEqual( | ||
options.body, | ||
undefined, | ||
'HEAD request does not have a request body when `data` is an empty object' | ||
); | ||
|
||
baseOptions.method = 'GET'; | ||
options = fetchOptions(baseOptions); | ||
assert.strictEqual( | ||
options.body, | ||
undefined, | ||
'GET request does not have a request body when `data` is an empty object' | ||
); | ||
}); | ||
|
||
test("fetchOptions correctly processes an empty 'data' object", function(assert) { | ||
assert.expect(2); | ||
|
||
const getData = { | ||
url: 'https://emberjs.com', | ||
method: 'GET', | ||
data: {}, | ||
}; | ||
|
||
const getOptions = fetchOptions(getData); | ||
assert.equal(getOptions.url.indexOf('?'), -1, 'A question mark is not added if there are no query params to add'); | ||
|
||
const postData = { | ||
url: 'https://emberjs.com', | ||
method: 'POST', | ||
data: {}, | ||
}; | ||
|
||
const postOptions = fetchOptions(postData); | ||
assert.equal(postOptions.body, '{}', "'options.body' is an empty object"); | ||
}); | ||
|
||
test("fetchOptions sets the request body correctly when 'data' is FormData", function(assert) { | ||
assert.expect(1); | ||
|
||
const formData = new FormData(); | ||
const postData = { | ||
url: 'https://emberjs.com', | ||
method: 'POST', | ||
data: formData, | ||
}; | ||
|
||
const postOptions = fetchOptions(postData); | ||
assert.strictEqual(postOptions.body, formData, "'options.body' is the FormData passed in"); | ||
}); | ||
|
||
test("fetchOptions sets the request body correctly when 'data' is a String", function(assert) { | ||
assert.expect(1); | ||
|
||
let stringBody = JSON.stringify({ a: 1, b: 2, c: 3 }); | ||
const postData = { | ||
url: 'https://emberjs.com', | ||
method: 'POST', | ||
data: stringBody, | ||
}; | ||
|
||
const postOptions = fetchOptions(postData); | ||
assert.equal(postOptions.body, stringBody, "'options.body' is the String passed in"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters