From 124d55d1ae77bdf0f7b7512627018d544ede6b21 Mon Sep 17 00:00:00 2001 From: David Diaz Date: Thu, 1 Mar 2018 10:58:30 -0600 Subject: [PATCH] Fixed issue where an empty payload was changed to an empty object. --- .../__snapshots__/request.spec.js.snap | 4 +-- .../__snapshots__/requestList.spec.js.snap | 2 +- src/request.js | 29 ++++++++++++------- src/shared.js | 21 +++++++++++++- 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/src/__tests__/__snapshots__/request.spec.js.snap b/src/__tests__/__snapshots__/request.spec.js.snap index 45cd417..9d11666 100644 --- a/src/__tests__/__snapshots__/request.spec.js.snap +++ b/src/__tests__/__snapshots__/request.spec.js.snap @@ -24,9 +24,9 @@ exports[`Request should execute a request 2`] = ` Object { "body": "{\\"hello\\": \\"world\\"}", "request": Object { - "body": Object {}, + "body": undefined, "endpoint": "http://martianwabbit.com/user", - "headers": Object {}, + "headers": undefined, }, "response": Object { "body": "{\\"hello\\": \\"world\\"}", diff --git a/src/__tests__/__snapshots__/requestList.spec.js.snap b/src/__tests__/__snapshots__/requestList.spec.js.snap index 74a03f5..a7687de 100644 --- a/src/__tests__/__snapshots__/requestList.spec.js.snap +++ b/src/__tests__/__snapshots__/requestList.spec.js.snap @@ -10,7 +10,7 @@ Object { "name": "Sergio", }, "endpoint": "http://martianwabbit.com/user", - "headers": Object {}, + "headers": undefined, }, "response": Object { "body": "{\\"hello\\": \\"world\\"}", diff --git a/src/request.js b/src/request.js index 40f57da..033e885 100644 --- a/src/request.js +++ b/src/request.js @@ -3,7 +3,8 @@ const { httpVerbs, requestRegex, replacementRegex, - UpperCaseKeys + UpperCaseKeys, + removeOptionalKeys } = require('./shared'); const RequestList = require('./requestList'); const RequestCache = require('./requestCache'); @@ -81,17 +82,23 @@ class Request { }); try { - const response = await request({ - url: settings.endpoint, - method: settings.method, - headers: settings.headers, - qs: settings.query, - body: settings.payload, + const response = await request( + removeOptionalKeys( + { + url: settings.endpoint, + method: settings.method, - json: true, - simple: false, - resolveWithFullResponse: true - }); + headers: settings.headers, + qs: settings.query, + body: settings.payload, + + json: true, + simple: false, + resolveWithFullResponse: true + }, + ['headers', 'qs', 'body'] + ) + ); const results = { request: { diff --git a/src/shared.js b/src/shared.js index ae7a66a..58ece43 100644 --- a/src/shared.js +++ b/src/shared.js @@ -19,9 +19,28 @@ const UpperCaseKeys = function(obj) { return result; }; +const removeOptionalKeys = function(obj, optionalValues) { + let result = {}; + + Object.keys(obj).forEach(key => { + if ( + optionalValues.includes(key) && + (Object.keys(obj[key]).length === 0 && + obj[key].constructor === Object) + ) { + return; + } + + result[key] = obj[key]; + }); + + return result; +}; + module.exports = { httpVerbs, requestRegex, replacementRegex, - UpperCaseKeys + UpperCaseKeys, + removeOptionalKeys };