Skip to content

Commit 6c216ac

Browse files
authored
Merge pull request #1421 from blackflux/dev
[Gally]: master <- dev
2 parents 832b756 + 4bcfbb3 commit 6c216ac

File tree

3 files changed

+67
-23
lines changed

3 files changed

+67
-23
lines changed

src/modules/request-recorder.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,9 @@ export default (opts) => {
178178
scope.filteringRequestBody = (body) => {
179179
if (anyFlagPresent(['magic', 'body'])) {
180180
const idx = pendingMocks.findIndex((m) => m.idx === scopeIdx);
181-
const requestBody = tryParseJson(body);
182-
const requestBodyStr = nullAsString(requestBody);
183-
if (!isEqual(scope.body, requestBodyStr)) {
184-
pendingMocks[idx].record.body = requestBodyStr;
181+
const requestBody = nullAsString(tryParseJson(body));
182+
if (!isEqual(scope.body, requestBody)) {
183+
pendingMocks[idx].record.body = requestBody;
185184
}
186185
return scope.body;
187186
}
@@ -215,6 +214,18 @@ export default (opts) => {
215214
interceptor.delayBody(pendingMocks[idx].record.delayBody);
216215
}
217216

217+
const hasSentBody = requestBodyString !== undefined;
218+
// eslint-disable-next-line no-underscore-dangle
219+
const hasRecordBody = interceptor._requestBody !== undefined;
220+
if (hasSentBody !== hasRecordBody) {
221+
if (anyFlagPresent(['magic', 'body'])) {
222+
pendingMocks[idx].record.body = nullAsString(tryParseJson(requestBodyString));
223+
} else {
224+
// eslint-disable-next-line no-param-reassign
225+
interceptor.errorMessage = 'Recording body mismatch';
226+
}
227+
}
228+
218229
if (anyFlagPresent(['magic', 'headers'])) {
219230
// add new headers
220231
const reqheaders = {

src/modules/request-recorder/apply-modifiers.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ export default (input, modifiers) => {
99
const unknownModifiers = modifierNames
1010
.filter((n) => typeof modifiers[n] !== 'function');
1111
if (unknownModifiers.length !== 0) {
12-
// eslint-disable-next-line no-console
13-
console.warn(`Unknown Modifier(s) detected: ${unknownModifiers.join(', ')}`);
14-
return;
12+
throw new Error(`Unknown Modifier(s) detected: ${unknownModifiers.join(', ')}`);
1513
}
1614
// eslint-disable-next-line no-param-reassign
1715
delete parent[k];

test/modules/request-recorder.spec.js

+51-16
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe('Testing RequestRecorder', { useTmpDir: true, timestamp: 0 }, () => {
186186
expect(recorder.get()).to.deep.equal([]);
187187
});
188188

189-
it('Testing unknown modifiers (top level)', async ({ recorder }) => {
189+
it('Testing unknown modifiers (top level)', async ({ capture }) => {
190190
prepareCassette({
191191
'response|jsonStringify|toBase64': {},
192192
'body|jsonStringify|toBase64': {
@@ -195,18 +195,19 @@ describe('Testing RequestRecorder', { useTmpDir: true, timestamp: 0 }, () => {
195195
}
196196
}
197197
});
198-
await validate({
198+
const err = await capture(() => validate({
199199
json: true,
200-
body: undefined,
201-
response: ''
202-
});
203-
expect(recorder.get()).to.deep.equal([
204-
'Unknown Modifier(s) detected: jsonStringify, toBase64',
205-
'Unknown Modifier(s) detected: jsonStringify, toBase64'
206-
]);
200+
body: {
201+
payload: {
202+
key: 'value'
203+
}
204+
},
205+
response: {}
206+
}));
207+
expect(err.message).to.deep.equal('Unknown Modifier(s) detected: jsonStringify, toBase64');
207208
});
208209

209-
it('Testing unknown modifiers (nested)', async ({ recorder }) => {
210+
it('Testing unknown modifiers (nested)', async ({ capture }) => {
210211
prepareCassette({
211212
response: {},
212213
body: {
@@ -215,18 +216,17 @@ describe('Testing RequestRecorder', { useTmpDir: true, timestamp: 0 }, () => {
215216
}
216217
}
217218
});
218-
await validate({
219+
220+
const err = await capture(() => validate({
219221
json: true,
220222
body: {
221-
'payload|jsonStringify|toBase64': {
223+
payload: {
222224
key: 'value'
223225
}
224226
},
225227
response: {}
226-
});
227-
expect(recorder.get()).to.deep.equal([
228-
'Unknown Modifier(s) detected: jsonStringify, toBase64'
229-
]);
228+
}));
229+
expect(err.message).to.deep.equal('Unknown Modifier(s) detected: jsonStringify, toBase64');
230230
});
231231
});
232232

@@ -317,6 +317,41 @@ describe('Testing RequestRecorder', { useTmpDir: true, timestamp: 0 }, () => {
317317
await runner('body', { body: null });
318318
});
319319

320+
it('Testing body healing with missing body', async () => {
321+
await runner('body', {
322+
body: {},
323+
cassetteContent: [
324+
{
325+
scope: server.uri,
326+
method: 'GET',
327+
path: '/?q=1',
328+
status: 200,
329+
reqheaders: {},
330+
response: { data: '1' },
331+
responseIsBinary: false
332+
}
333+
]
334+
});
335+
});
336+
337+
it('Testing missing body error', async ({ capture }) => {
338+
const err = await capture(() => runner(false, {
339+
body: {},
340+
cassetteContent: [
341+
{
342+
scope: server.uri,
343+
method: 'GET',
344+
path: '/?q=1',
345+
status: 200,
346+
reqheaders: {},
347+
response: { data: '1' },
348+
responseIsBinary: false
349+
}
350+
]
351+
}));
352+
expect(err.message).to.deep.equal('Recording body mismatch');
353+
});
354+
320355
it('Testing body healing with mismatched request method', async () => {
321356
await runner('body', { raises: true, heals: false, method: 'POST' });
322357
});

0 commit comments

Comments
 (0)