Skip to content

Commit

Permalink
Merge pull request #1431 from blackflux/dev
Browse files Browse the repository at this point in the history
[Gally]: master <- dev
  • Loading branch information
simlu authored Apr 12, 2024
2 parents 80dfb20 + 07f3ccd commit cd4b83f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 6 additions & 10 deletions src/modules/request-recorder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -22,7 +21,7 @@ import {
convertHeaders,
rewriteHeaders
} from './request-recorder/util.js';
import healBody from './request-recorder/heal-body.js';
import updateAndRestoreModifiers from './request-recorder/update-and-restore-modifiers.js';

const nockBack = nock.back;
const nockRecorder = nock.recorder;
Expand Down Expand Up @@ -180,9 +179,7 @@ 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);
}
updateAndRestoreModifiers(pendingMocks[idx].record, 'body', scope.body, requestBody);
return scope.body;
}
return body;
Expand Down Expand Up @@ -237,17 +234,16 @@ 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)) {
// eslint-disable-next-line no-param-reassign
interceptor.body = responseBody;
pendingMocks[idx].record.response = responseBody;
}
updateAndRestoreModifiers(pendingMocks[idx].record, 'response', interceptorBody, responseBody);
// eslint-disable-next-line no-param-reassign
interceptor.body = responseBody;
}

expectedCassette.push(pendingMocks[idx].record);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import objectScan from 'object-scan';
import cloneDeep from 'lodash.clonedeep';

const healer = objectScan(['**.*|*'], {
const restorer = objectScan(['**.*|*'], {
breakFn: ({
isMatch, depth, property, context
}) => {
if (property === undefined) {
if (depth === 0) {
return false;
}
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
Expand All @@ -28,10 +28,12 @@ const healer = objectScan(['**.*|*'], {
afterFn: ({ context }) => context.actual[0]
});

export default (original, expected, actual) => healer(
original,
{
expected: [expected],
actual: [cloneDeep(actual)]
}
);
export default (original, field, expected, actual) => {
const context = {
expected: [{ [field]: expected }],
actual: [{ [field]: cloneDeep(actual) }],
field
};
const restored = restorer(original, context);
Object.assign(original, restored);
};

0 comments on commit cd4b83f

Please sign in to comment.