@apistudio/apim-cli
Version:
CLI for API Management Products
105 lines (104 loc) • 4.42 kB
JavaScript
/**
* Copyright Super iPaaS Integration LLC, an IBM Company 2024
*/
import newman from 'newman';
import { LogWrapper } from '../service/log-wrapper.js';
export class NewmanRunner {
constructor(collection) {
this.collection = collection;
}
run() {
return new Promise((resolve, reject) => {
LogWrapper.logInfo('0215', `${this.collection.info.name}`);
newman.run({ collection: this.collection }, (err, summary) => {
if (err) {
LogWrapper.logError('0013', 'running tests', `${err.message}`);
reject(new Error('Error running tests'));
return;
}
try {
LogWrapper.logDebug('0003', 'Newman run completed. Processing results.');
const results = this.getExecutionResults(summary.run.executions);
const filteredSummary = this.createFilteredSummary(summary, results);
LogWrapper.logInfo('0216', `${this.collection.info.name}`);
resolve(filteredSummary);
return;
}
catch (error) {
LogWrapper.logError('0013', 'processing test results', error.message);
reject(new Error('Error processing test results'));
return;
}
});
});
}
getExecutionResults(executions) {
const getResponse = (execution) => {
if (execution.response?.stream) {
return execution.response?.stream.toString();
}
else if (execution.response?.body) {
return execution.response?.body;
}
else {
return 'Response unavailable';
}
};
const formatExecution = (execution) => {
try {
return {
id: execution.id,
name: execution.item.name,
url: execution.request.url.toString(),
method: execution.request.method,
header: execution.request.headers,
time: execution.response?.responseTime || 0,
responseCode: {
code: execution.response?.code || 408,
name: execution.response?.status || 'Request Timed out',
time: execution.response?.responseTime || 0,
size: execution.response?.responseSize || 0
},
response: getResponse(execution),
responseHeaders: execution.response?.headers || null,
allTests: execution.assertions.map(assertion => ({
[assertion.assertion]: assertion.error ? {
status: false,
error: assertion.error
} : { status: true }
}))
};
}
catch (error) {
LogWrapper.logError('0013', `formatting execution with id ${execution.id}`, error.message);
return null;
}
};
try {
LogWrapper.logDebug('0003', 'Formatting execution results.');
return executions.map(formatExecution).filter(result => result !== null);
}
catch (e) {
LogWrapper.logError('0013', 'processing executions', e.message);
return [];
}
}
// @ts-ignore
createFilteredSummary(summary, results) {
LogWrapper.logDebug('0003', 'Creating filtered summary from execution results.');
return {
id: summary.collection.id,
name: summary.collection.name,
timestamp: summary.run.timings.completed,
// @ts-ignore
totalPass: summary.run.stats.assertions.total - summary.run.stats.assertions.failed,
status: summary.run.failures.length === 0 ? 'finished' : 'failed',
startedAt: summary.run.timings.started,
totalFail: summary.run.stats.assertions.failed,
totalTime: (summary.run.timings.completed && summary.run.timings.started)
? summary.run.timings.completed - summary.run.timings.started
: undefined,
results: results
};
}
}