@mablhq/mabl-cli 1.54.3 → 1.55.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,113 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.restoreCustomRequestFields = exports.deduplicateApiTestExecutionResults = void 0;
6
+ exports.isJson = exports.isFolder = exports.validateCollectionFeatures = exports.generateRawScriptField = exports.processPostmanItems = exports.createEmptyRequest = exports.createEmptyRequestEventArray = exports.splitLines = exports.PRE_REQUEST_LISTEN = exports.insertMablAssertionScripts = exports.toPostmanScript = exports.replaceVariables = exports.getActualValue = exports.readBody = exports.getResponseContentType = exports.valueToUnquotedString = exports.humanizeAssertion = exports.getAssertionTargetArgument = exports.assertionHasTest = exports.isValidMablAssertion = exports.normalizeExpectedValue = exports.assertionRequiresValue = exports.filterValidMablAssertions = exports.filterValidMablVariableAssignments = exports.isValidMablVariableAssignment = exports.removeMablGeneratedScripts = exports.createEmptyRequestEvent = exports.findFirstMatchingEvent = exports.restoreCustomRequestFields = exports.deduplicateApiTestExecutionResults = exports.createEmptyVariable = exports.createEmptyAssertion = exports.humanizeAssertionType = exports.getAssertionTypesForTarget = exports.caseInsensitiveEquals = exports.compareStringsCaseInsensitive = exports.ASSERT_TYPES = exports.ASSERT_TARGETS = exports.COLLECTION_WITH_FOLDERS_ERROR = exports.HMAC_SHA1_SIGNATURE = exports.DEFAULT_ADD_OAUTH1_TO_HEADER_VALUE = exports.DEFAULT_OAUTH1_VERSION = exports.OAUTH1_AUTH = exports.NO_AUTH = exports.BEARER_TOKEN_AUTH = exports.BASIC_AUTH = exports.API_KEY_AUTH = exports.INHERIT_AUTH_FROM_PARENT_VALUE = exports.MABL_GENERATED_ASSERTION_TOKEN = exports.TEST_LISTEN = void 0;
7
+ exports.processPostmanItem = exports.replaceRawScriptWithExecArray = exports.removeInvalidHeaders = exports.processItemAuth = exports.mablAuthToPostman = exports.formatContent = exports.getRequestMode = exports.getFormDataArray = exports.isText = exports.isXML = void 0;
8
+ const newman_types_1 = require("./newman-types");
9
+ const lodash_1 = __importDefault(require("lodash"));
10
+ const uuid_1 = require("uuid");
11
+ exports.TEST_LISTEN = 'test';
12
+ exports.MABL_GENERATED_ASSERTION_TOKEN = '__MABL_GENERATED__';
13
+ exports.INHERIT_AUTH_FROM_PARENT_VALUE = 'inherit_from_parent';
14
+ exports.API_KEY_AUTH = 'apikey';
15
+ exports.BASIC_AUTH = 'basic';
16
+ exports.BEARER_TOKEN_AUTH = 'bearer';
17
+ exports.NO_AUTH = 'noauth';
18
+ exports.OAUTH1_AUTH = 'oauth1';
19
+ exports.DEFAULT_OAUTH1_VERSION = '1.0';
20
+ const ADD_OAUTH1_TO_HEADER = true;
21
+ const ADD_OAUTH1_TO_HEADER_VALUE = `${ADD_OAUTH1_TO_HEADER}`;
22
+ exports.DEFAULT_ADD_OAUTH1_TO_HEADER_VALUE = ADD_OAUTH1_TO_HEADER_VALUE;
23
+ exports.HMAC_SHA1_SIGNATURE = 'HMAC-SHA1';
24
+ exports.COLLECTION_WITH_FOLDERS_ERROR = 'Collections with folders are not supported';
25
+ const RESPONSE_PROPERTIES = {
26
+ Size: 'size().body',
27
+ Status: 'code',
28
+ };
29
+ exports.ASSERT_TARGETS = [
30
+ { label: 'Header', value: newman_types_1.AssertionTarget.Header },
31
+ { label: 'Status', value: newman_types_1.AssertionTarget.Status },
32
+ { label: 'Size', value: newman_types_1.AssertionTarget.Size },
33
+ { label: 'JSON Body', value: newman_types_1.AssertionTarget.JSONBody },
34
+ ];
35
+ exports.ASSERT_TYPES = [
36
+ { label: 'Equals', value: newman_types_1.AssertionType.Equals },
37
+ { label: 'Not equals', value: newman_types_1.AssertionType.NotEquals },
38
+ { label: 'Contains', value: newman_types_1.AssertionType.Contains },
39
+ { label: 'Not contains', value: newman_types_1.AssertionType.DoesNotContain },
40
+ { label: 'Is present', value: newman_types_1.AssertionType.Present },
41
+ { label: 'Not present', value: newman_types_1.AssertionType.NotPresent },
42
+ ];
43
+ const EXPECT_FUNCTIONS = {
44
+ Equals: 'eql',
45
+ NotEquals: 'not.eql',
46
+ Contains: 'include',
47
+ DoesNotContain: 'not.include',
48
+ Present: 'not.be.undefined',
49
+ NotPresent: 'be.undefined',
50
+ };
51
+ function compareStringsCaseInsensitive(a, b) {
52
+ a = a === null || a === void 0 ? void 0 : a.toLowerCase();
53
+ b = b === null || b === void 0 ? void 0 : b.toLowerCase();
54
+ if (!a) {
55
+ return -1;
56
+ }
57
+ if (!b) {
58
+ return 1;
59
+ }
60
+ return a.localeCompare(b, undefined, { sensitivity: 'base' });
61
+ }
62
+ exports.compareStringsCaseInsensitive = compareStringsCaseInsensitive;
63
+ function caseInsensitiveEquals(a, b) {
64
+ return compareStringsCaseInsensitive(a, b) === 0;
65
+ }
66
+ exports.caseInsensitiveEquals = caseInsensitiveEquals;
67
+ function getAssertionTypesForTarget(target) {
68
+ switch (target) {
69
+ case newman_types_1.AssertionTarget.Size:
70
+ case newman_types_1.AssertionTarget.Status:
71
+ return exports.ASSERT_TYPES.filter((assertType) => assertType.value === newman_types_1.AssertionType.Equals ||
72
+ assertType.value === newman_types_1.AssertionType.NotEquals);
73
+ default:
74
+ return exports.ASSERT_TYPES;
75
+ }
76
+ }
77
+ exports.getAssertionTypesForTarget = getAssertionTypesForTarget;
78
+ function humanizeAssertionType(assertionType) {
79
+ switch (assertionType) {
80
+ case 'NotEquals':
81
+ return 'not equals';
82
+ case 'DoesNotContain':
83
+ return 'does not contain';
84
+ case 'Present':
85
+ return 'is present';
86
+ case 'NotPresent':
87
+ return 'is not present';
88
+ default:
89
+ return assertionType.toString().toLowerCase();
90
+ }
91
+ }
92
+ exports.humanizeAssertionType = humanizeAssertionType;
93
+ function createEmptyAssertion() {
94
+ return {
95
+ id: (0, uuid_1.v4)(),
96
+ assertTarget: newman_types_1.AssertionTarget.JSONBody,
97
+ assertType: newman_types_1.AssertionType.Present,
98
+ description: '',
99
+ };
100
+ }
101
+ exports.createEmptyAssertion = createEmptyAssertion;
102
+ function createEmptyVariable() {
103
+ return {
104
+ id: (0, uuid_1.v4)(),
105
+ assertTarget: newman_types_1.AssertionTarget.JSONBody,
106
+ variableName: '',
107
+ description: '',
108
+ };
109
+ }
110
+ exports.createEmptyVariable = createEmptyVariable;
4
111
  function deduplicateApiTestExecutionResults(postmanResult) {
5
112
  var _a;
6
113
  const executionsById = {};
@@ -56,3 +163,676 @@ function restoreFormDataFileMetadata(resultRequest, collectionRequest) {
56
163
  }
57
164
  }
58
165
  }
166
+ function findFirstMatchingEvent(eventDefinition, listen) {
167
+ const eventIndex = lodash_1.default.findIndex(eventDefinition, { listen });
168
+ let eventValue;
169
+ if (eventIndex !== -1) {
170
+ eventValue = eventDefinition === null || eventDefinition === void 0 ? void 0 : eventDefinition[eventIndex];
171
+ }
172
+ return { eventIndex, eventValue };
173
+ }
174
+ exports.findFirstMatchingEvent = findFirstMatchingEvent;
175
+ function createEmptyRequestEvent() {
176
+ return {
177
+ listen: exports.TEST_LISTEN,
178
+ script: {
179
+ exec: [],
180
+ raw: '',
181
+ type: 'text/javascript',
182
+ },
183
+ mablAssertions: [
184
+ {
185
+ id: (0, uuid_1.v4)(),
186
+ assertTarget: newman_types_1.AssertionTarget.Status,
187
+ assertType: newman_types_1.AssertionType.Equals,
188
+ value: '200',
189
+ },
190
+ ],
191
+ mablVariables: [],
192
+ };
193
+ }
194
+ exports.createEmptyRequestEvent = createEmptyRequestEvent;
195
+ function removeMablGeneratedScriptsFromLines(lines) {
196
+ const mablGeneratedIndex = lines.findIndex((line) => line.includes(exports.MABL_GENERATED_ASSERTION_TOKEN));
197
+ if (mablGeneratedIndex >= 0) {
198
+ return lines.slice(0, mablGeneratedIndex);
199
+ }
200
+ return lines;
201
+ }
202
+ function removeMablGeneratedScripts(eventDefinition) {
203
+ var _a, _b;
204
+ lodash_1.default.set(eventDefinition, 'script.exec', removeMablGeneratedScriptsFromLines((_b = (_a = eventDefinition.script) === null || _a === void 0 ? void 0 : _a.exec) !== null && _b !== void 0 ? _b : []));
205
+ }
206
+ exports.removeMablGeneratedScripts = removeMablGeneratedScripts;
207
+ function isValidMablVariableAssignment(variable) {
208
+ return !!variable.assertTarget && !!variable.variableName;
209
+ }
210
+ exports.isValidMablVariableAssignment = isValidMablVariableAssignment;
211
+ function filterValidMablVariableAssignments(variables) {
212
+ return variables === null || variables === void 0 ? void 0 : variables.filter(isValidMablVariableAssignment);
213
+ }
214
+ exports.filterValidMablVariableAssignments = filterValidMablVariableAssignments;
215
+ function filterValidMablAssertions(assertions) {
216
+ return assertions === null || assertions === void 0 ? void 0 : assertions.filter(isValidMablAssertion);
217
+ }
218
+ exports.filterValidMablAssertions = filterValidMablAssertions;
219
+ function assertionRequiresValue(assertion) {
220
+ switch (assertion.assertType) {
221
+ case newman_types_1.AssertionType.Present:
222
+ case newman_types_1.AssertionType.NotPresent:
223
+ return false;
224
+ default:
225
+ return true;
226
+ }
227
+ }
228
+ exports.assertionRequiresValue = assertionRequiresValue;
229
+ function normalizeExpectedValue(value) {
230
+ if (value === undefined) {
231
+ return;
232
+ }
233
+ value = value.trim();
234
+ if (value.match(/".*"/)) {
235
+ return `pm.variables.replaceIn(${value})`;
236
+ }
237
+ const lower = value.toLowerCase();
238
+ if (lower === 'true' || lower === 'false' || !Number.isNaN(Number(value))) {
239
+ return JSON.stringify(value);
240
+ }
241
+ return `pm.variables.replaceIn(${JSON.stringify(value)})`;
242
+ }
243
+ exports.normalizeExpectedValue = normalizeExpectedValue;
244
+ function isValidMablAssertion(assertion) {
245
+ var _a, _b, _c, _d, _e, _f, _g, _h;
246
+ if (!assertion.assertType) {
247
+ return false;
248
+ }
249
+ const assertTarget = assertion.assertTarget;
250
+ const requiresValue = assertionRequiresValue(assertion);
251
+ const expectedValue = normalizeExpectedValue(assertion.value);
252
+ const hasRequiredValue = !requiresValue || ((_a = expectedValue === null || expectedValue === void 0 ? void 0 : expectedValue.length) !== null && _a !== void 0 ? _a : 0) > 0;
253
+ let valid = false;
254
+ switch (assertTarget) {
255
+ case 'JSONBody':
256
+ const hasBodyPath = ((_d = (_c = (_b = assertion.bodyPath) === null || _b === void 0 ? void 0 : _b.trim()) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) > 0;
257
+ valid = hasBodyPath && hasRequiredValue;
258
+ break;
259
+ case 'Status':
260
+ valid = ((_e = expectedValue === null || expectedValue === void 0 ? void 0 : expectedValue.length) !== null && _e !== void 0 ? _e : 0) > 0;
261
+ break;
262
+ case 'Header':
263
+ const hasHeaderName = ((_g = (_f = assertion.headerName) === null || _f === void 0 ? void 0 : _f.length) !== null && _g !== void 0 ? _g : 0) > 0;
264
+ valid = hasHeaderName && hasRequiredValue;
265
+ break;
266
+ case 'Size':
267
+ valid = ((_h = expectedValue === null || expectedValue === void 0 ? void 0 : expectedValue.length) !== null && _h !== void 0 ? _h : 0) > 0;
268
+ break;
269
+ }
270
+ return valid;
271
+ }
272
+ exports.isValidMablAssertion = isValidMablAssertion;
273
+ function assertionHasTest(assertion) {
274
+ if (!assertion.assertTarget || !assertion.assertType) {
275
+ return false;
276
+ }
277
+ switch (assertion.assertTarget) {
278
+ case 'Header':
279
+ return !!assertion.headerName;
280
+ case 'JSONBody':
281
+ return !!assertion.bodyPath;
282
+ default:
283
+ return !!assertion.assertType && !!assertion.value;
284
+ }
285
+ }
286
+ exports.assertionHasTest = assertionHasTest;
287
+ function getAssertionTargetArgument(assertion) {
288
+ switch (assertion.assertTarget) {
289
+ case 'JSONBody':
290
+ return assertion.bodyPath;
291
+ case 'Header':
292
+ return assertion.headerName;
293
+ default:
294
+ return;
295
+ }
296
+ }
297
+ exports.getAssertionTargetArgument = getAssertionTargetArgument;
298
+ function humanizeAssertion(assertion) {
299
+ const { assertTarget, assertType, value, description } = assertion;
300
+ if (description && description !== '') {
301
+ return description;
302
+ }
303
+ if (assertionHasTest(assertion)) {
304
+ const assertionTargetArgument = getAssertionTargetArgument(assertion);
305
+ const humanized = `${exports.ASSERT_TARGETS.filter((target) => target.value === assertTarget)[0].label}${assertionTargetArgument ? ` ${assertionTargetArgument}` : ''} ${humanizeAssertionType(assertType)}`;
306
+ return assertionRequiresValue(assertion)
307
+ ? `${humanized} ${value}`
308
+ : humanized;
309
+ }
310
+ return '';
311
+ }
312
+ exports.humanizeAssertion = humanizeAssertion;
313
+ function generateAssertionScript(assertion) {
314
+ if (!assertion.assertType) {
315
+ return;
316
+ }
317
+ const assertTarget = assertion.assertTarget;
318
+ const assertType = assertion.assertType;
319
+ const description = humanizeAssertion(assertion);
320
+ const requiresValue = assertionRequiresValue(assertion);
321
+ const expectedValue = normalizeExpectedValue(assertion.value);
322
+ switch (assertTarget) {
323
+ case 'JSONBody':
324
+ return generateJsonBodyAssertion(description, assertType, requiresValue, assertion.bodyPath, expectedValue);
325
+ case 'Status':
326
+ return generateStatusAssertion(description, assertType, expectedValue);
327
+ case 'Header':
328
+ return generateHeaderAssertion(description, assertType, assertion.headerName, expectedValue);
329
+ case 'Size':
330
+ return generateSizeAssertion(description, assertTarget, assertType, expectedValue);
331
+ }
332
+ return;
333
+ }
334
+ function generateVariableAssignmentScript(variable) {
335
+ if (!variable.variableName) {
336
+ return;
337
+ }
338
+ const assertTarget = variable.assertTarget;
339
+ switch (assertTarget) {
340
+ case 'JSONBody':
341
+ return generateJsonBodyVariableAssignment(variable.variableName, variable.bodyPath);
342
+ case 'Header':
343
+ return generateHeaderVariableAssignment(variable.variableName, variable.headerName);
344
+ default:
345
+ return generateSimpleVariableAssignment(variable.variableName, assertTarget);
346
+ }
347
+ }
348
+ function generateJsonBodyAssertion(description, assertType, requiresValues, path, expectedValue) {
349
+ if (!(path === null || path === void 0 ? void 0 : path.trim().length) || (requiresValues && !(expectedValue === null || expectedValue === void 0 ? void 0 : expectedValue.length))) {
350
+ return;
351
+ }
352
+ let propertyAccessor = getJsonBodyProperty(path);
353
+ if (requiresValues) {
354
+ propertyAccessor = valueToUnquotedString(propertyAccessor);
355
+ }
356
+ const expect = EXPECT_FUNCTIONS[assertType];
357
+ let test = `pm.expect(${propertyAccessor}).to.${expect}`;
358
+ if (requiresValues) {
359
+ test += `(${expectedValue})`;
360
+ }
361
+ return generatePostmanTest(description, test);
362
+ }
363
+ function generateJsonBodyVariableAssignment(variableName, path) {
364
+ return generatePostmanVariableAssignment(variableName, getJsonBodyProperty(path));
365
+ }
366
+ function getJsonBodyProperty(path) {
367
+ const jsonBodyAccessor = 'pm.response.json()';
368
+ return path
369
+ ? `_.get(${jsonBodyAccessor}, pm.variables.replaceIn('${path}'))`
370
+ : jsonBodyAccessor;
371
+ }
372
+ function generateHeaderVariableAssignment(variableName, headerName) {
373
+ if (!headerName) {
374
+ return;
375
+ }
376
+ return generatePostmanVariableAssignment(variableName, generateGetHeaderValue(headerName));
377
+ }
378
+ function generateGetHeaderValue(headerName) {
379
+ return `pm.response.headers.get(${JSON.stringify(headerName)})`;
380
+ }
381
+ function generateStatusAssertion(description, assertType, expectedValue) {
382
+ if (!(expectedValue === null || expectedValue === void 0 ? void 0 : expectedValue.length)) {
383
+ return;
384
+ }
385
+ let expect;
386
+ let test;
387
+ switch (assertType) {
388
+ case 'Equals':
389
+ expect = 'to.have.status';
390
+ break;
391
+ case 'NotEquals':
392
+ expect = 'to.not.have.status';
393
+ break;
394
+ case 'Contains':
395
+ expect = 'to.include';
396
+ test = `pm.expect(pm.response.code.toString()).${expect}(${expectedValue})`;
397
+ break;
398
+ case 'DoesNotContain':
399
+ expect = 'to.not.include';
400
+ test = `pm.expect(pm.response.code.toString()).${expect}(${expectedValue})`;
401
+ break;
402
+ default:
403
+ throw new Error(`Unexpected status assert type: ${assertType}`);
404
+ }
405
+ return generatePostmanTest(description, test !== null && test !== void 0 ? test : `pm.response.${expect}(parseInt(${expectedValue}))`);
406
+ }
407
+ function generateHeaderAssertion(description, assertType, headerName, expectedValue) {
408
+ if (!headerName) {
409
+ return;
410
+ }
411
+ const normalizedHeaderName = JSON.stringify(headerName);
412
+ let test;
413
+ const expect = EXPECT_FUNCTIONS[assertType];
414
+ switch (assertType) {
415
+ case 'Present':
416
+ test = `pm.response.to.have.header(${normalizedHeaderName})`;
417
+ break;
418
+ case 'NotPresent':
419
+ test = `pm.response.to.not.have.header(${normalizedHeaderName})`;
420
+ break;
421
+ case 'Equals':
422
+ break;
423
+ case 'NotEquals':
424
+ break;
425
+ case 'Contains':
426
+ break;
427
+ case 'DoesNotContain':
428
+ break;
429
+ default:
430
+ throw new Error(`Unexpected header assert type: ${assertType}`);
431
+ }
432
+ return generatePostmanTest(description, test !== null && test !== void 0 ? test : `pm.expect(pm.response.headers.get(${normalizedHeaderName})).to.${expect}(${expectedValue})`);
433
+ }
434
+ function generateSizeAssertion(description, target, type, expectedValue) {
435
+ if (!(expectedValue === null || expectedValue === void 0 ? void 0 : expectedValue.length)) {
436
+ return;
437
+ }
438
+ return generateSimpleAssertion(description, target, type, `parseInt(${expectedValue})`);
439
+ }
440
+ function generateSimpleAssertion(description, target, type, expectedValue) {
441
+ const expect = EXPECT_FUNCTIONS[type];
442
+ return generatePostmanTest(description, `pm.expect(${getResponseProperty(target)}).to.${expect}(${expectedValue})`);
443
+ }
444
+ function generateSimpleVariableAssignment(name, target) {
445
+ return generatePostmanVariableAssignment(name, getResponseProperty(target));
446
+ }
447
+ function getResponseProperty(target) {
448
+ const property = RESPONSE_PROPERTIES[target];
449
+ if (!property) {
450
+ throw new Error(`No response property found for target [${target}]`);
451
+ }
452
+ return `pm.response.${property}`;
453
+ }
454
+ function generatePostmanTest(description, test) {
455
+ return `pm.test(${JSON.stringify(description)}, function () { ${test}; });`;
456
+ }
457
+ function generatePostmanVariableAssignment(name, value) {
458
+ return `pm.environment.set(${JSON.stringify(name)}, ${valueToUnquotedString(value)});`;
459
+ }
460
+ function valueToUnquotedString(value) {
461
+ return `JSON.stringify(${value})?.replace(/^"(.+(?="$))"$/, '$1')`;
462
+ }
463
+ exports.valueToUnquotedString = valueToUnquotedString;
464
+ function getResponseContentType(response) {
465
+ var _a, _b;
466
+ const contentType = (_b = (_a = response === null || response === void 0 ? void 0 : response.header) === null || _a === void 0 ? void 0 : _a.find((header) => header.key.toLowerCase() === 'content-type')) === null || _b === void 0 ? void 0 : _b.value;
467
+ return contentType === null || contentType === void 0 ? void 0 : contentType.toLowerCase();
468
+ }
469
+ exports.getResponseContentType = getResponseContentType;
470
+ function readBody(body, contentType) {
471
+ var _a;
472
+ const bodyLength = (_a = body === null || body === void 0 ? void 0 : body.data) === null || _a === void 0 ? void 0 : _a.length;
473
+ if (!body || !bodyLength) {
474
+ return;
475
+ }
476
+ if (!contentType) {
477
+ return `${bodyLength} bytes of data with unknown type`;
478
+ }
479
+ else if (isJson(contentType)) {
480
+ let bodyAsString = `${bodyLength} bytes of data`;
481
+ try {
482
+ bodyAsString = Buffer.from(body.data).toString();
483
+ }
484
+ catch (e) {
485
+ console.error('Unable to convert body.data to string', e);
486
+ return bodyAsString;
487
+ }
488
+ try {
489
+ return JSON.parse(bodyAsString);
490
+ }
491
+ catch (e) {
492
+ console.error('Unable to parse body into JSON object.', e);
493
+ return bodyAsString;
494
+ }
495
+ }
496
+ else {
497
+ try {
498
+ return Buffer.from(body.data).toString();
499
+ }
500
+ catch (e) {
501
+ console.error('Unable to convert body.data to string', e);
502
+ return `${bodyLength} bytes of data`;
503
+ }
504
+ }
505
+ }
506
+ exports.readBody = readBody;
507
+ function getActualValue(assertion, apiStepExecutionResult, variables) {
508
+ var _a, _b, _c, _d;
509
+ const { assertTarget, bodyPath, headerName } = assertion;
510
+ switch (assertTarget) {
511
+ case newman_types_1.AssertionTarget.Header:
512
+ return lodash_1.default.find((_a = apiStepExecutionResult.response) === null || _a === void 0 ? void 0 : _a.header, (header) => caseInsensitiveEquals(header.key, headerName));
513
+ case newman_types_1.AssertionTarget.JSONBody:
514
+ const contentType = getResponseContentType(apiStepExecutionResult.response);
515
+ const content = readBody((_b = apiStepExecutionResult.response) === null || _b === void 0 ? void 0 : _b.stream, contentType);
516
+ return bodyPath
517
+ ? lodash_1.default.get(content, replaceVariables(bodyPath, variables))
518
+ : content;
519
+ case newman_types_1.AssertionTarget.Size:
520
+ return (_c = apiStepExecutionResult.response) === null || _c === void 0 ? void 0 : _c.responseSize;
521
+ case newman_types_1.AssertionTarget.Status:
522
+ return (_d = apiStepExecutionResult.response) === null || _d === void 0 ? void 0 : _d.code;
523
+ }
524
+ return null;
525
+ }
526
+ exports.getActualValue = getActualValue;
527
+ function replaceVariables(value, variables) {
528
+ var _a, _b;
529
+ let toReplace = value;
530
+ let matches;
531
+ while ((matches = toReplace.match(new RegExp('{{@(?<name>[^{}]+)}}')))) {
532
+ const variableName = (_a = matches.groups) === null || _a === void 0 ? void 0 : _a.name;
533
+ const variableValue = (_b = (variableName && variables[variableName])) !== null && _b !== void 0 ? _b : '';
534
+ toReplace = toReplace.replace(new RegExp(`{{@${variableName}}}`, 'g'), variableValue);
535
+ }
536
+ return toReplace;
537
+ }
538
+ exports.replaceVariables = replaceVariables;
539
+ function toPostmanScript(assertion) {
540
+ return [
541
+ generateAssertionScript(assertion),
542
+ generateVariableAssignmentScript(assertion),
543
+ ]
544
+ .filter((script) => script)
545
+ .map((script) => script);
546
+ }
547
+ exports.toPostmanScript = toPostmanScript;
548
+ function insertMablAssertionScripts(item) {
549
+ var _a, _b, _c, _d, _e, _f;
550
+ const { eventValue: existingEventValue } = findFirstMatchingEvent(item.event, exports.TEST_LISTEN);
551
+ const eventValue = existingEventValue !== null && existingEventValue !== void 0 ? existingEventValue : createEmptyRequestEvent();
552
+ if (!existingEventValue) {
553
+ (_a = item.event) === null || _a === void 0 ? void 0 : _a.push(eventValue);
554
+ }
555
+ removeMablGeneratedScripts(eventValue);
556
+ eventValue.mablVariables = filterValidMablVariableAssignments(eventValue.mablVariables);
557
+ eventValue.mablAssertions = filterValidMablAssertions(eventValue.mablAssertions);
558
+ const mablVariables = (_b = eventValue.mablVariables) === null || _b === void 0 ? void 0 : _b.map((variable) => toPostmanScript(variable)).flat();
559
+ const mablAssertions = (_c = eventValue.mablAssertions) === null || _c === void 0 ? void 0 : _c.map((assertion) => toPostmanScript(assertion)).flat();
560
+ if ((mablAssertions === null || mablAssertions === void 0 ? void 0 : mablAssertions.length) || (mablVariables === null || mablVariables === void 0 ? void 0 : mablVariables.length)) {
561
+ (_d = eventValue.script.exec) === null || _d === void 0 ? void 0 : _d.push(`// ${exports.MABL_GENERATED_ASSERTION_TOKEN} Do not edit below this line`);
562
+ }
563
+ if (mablVariables === null || mablVariables === void 0 ? void 0 : mablVariables.length) {
564
+ (_e = eventValue.script.exec) === null || _e === void 0 ? void 0 : _e.push(...mablVariables);
565
+ }
566
+ if (mablAssertions === null || mablAssertions === void 0 ? void 0 : mablAssertions.length) {
567
+ (_f = eventValue.script.exec) === null || _f === void 0 ? void 0 : _f.push(...mablAssertions);
568
+ }
569
+ }
570
+ exports.insertMablAssertionScripts = insertMablAssertionScripts;
571
+ exports.PRE_REQUEST_LISTEN = 'prerequest';
572
+ function splitLines(value) {
573
+ return value.length ? lodash_1.default.split(value, /\r?\n/) : [];
574
+ }
575
+ exports.splitLines = splitLines;
576
+ function createEmptyRequestEventArray() {
577
+ return [createEmptyRequestEvent()];
578
+ }
579
+ exports.createEmptyRequestEventArray = createEmptyRequestEventArray;
580
+ const createEmptyRequest = (id = (0, uuid_1.v4)()) => ({
581
+ id,
582
+ name: '',
583
+ request: {
584
+ auth: {
585
+ type: '',
586
+ },
587
+ body: {
588
+ mode: 'none',
589
+ raw: '',
590
+ file: '',
591
+ graphql: '',
592
+ formdata: [],
593
+ urlencoded: [],
594
+ },
595
+ header: [],
596
+ method: 'GET',
597
+ url: '',
598
+ },
599
+ event: createEmptyRequestEventArray(),
600
+ });
601
+ exports.createEmptyRequest = createEmptyRequest;
602
+ function processPostmanItems(items, handleItem, handleFolder) {
603
+ lodash_1.default.forEach(items, (item) => {
604
+ const maybeItemGroup = item;
605
+ if (isFolder(maybeItemGroup)) {
606
+ if (handleFolder) {
607
+ handleFolder(maybeItemGroup);
608
+ }
609
+ processPostmanItems(maybeItemGroup.item, handleItem);
610
+ }
611
+ else {
612
+ handleItem(item);
613
+ }
614
+ });
615
+ }
616
+ exports.processPostmanItems = processPostmanItems;
617
+ function generateRawScriptField(eventValue) {
618
+ if (eventValue.script) {
619
+ eventValue.script.raw = lodash_1.default.join(eventValue.script.exec, '\n');
620
+ }
621
+ }
622
+ exports.generateRawScriptField = generateRawScriptField;
623
+ function validateCollectionFeatures(collection) {
624
+ let hasFolders = false;
625
+ const handleFolder = (_item) => {
626
+ hasFolders = true;
627
+ };
628
+ processPostmanItems(collection.item, () => { }, handleFolder);
629
+ if (hasFolders) {
630
+ return exports.COLLECTION_WITH_FOLDERS_ERROR;
631
+ }
632
+ return undefined;
633
+ }
634
+ exports.validateCollectionFeatures = validateCollectionFeatures;
635
+ function isFolder(item) {
636
+ return 'item' in item && !!item.item;
637
+ }
638
+ exports.isFolder = isFolder;
639
+ function isJson(contentType) {
640
+ return (!!contentType &&
641
+ (contentType.startsWith('application/json') ||
642
+ contentType.startsWith('application/vnd.api+json')));
643
+ }
644
+ exports.isJson = isJson;
645
+ function isXML(contentType) {
646
+ return (!!contentType &&
647
+ (contentType.startsWith('application/xml') ||
648
+ contentType.startsWith('text/xml')));
649
+ }
650
+ exports.isXML = isXML;
651
+ function isText(contentType) {
652
+ return !!contentType && (contentType === null || contentType === void 0 ? void 0 : contentType.startsWith('text/'));
653
+ }
654
+ exports.isText = isText;
655
+ function getFormDataArray(item) {
656
+ var _a, _b, _c;
657
+ if (getRequestMode(item) === 'formdata') {
658
+ const formdata = (_b = (_a = item === null || item === void 0 ? void 0 : item.request) === null || _a === void 0 ? void 0 : _a.body) === null || _b === void 0 ? void 0 : _b.formdata;
659
+ if (Array.isArray(formdata)) {
660
+ return formdata;
661
+ }
662
+ return (_c = formdata === null || formdata === void 0 ? void 0 : formdata.all()) !== null && _c !== void 0 ? _c : [];
663
+ }
664
+ return [];
665
+ }
666
+ exports.getFormDataArray = getFormDataArray;
667
+ function getRequestMode(item) {
668
+ var _a, _b;
669
+ return (_b = (_a = item === null || item === void 0 ? void 0 : item.request) === null || _a === void 0 ? void 0 : _a.body) === null || _b === void 0 ? void 0 : _b.mode;
670
+ }
671
+ exports.getRequestMode = getRequestMode;
672
+ function formatContent(contentContainer) {
673
+ if (contentContainer === null || contentContainer === void 0 ? void 0 : contentContainer.hasOwnProperty('content')) {
674
+ return contentContainer.content;
675
+ }
676
+ return contentContainer;
677
+ }
678
+ exports.formatContent = formatContent;
679
+ function processMablProtocolProfileBehavior(item) {
680
+ var _a, _b;
681
+ const mablProtocolProfileBehavior = (_b = (_a = item.mablTemporaryStorage) === null || _a === void 0 ? void 0 : _a.mablProtocolProfileBehavior) !== null && _b !== void 0 ? _b : [];
682
+ if (mablProtocolProfileBehavior.length === 0) {
683
+ delete item.protocolProfileBehavior;
684
+ return;
685
+ }
686
+ lodash_1.default.set(item, 'protocolProfileBehavior', {});
687
+ mablProtocolProfileBehavior === null || mablProtocolProfileBehavior === void 0 ? void 0 : mablProtocolProfileBehavior.forEach((setting) => {
688
+ if (setting.key && setting.value) {
689
+ let valueToSet;
690
+ try {
691
+ valueToSet = JSON.parse(setting.value);
692
+ }
693
+ catch (error) {
694
+ valueToSet = setting.value;
695
+ }
696
+ lodash_1.default.set(item, ['protocolProfileBehavior', setting.key], valueToSet);
697
+ }
698
+ });
699
+ }
700
+ function createOptionalVariable(key, type, value, defaultValue) {
701
+ if (value === undefined && defaultValue === undefined) {
702
+ return;
703
+ }
704
+ let parsedValue = value !== null && value !== void 0 ? value : defaultValue;
705
+ if (type === 'boolean') {
706
+ parsedValue = parsedValue === 'true';
707
+ }
708
+ return { key, type, value: parsedValue };
709
+ }
710
+ function mablAuthToPostman(mablAuth, undefinedType) {
711
+ let auth;
712
+ if (mablAuth) {
713
+ const { apikey, basic, bearer, oauth1 } = mablAuth;
714
+ const type = mablAuth.type;
715
+ if (type === undefinedType) {
716
+ return;
717
+ }
718
+ else if (type === exports.NO_AUTH) {
719
+ auth = { type };
720
+ }
721
+ else if (type === exports.API_KEY_AUTH) {
722
+ auth = {
723
+ type,
724
+ apikey: [
725
+ {
726
+ key: 'key',
727
+ value: apikey === null || apikey === void 0 ? void 0 : apikey.key,
728
+ type: 'string',
729
+ },
730
+ {
731
+ key: 'value',
732
+ value: apikey === null || apikey === void 0 ? void 0 : apikey.value,
733
+ type: 'string',
734
+ },
735
+ {
736
+ key: 'in',
737
+ value: apikey === null || apikey === void 0 ? void 0 : apikey.in,
738
+ type: 'string',
739
+ },
740
+ ],
741
+ };
742
+ }
743
+ else if (type === exports.BASIC_AUTH) {
744
+ auth = {
745
+ type,
746
+ basic: [
747
+ {
748
+ key: 'username',
749
+ value: basic === null || basic === void 0 ? void 0 : basic.username,
750
+ type: 'string',
751
+ },
752
+ {
753
+ key: 'password',
754
+ value: basic === null || basic === void 0 ? void 0 : basic.password,
755
+ type: 'string',
756
+ },
757
+ ],
758
+ };
759
+ }
760
+ else if (type === exports.BEARER_TOKEN_AUTH) {
761
+ auth = {
762
+ type,
763
+ bearer: [
764
+ {
765
+ key: 'token',
766
+ value: bearer === null || bearer === void 0 ? void 0 : bearer.token,
767
+ type: 'string',
768
+ },
769
+ ],
770
+ };
771
+ }
772
+ else if (type === exports.OAUTH1_AUTH) {
773
+ auth = {
774
+ type,
775
+ oauth1: [
776
+ createOptionalVariable('addParamsToHeader', 'boolean', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.addParamsToHeader, exports.DEFAULT_ADD_OAUTH1_TO_HEADER_VALUE),
777
+ createOptionalVariable('addEmptyParamsToSign', 'boolean', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.addEmptyParamsToSign),
778
+ createOptionalVariable('callback', 'string', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.callback),
779
+ createOptionalVariable('consumerKey', 'string', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.consumerKey),
780
+ createOptionalVariable('consumerSecret', 'string', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.consumerSecret),
781
+ createOptionalVariable('includeBodyHash', 'boolean', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.includeBodyHash),
782
+ createOptionalVariable('nonce', 'string', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.nonce),
783
+ createOptionalVariable('realm', 'string', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.realm),
784
+ createOptionalVariable('signatureMethod', 'string', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.signatureMethod, exports.HMAC_SHA1_SIGNATURE),
785
+ createOptionalVariable('timestamp', 'string', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.timestamp),
786
+ createOptionalVariable('token', 'string', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.token),
787
+ createOptionalVariable('tokenSecret', 'string', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.tokenSecret),
788
+ createOptionalVariable('verifier', 'string', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.verifier),
789
+ createOptionalVariable('version', 'string', oauth1 === null || oauth1 === void 0 ? void 0 : oauth1.version, exports.DEFAULT_OAUTH1_VERSION),
790
+ ].filter((variable) => variable),
791
+ };
792
+ }
793
+ }
794
+ return auth;
795
+ }
796
+ exports.mablAuthToPostman = mablAuthToPostman;
797
+ function processItemAuth(item) {
798
+ var _a, _b;
799
+ const itemAuth = mablAuthToPostman((_a = item.mablTemporaryStorage) === null || _a === void 0 ? void 0 : _a.mablAuth, exports.INHERIT_AUTH_FROM_PARENT_VALUE);
800
+ if (itemAuth) {
801
+ lodash_1.default.set(item, 'request.auth', itemAuth);
802
+ }
803
+ else if ((_b = item.request) === null || _b === void 0 ? void 0 : _b.auth) {
804
+ item.request.auth = {};
805
+ }
806
+ }
807
+ exports.processItemAuth = processItemAuth;
808
+ function removeInvalidHeaders(item) {
809
+ var _a;
810
+ if ((_a = item.request) === null || _a === void 0 ? void 0 : _a.header) {
811
+ item.request.header = item.request.header.filter((header) => { var _a, _b; return (_b = (_a = header.key) === null || _a === void 0 ? void 0 : _a.trim()) === null || _b === void 0 ? void 0 : _b.length; });
812
+ }
813
+ }
814
+ exports.removeInvalidHeaders = removeInvalidHeaders;
815
+ function replaceRawScriptWithExecArray(eventsContainer) {
816
+ var _a;
817
+ (_a = eventsContainer.event) === null || _a === void 0 ? void 0 : _a.forEach((event) => {
818
+ var _a, _b, _c;
819
+ if (((_a = event.script) === null || _a === void 0 ? void 0 : _a.raw) !== undefined) {
820
+ if (((_c = (_b = event.script) === null || _b === void 0 ? void 0 : _b.raw) === null || _c === void 0 ? void 0 : _c.length) > 0) {
821
+ event.script.exec = splitLines(event.script.raw);
822
+ }
823
+ delete event.script.raw;
824
+ }
825
+ });
826
+ }
827
+ exports.replaceRawScriptWithExecArray = replaceRawScriptWithExecArray;
828
+ function processPostmanItem(item) {
829
+ if (!item.id) {
830
+ item.id = (0, uuid_1.v4)();
831
+ }
832
+ processMablProtocolProfileBehavior(item);
833
+ processItemAuth(item);
834
+ removeInvalidHeaders(item);
835
+ replaceRawScriptWithExecArray(item);
836
+ insertMablAssertionScripts(item);
837
+ }
838
+ exports.processPostmanItem = processPostmanItem;