From 9676fd6fd80d677ac5c26296fc2b138d8b95b560 Mon Sep 17 00:00:00 2001 From: Christian Stoyanov Date: Mon, 11 May 2020 22:32:10 +0200 Subject: [PATCH 1/2] When data is formData Content-Type is set to application/x-www-form-urlencoded --- src/index.js | 7 +++++-- test/index.test.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index f4c86f2..050d8fb 100644 --- a/src/index.js +++ b/src/index.js @@ -142,11 +142,14 @@ export default (function create(defaults) { } } } - + const fetchFunc = options.fetch || fetch; const customHeaders = {}; - if (data && typeof data === 'object') { + if (data && data instanceof FormData) { + customHeaders['Content-Type'] = 'application/x-www-form-urlencoded'; + } + else if (data && typeof data === 'object') { data = JSON.stringify(data); customHeaders['Content-Type'] = 'application/json'; } diff --git a/test/index.test.js b/test/index.test.js index 8327ba5..146c0b3 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -83,4 +83,19 @@ describe('redaxios', () => { expect(res.status).toEqual(200); expect(JSON.parse(res.data)).toEqual({ hello: 'world' }); }); + + it('should send formData as application/x-www-form-urlencoded', async() => { + window.fetch = jasmine.createSpy('fetch').and.returnValue(Promise.resolve({ ok: true, status: 200, text: () => Promise.resolve('yep') })); + const data = new FormData(); + data.append('hello', 'world'); + axios.post('/foo', data); + expect(window.fetch).toHaveBeenCalledTimes(1); + expect(window.fetch).toHaveBeenCalledWith('/foo', jasmine.objectContaining({ + method: 'post', + headers: { + 'content-type': 'application/x-www-form-urlencoded' + }, + body: data + })); + }); }); From f5b94d8b4c72e17f847de2d4bcad6c8f6b849cc6 Mon Sep 17 00:00:00 2001 From: Christian Stoyanov Date: Mon, 11 May 2020 22:44:14 +0200 Subject: [PATCH 2/2] Add finally to jasmine spy tests --- test/index.test.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/test/index.test.js b/test/index.test.js index 146c0b3..d050c60 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -85,17 +85,23 @@ describe('redaxios', () => { }); it('should send formData as application/x-www-form-urlencoded', async() => { - window.fetch = jasmine.createSpy('fetch').and.returnValue(Promise.resolve({ ok: true, status: 200, text: () => Promise.resolve('yep') })); - const data = new FormData(); - data.append('hello', 'world'); - axios.post('/foo', data); - expect(window.fetch).toHaveBeenCalledTimes(1); - expect(window.fetch).toHaveBeenCalledWith('/foo', jasmine.objectContaining({ - method: 'post', - headers: { - 'content-type': 'application/x-www-form-urlencoded' - }, - body: data - })); + const oldFetch = window.fetch; + try { + window.fetch = jasmine.createSpy('fetch').and.returnValue(Promise.resolve({ ok: true, status: 200, text: () => Promise.resolve('yep') })); + const data = new FormData(); + data.append('hello', 'world'); + axios.post('/foo', data); + expect(window.fetch).toHaveBeenCalledTimes(1); + expect(window.fetch).toHaveBeenCalledWith('/foo', jasmine.objectContaining({ + method: 'post', + headers: { + 'content-type': 'application/x-www-form-urlencoded' + }, + body: data + })); + } + finally { + window.fetch = oldFetch; + } }); });