Skip to content

Commit

Permalink
Merge pull request #64 from hogpilot/data-undefined
Browse files Browse the repository at this point in the history
Check for `undefined` body
  • Loading branch information
mdlavin committed Mar 17, 2021
2 parents 86b4b70 + e82f3fe commit 3f1b9f5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -21,7 +21,7 @@ async function axiosFetch (axios, transfomer, input, init = {}) {
const config = transfomer({
url: input,
method: init.method || 'GET',
data: init.body instanceof FormData ? init.body : String(init.body),
data: typeof init.body === 'undefined' || init.body instanceof FormData ? init.body : String(init.body),
headers: lowerCasedHeaders,
validateStatus: () => true,
// Force the response to an arraybuffer type. Without this, the Response
Expand Down
13 changes: 13 additions & 0 deletions test/index.test.js
Expand Up @@ -142,6 +142,19 @@ test('handles json body init options', async function (test) {
test.deepEqual(axiosBody.headers['content-type'], expectedBody.headers['content-type']);
});

test('handles undefined body in init options', async function (test) {
const init = {
method: 'POST',
body: undefined
};
const { expectedResponse, axiosResponse } = await dualFetch(`${TEST_URL_ROOT}/body`, init);

const expectedBody = await expectedResponse.json();
const axiosBody = await axiosResponse.json();

test.deepEqual(axiosBody.body, expectedBody.body);
});

test('returns the expected response on a multipart request', async function (test) {
const data = new FormData();
data.append('key', 'value');
Expand Down

0 comments on commit 3f1b9f5

Please sign in to comment.