From 2cd080cc2b4919d9dc0ec1dfbc67d466432ec841 Mon Sep 17 00:00:00 2001 From: Lukas Siemon Date: Thu, 11 Apr 2024 17:04:03 -0700 Subject: [PATCH 1/7] fix: improved response healing --- src/modules/request-recorder.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/request-recorder.js b/src/modules/request-recorder.js index 7ebc554f..c7d07202 100644 --- a/src/modules/request-recorder.js +++ b/src/modules/request-recorder.js @@ -237,13 +237,14 @@ export default (opts) => { } if (anyFlagPresent(['magic', 'response'])) { + const interceptorBody = tryParseJson(interceptor.body); const responseBody = tryParseJson([ healSqs ].reduce( (respBody, fn) => fn(requestBodyString, respBody, scope, req), interceptor.body )); - if (!isEqual(interceptor.body, responseBody)) { + if (!isEqual(interceptorBody, responseBody)) { // eslint-disable-next-line no-param-reassign interceptor.body = responseBody; pendingMocks[idx].record.response = responseBody; From 012a459cee2cb104ec5888393ed43c5df52c34d6 Mon Sep 17 00:00:00 2001 From: Lukas Siemon Date: Thu, 11 Apr 2024 17:33:38 -0700 Subject: [PATCH 2/7] fix: reworked body and response healing --- package.json | 1 - src/modules/request-recorder.js | 23 +++++++++++-------- .../{heal-body.js => heal-modifiers.js} | 0 3 files changed, 13 insertions(+), 11 deletions(-) rename src/modules/request-recorder/{heal-body.js => heal-modifiers.js} (100%) diff --git a/package.json b/package.json index b98c4b10..209c63b7 100644 --- a/package.json +++ b/package.json @@ -78,7 +78,6 @@ "joi-strict": "3.0.1", "lodash.clonedeep": "4.5.0", "lodash.get": "4.4.2", - "lodash.isequal": "4.5.0", "lru-cache-ext": "4.0.0", "minimist": "1.2.8", "nock": "13.5.4", diff --git a/src/modules/request-recorder.js b/src/modules/request-recorder.js index c7d07202..3983f40a 100644 --- a/src/modules/request-recorder.js +++ b/src/modules/request-recorder.js @@ -6,7 +6,6 @@ import Joi from 'joi-strict'; import nock from 'nock'; import get from 'lodash.get'; import cloneDeep from 'lodash.clonedeep'; -import isEqual from 'lodash.isequal'; import nockCommon from 'nock/lib/common.js'; import compareUrls from '../util/compare-urls.js'; import nockListener from './request-recorder/nock-listener.js'; @@ -22,7 +21,7 @@ import { convertHeaders, rewriteHeaders } from './request-recorder/util.js'; -import healBody from './request-recorder/heal-body.js'; +import healModifiers from './request-recorder/heal-modifiers.js'; const nockBack = nock.back; const nockRecorder = nock.recorder; @@ -180,9 +179,11 @@ export default (opts) => { if (anyFlagPresent(['magic', 'body'])) { const idx = pendingMocks.findIndex((m) => m.idx === scopeIdx); const requestBody = nullAsString(tryParseJson(body)); - if (!isEqual(scope.body, requestBody)) { - pendingMocks[idx].record.body = healBody(pendingMocks[idx].record.body, scope.body, requestBody); - } + pendingMocks[idx].record.body = healModifiers( + pendingMocks[idx].record.body, + scope.body, + requestBody + ); return scope.body; } return body; @@ -244,11 +245,13 @@ export default (opts) => { (respBody, fn) => fn(requestBodyString, respBody, scope, req), interceptor.body )); - if (!isEqual(interceptorBody, responseBody)) { - // eslint-disable-next-line no-param-reassign - interceptor.body = responseBody; - pendingMocks[idx].record.response = responseBody; - } + pendingMocks[idx].record.response = healModifiers( + pendingMocks[idx].record.response, + interceptorBody, + responseBody + ); + // eslint-disable-next-line no-param-reassign + interceptor.body = responseBody; } expectedCassette.push(pendingMocks[idx].record); diff --git a/src/modules/request-recorder/heal-body.js b/src/modules/request-recorder/heal-modifiers.js similarity index 100% rename from src/modules/request-recorder/heal-body.js rename to src/modules/request-recorder/heal-modifiers.js From ee49bcd1cddbf3d9fd2d0a64e7bfb37fde0b9b08 Mon Sep 17 00:00:00 2001 From: Lukas Siemon Date: Thu, 11 Apr 2024 17:35:10 -0700 Subject: [PATCH 3/7] amend: refactor --- src/modules/request-recorder.js | 12 ++---------- src/modules/request-recorder/heal-modifiers.js | 17 ++++++++++------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/modules/request-recorder.js b/src/modules/request-recorder.js index 3983f40a..643ef409 100644 --- a/src/modules/request-recorder.js +++ b/src/modules/request-recorder.js @@ -179,11 +179,7 @@ export default (opts) => { if (anyFlagPresent(['magic', 'body'])) { const idx = pendingMocks.findIndex((m) => m.idx === scopeIdx); const requestBody = nullAsString(tryParseJson(body)); - pendingMocks[idx].record.body = healModifiers( - pendingMocks[idx].record.body, - scope.body, - requestBody - ); + healModifiers(pendingMocks[idx].record, scope.body, requestBody, 'body'); return scope.body; } return body; @@ -245,11 +241,7 @@ export default (opts) => { (respBody, fn) => fn(requestBodyString, respBody, scope, req), interceptor.body )); - pendingMocks[idx].record.response = healModifiers( - pendingMocks[idx].record.response, - interceptorBody, - responseBody - ); + healModifiers(pendingMocks[idx].record, interceptorBody, responseBody, 'response'); // eslint-disable-next-line no-param-reassign interceptor.body = responseBody; } diff --git a/src/modules/request-recorder/heal-modifiers.js b/src/modules/request-recorder/heal-modifiers.js index 0c4220e6..bfd5ff19 100644 --- a/src/modules/request-recorder/heal-modifiers.js +++ b/src/modules/request-recorder/heal-modifiers.js @@ -28,10 +28,13 @@ const healer = objectScan(['**.*|*'], { afterFn: ({ context }) => context.actual[0] }); -export default (original, expected, actual) => healer( - original, - { - expected: [expected], - actual: [cloneDeep(actual)] - } -); +export default (original, expected, actual, field) => { + // eslint-disable-next-line no-param-reassign + original[field] = healer( + original[field], + { + expected: [expected], + actual: [cloneDeep(actual)] + } + ); +}; From b649d18d4cd9837222f4ef88cc4d9a37286cef3e Mon Sep 17 00:00:00 2001 From: Lukas Siemon Date: Thu, 11 Apr 2024 17:41:31 -0700 Subject: [PATCH 4/7] amend: minor --- src/modules/request-recorder/heal-modifiers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/request-recorder/heal-modifiers.js b/src/modules/request-recorder/heal-modifiers.js index bfd5ff19..e410de9c 100644 --- a/src/modules/request-recorder/heal-modifiers.js +++ b/src/modules/request-recorder/heal-modifiers.js @@ -5,7 +5,7 @@ const healer = objectScan(['**.*|*'], { breakFn: ({ isMatch, depth, property, context }) => { - if (property === undefined) { + if (depth === 0) { return false; } context.expected[depth] = context.expected[depth - 1]?.[property]; From 4af65adbf42951ab80b3c9e2edd1bfc33e8865e2 Mon Sep 17 00:00:00 2001 From: Lukas Siemon Date: Thu, 11 Apr 2024 17:59:42 -0700 Subject: [PATCH 5/7] fix: now allow top level modifier restore --- src/modules/request-recorder.js | 6 +++--- ...heal-modifiers.js => restore-modifiers.js} | 19 +++++++++---------- 2 files changed, 12 insertions(+), 13 deletions(-) rename src/modules/request-recorder/{heal-modifiers.js => restore-modifiers.js} (73%) diff --git a/src/modules/request-recorder.js b/src/modules/request-recorder.js index 643ef409..85a2543e 100644 --- a/src/modules/request-recorder.js +++ b/src/modules/request-recorder.js @@ -21,7 +21,7 @@ import { convertHeaders, rewriteHeaders } from './request-recorder/util.js'; -import healModifiers from './request-recorder/heal-modifiers.js'; +import restoreModifiers from './request-recorder/restore-modifiers.js'; const nockBack = nock.back; const nockRecorder = nock.recorder; @@ -179,7 +179,7 @@ export default (opts) => { if (anyFlagPresent(['magic', 'body'])) { const idx = pendingMocks.findIndex((m) => m.idx === scopeIdx); const requestBody = nullAsString(tryParseJson(body)); - healModifiers(pendingMocks[idx].record, scope.body, requestBody, 'body'); + restoreModifiers(pendingMocks[idx].record, scope.body, requestBody, 'body'); return scope.body; } return body; @@ -241,7 +241,7 @@ export default (opts) => { (respBody, fn) => fn(requestBodyString, respBody, scope, req), interceptor.body )); - healModifiers(pendingMocks[idx].record, interceptorBody, responseBody, 'response'); + restoreModifiers(pendingMocks[idx].record, interceptorBody, responseBody, 'response'); // eslint-disable-next-line no-param-reassign interceptor.body = responseBody; } diff --git a/src/modules/request-recorder/heal-modifiers.js b/src/modules/request-recorder/restore-modifiers.js similarity index 73% rename from src/modules/request-recorder/heal-modifiers.js rename to src/modules/request-recorder/restore-modifiers.js index e410de9c..3395a0e6 100644 --- a/src/modules/request-recorder/heal-modifiers.js +++ b/src/modules/request-recorder/restore-modifiers.js @@ -1,7 +1,7 @@ import objectScan from 'object-scan'; import cloneDeep from 'lodash.clonedeep'; -const healer = objectScan(['**.*|*'], { +const restorer = objectScan(['**.*|*'], { breakFn: ({ isMatch, depth, property, context }) => { @@ -10,7 +10,7 @@ const healer = objectScan(['**.*|*'], { } context.expected[depth] = context.expected[depth - 1]?.[property]; context.actual[depth] = context.actual[depth - 1]?.[property]; - return isMatch; + return isMatch || (depth === 1 && property !== context.field); }, filterFn: ({ context, depth, property, value @@ -29,12 +29,11 @@ const healer = objectScan(['**.*|*'], { }); export default (original, expected, actual, field) => { - // eslint-disable-next-line no-param-reassign - original[field] = healer( - original[field], - { - expected: [expected], - actual: [cloneDeep(actual)] - } - ); + const context = { + expected: [{ [field]: expected }], + actual: [{ [field]: cloneDeep(actual) }], + field + }; + const restored = restorer(original, context); + Object.assign(original, restored); }; From 06467056b50dfce6b58cb84a87e643c05442a1a8 Mon Sep 17 00:00:00 2001 From: Lukas Siemon Date: Thu, 11 Apr 2024 18:02:46 -0700 Subject: [PATCH 6/7] amend: minor --- src/modules/request-recorder.js | 4 ++-- src/modules/request-recorder/restore-modifiers.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/modules/request-recorder.js b/src/modules/request-recorder.js index 85a2543e..eb6f6ff2 100644 --- a/src/modules/request-recorder.js +++ b/src/modules/request-recorder.js @@ -179,7 +179,7 @@ export default (opts) => { if (anyFlagPresent(['magic', 'body'])) { const idx = pendingMocks.findIndex((m) => m.idx === scopeIdx); const requestBody = nullAsString(tryParseJson(body)); - restoreModifiers(pendingMocks[idx].record, scope.body, requestBody, 'body'); + restoreModifiers(pendingMocks[idx].record, 'body', scope.body, requestBody); return scope.body; } return body; @@ -241,7 +241,7 @@ export default (opts) => { (respBody, fn) => fn(requestBodyString, respBody, scope, req), interceptor.body )); - restoreModifiers(pendingMocks[idx].record, interceptorBody, responseBody, 'response'); + restoreModifiers(pendingMocks[idx].record, 'response', interceptorBody, responseBody); // eslint-disable-next-line no-param-reassign interceptor.body = responseBody; } diff --git a/src/modules/request-recorder/restore-modifiers.js b/src/modules/request-recorder/restore-modifiers.js index 3395a0e6..f83f9489 100644 --- a/src/modules/request-recorder/restore-modifiers.js +++ b/src/modules/request-recorder/restore-modifiers.js @@ -28,7 +28,7 @@ const restorer = objectScan(['**.*|*'], { afterFn: ({ context }) => context.actual[0] }); -export default (original, expected, actual, field) => { +export default (original, field, expected, actual) => { const context = { expected: [{ [field]: expected }], actual: [{ [field]: cloneDeep(actual) }], From 0be6b30064bda8b6fbad6afdc2ea1b47bcbef821 Mon Sep 17 00:00:00 2001 From: Lukas Siemon Date: Thu, 11 Apr 2024 18:06:01 -0700 Subject: [PATCH 7/7] amend: minor --- src/modules/request-recorder.js | 6 +++--- ...restore-modifiers.js => update-and-restore-modifiers.js} | 0 2 files changed, 3 insertions(+), 3 deletions(-) rename src/modules/request-recorder/{restore-modifiers.js => update-and-restore-modifiers.js} (100%) diff --git a/src/modules/request-recorder.js b/src/modules/request-recorder.js index eb6f6ff2..fb23378f 100644 --- a/src/modules/request-recorder.js +++ b/src/modules/request-recorder.js @@ -21,7 +21,7 @@ import { convertHeaders, rewriteHeaders } from './request-recorder/util.js'; -import restoreModifiers from './request-recorder/restore-modifiers.js'; +import updateAndRestoreModifiers from './request-recorder/update-and-restore-modifiers.js'; const nockBack = nock.back; const nockRecorder = nock.recorder; @@ -179,7 +179,7 @@ export default (opts) => { if (anyFlagPresent(['magic', 'body'])) { const idx = pendingMocks.findIndex((m) => m.idx === scopeIdx); const requestBody = nullAsString(tryParseJson(body)); - restoreModifiers(pendingMocks[idx].record, 'body', scope.body, requestBody); + updateAndRestoreModifiers(pendingMocks[idx].record, 'body', scope.body, requestBody); return scope.body; } return body; @@ -241,7 +241,7 @@ export default (opts) => { (respBody, fn) => fn(requestBodyString, respBody, scope, req), interceptor.body )); - restoreModifiers(pendingMocks[idx].record, 'response', interceptorBody, responseBody); + updateAndRestoreModifiers(pendingMocks[idx].record, 'response', interceptorBody, responseBody); // eslint-disable-next-line no-param-reassign interceptor.body = responseBody; } diff --git a/src/modules/request-recorder/restore-modifiers.js b/src/modules/request-recorder/update-and-restore-modifiers.js similarity index 100% rename from src/modules/request-recorder/restore-modifiers.js rename to src/modules/request-recorder/update-and-restore-modifiers.js