This is an automated email from the ASF dual-hosted git repository. amaranhao pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/couchdb-fauxton.git
The following commit(s) were added to refs/heads/master by this push: new 5c57d3d Fix - Error is shown when enabling CORS (#1131) 5c57d3d is described below commit 5c57d3dbfbb8656c8cb70266d072552ee7b6f19c Author: Antonio Maranhao <30349380+antonio-maran...@users.noreply.github.com> AuthorDate: Wed Oct 3 14:50:41 2018 -0400 Fix - Error is shown when enabling CORS (#1131) * Fix PUT request for empty strings * Add tests --- app/core/__tests__/ajax.test.js | 70 +++++++++++++++++++++++++++++++++++++++++ app/core/ajax.js | 4 +-- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/app/core/__tests__/ajax.test.js b/app/core/__tests__/ajax.test.js index 5ffc82e..c16aa2e 100644 --- a/app/core/__tests__/ajax.test.js +++ b/app/core/__tests__/ajax.test.js @@ -140,4 +140,74 @@ describe('Fauxton Ajax', () => { assert.ok(resp.ok); }); }); + + describe('POST with falsy values as the body', () => { + const successResponse = { + status: 200, + body: { + ok: true + } + }; + it('accepts empty string', () => { + fetchMock.postOnce('/testing', successResponse); + return post('/testing', '') + .then(resp =>{ + assert.ok(resp.ok); + assert.ok(fetchMock.lastOptions().body === '""'); + }); + }); + + it('accepts zero', () => { + fetchMock.postOnce('/testing', successResponse); + return post('/testing', 0) + .then(resp =>{ + assert.ok(resp.ok); + assert.ok(fetchMock.lastOptions().body === '0'); + }); + }); + + it('accepts false', () => { + fetchMock.postOnce('/testing', successResponse); + return post('/testing', false) + .then(resp =>{ + assert.ok(resp.ok); + assert.ok(fetchMock.lastOptions().body === 'false'); + }); + }); + }); + + describe('PUT with falsy values as the body', () => { + const successResponse = { + status: 200, + body: { + ok: true + } + }; + it('accepts empty string', () => { + fetchMock.putOnce('/testing', successResponse); + return put('/testing', '') + .then(resp =>{ + assert.ok(resp.ok); + assert.ok(fetchMock.lastOptions().body === '""'); + }); + }); + + it('accepts zero', () => { + fetchMock.putOnce('/testing', successResponse); + return put('/testing', 0) + .then(resp =>{ + assert.ok(resp.ok); + assert.ok(fetchMock.lastOptions().body === '0'); + }); + }); + + it('accepts false', () => { + fetchMock.putOnce('/testing', successResponse); + return put('/testing', false) + .then(resp =>{ + assert.ok(resp.ok); + assert.ok(fetchMock.lastOptions().body === 'false'); + }); + }); + }); }); diff --git a/app/core/ajax.js b/app/core/ajax.js index 07fd094..305d8dc 100644 --- a/app/core/ajax.js +++ b/app/core/ajax.js @@ -81,7 +81,7 @@ export const deleteRequest = (url, opts = {}) => { * @return {Promise} A promise with the request's response */ export const post = (url, body, opts = {}) => { - if (body) { + if (typeof body !== 'undefined') { if (opts.rawBody) opts.body = body; else @@ -101,7 +101,7 @@ export const post = (url, body, opts = {}) => { * @return {Promise} A promise with the request's response */ export const put = (url, body, opts = {}) => { - if (body) { + if (typeof body !== 'undefined') { if (opts.rawBody) opts.body = body; else