From afbf934d95b2761d6ca1ce2aa8c8b3ea6f839fe3 Mon Sep 17 00:00:00 2001 From: Dom Harrington Date: Thu, 18 Oct 2018 17:42:02 -0700 Subject: [PATCH] Build on top of #163 to add support for falsy query and header values as well --- .../__tests__/lib/oas-to-har.test.js | 40 ++++++++++++++++++- packages/api-explorer/src/lib/oas-to-har.js | 4 +- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/api-explorer/__tests__/lib/oas-to-har.test.js b/packages/api-explorer/__tests__/lib/oas-to-har.test.js index 1fd24b607..610f058d7 100644 --- a/packages/api-explorer/__tests__/lib/oas-to-har.test.js +++ b/packages/api-explorer/__tests__/lib/oas-to-har.test.js @@ -191,7 +191,7 @@ describe('path values', () => { ).toBe('https://example.com/param-path/456'); }); - test('should add non-undefined values to the url', () => { + test('should add falsy values to the url', () => { expect( oasToHar( {}, @@ -271,6 +271,25 @@ describe('query values', () => { ).log.entries[0].request.queryString, ).toEqual([{ name: 'a', value: 'test' }]); }); + + test('should add falsy values to the querystring', () => { + expect( + oasToHar( + {}, + { + path: '/param-path', + method: 'get', + parameters: [ + { + name: 'id', + in: 'query' + }, + ], + }, + { query: { id: 0 } }, + ).log.entries[0].request.queryString, + ).toEqual([{ name: 'id', value: '0' }]); + }); }); describe('header values', () => { @@ -389,6 +408,25 @@ describe('header values', () => { ).log.entries[0].request.headers, ).toEqual([{ name: 'Accept', value: 'application/xml' }]); }); + + test('should add falsy values to the headers', () => { + expect( + oasToHar( + {}, + { + path: '/param-path', + method: 'get', + parameters: [ + { + name: 'id', + in: 'header' + }, + ], + }, + { header: { id: 0 } }, + ).log.entries[0].request.headers, + ).toEqual([{ name: 'id', value: '0' }]); + }); }); const pathOperation = { diff --git a/packages/api-explorer/src/lib/oas-to-har.js b/packages/api-explorer/src/lib/oas-to-har.js index 79f2b3eea..437b4bdce 100644 --- a/packages/api-explorer/src/lib/oas-to-har.js +++ b/packages/api-explorer/src/lib/oas-to-har.js @@ -124,7 +124,7 @@ module.exports = ( if (queryStrings && queryStrings.length) { queryStrings.forEach(queryString => { const value = formatter(formData, queryString, 'query', true); - if (!value) return; + if (typeof value === 'undefined') return; har.queryString.push({ name: queryString.name, value: String(value), @@ -152,7 +152,7 @@ module.exports = ( if (headers && headers.length) { headers.forEach(header => { const value = formatter(formData, header, 'header', true); - if (!value) return; + if (typeof value === 'undefined') return; har.headers.push({ name: header.name, value: String(value),