From 6e73abcaece1ba6a97f19e91bcbeeda281396eb9 Mon Sep 17 00:00:00 2001 From: bvigerzi <48936239+bvigerzi@users.noreply.github.com> Date: Fri, 13 Jan 2023 20:46:38 +1100 Subject: [PATCH] fix: overwrite value with path parameters excludes parameters --- package-lock.json | 1 + package.json | 1 + .../overwriteRequestBaseUrl.test.ts.snap | 37 +++++++++++++++++++ .../overwriteRequestBaseUrl.test.ts | 16 ++++++++ .../overwrites/overwriteRequestBaseUrl.ts | 10 ++++- 5 files changed, 64 insertions(+), 1 deletion(-) diff --git a/package-lock.json b/package-lock.json index 38aa3a98..8b8a962a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "faker": "^5.5.3", "fp-ts": "^2.12.3", "fs-extra": "^10.1.0", + "lodash": "^4.17.21", "newman": "^5.3.2", "node-emoji": "^1.11.0", "openapi-format": "^1.13.0", diff --git a/package.json b/package.json index ba6fc2a2..72481a50 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "faker": "^5.5.3", "fp-ts": "^2.12.3", "fs-extra": "^10.1.0", + "lodash": "^4.17.21", "newman": "^5.3.2", "node-emoji": "^1.11.0", "openapi-format": "^1.13.0", diff --git a/src/application/overwrites/__snapshots__/overwriteRequestBaseUrl.test.ts.snap b/src/application/overwrites/__snapshots__/overwriteRequestBaseUrl.test.ts.snap index 08770a3b..edeca311 100644 --- a/src/application/overwrites/__snapshots__/overwriteRequestBaseUrl.test.ts.snap +++ b/src/application/overwrites/__snapshots__/overwriteRequestBaseUrl.test.ts.snap @@ -259,6 +259,43 @@ Object { } `; +exports[`overwriteRequestBaseUrl should overwrite the request base url with a variable and path 1`] = ` +Object { + "host": Array [ + "{{foo-bar-baz}}", + ], + "path": Array [ + "path", + "crm", + "companies", + ":id", + ], + "query": Array [ + Object { + "description": Object { + "content": "Include raw response. Mostly used for debugging purposes", + "type": "text/plain", + }, + "disabled": false, + "key": "raw", + "value": "true", + }, + ], + "variable": Array [ + Object { + "description": Object { + "content": "(Required) ID of the record you are acting upon.", + "type": "text/plain", + }, + "disabled": false, + "key": "id", + "type": "any", + "value": "123456", + }, + ], +} +`; + exports[`overwriteRequestBaseUrl should remove the request base url when remove is true 1`] = ` Object { "path": Array [ diff --git a/src/application/overwrites/overwriteRequestBaseUrl.test.ts b/src/application/overwrites/overwriteRequestBaseUrl.test.ts index 22429425..19168039 100644 --- a/src/application/overwrites/overwriteRequestBaseUrl.test.ts +++ b/src/application/overwrites/overwriteRequestBaseUrl.test.ts @@ -89,4 +89,20 @@ describe('overwriteRequestBaseUrl', () => { expect(result.item.request.url.getHost()).toBe('') expect(result.item.request.url).toMatchSnapshot() }) + + it('should overwrite the request base url with a variable and path', async () => { + const overwriteValue = { + value: '{{foo-bar-baz}}/path' + } + + const pmOperation = await getPostmanMappedOperation() + const result = overwriteRequestBaseUrl(overwriteValue, pmOperation) + expect(result.item.request.url.getHost()).toBe('{{foo-bar-baz}}') + expect(result.item.request.url.path).toBeDefined() + expect(result.item.request.url.path?.length).toEqual(4) + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore + expect(result.item.request.url.path[0]).toEqual('path') + expect(result.item.request.url).toMatchSnapshot() + }) }) diff --git a/src/application/overwrites/overwriteRequestBaseUrl.ts b/src/application/overwrites/overwriteRequestBaseUrl.ts index 4def4ed5..c3b300e4 100644 --- a/src/application/overwrites/overwriteRequestBaseUrl.ts +++ b/src/application/overwrites/overwriteRequestBaseUrl.ts @@ -1,5 +1,6 @@ import { PostmanMappedOperation } from '../../postman' import { OverwriteRequestBaseUrlConfig } from '../../types' +import { isEqual } from 'lodash' // import { Header } from 'postman-collection' /** @@ -31,10 +32,17 @@ export const overwriteRequestBaseUrl = ( newPm.item.request.url.update('') } - // Update protocol, host & port + // Update protocol, host, port & path pmOperation.item.request.url.protocol = newPm.item.request.url.protocol pmOperation.item.request.url.port = newPm.item.request.url.port pmOperation.item.request.url.host = newPm.item.request.url.host + // if they are equal then no path was provided in the overwrite value + if (!isEqual(pmOperation.item.request.url.path, newPm.item.request.url.path)) { + pmOperation.item.request.url.path = [ + ...(newPm.item.request.url.path ?? []), + ...(pmOperation.item.request.url.path ?? []) + ] + } return pmOperation }