feat(#177 #165): running tests and assertions for error responses

This commit is contained in:
Anoop M D 2023-08-30 21:35:59 +05:30
parent 97678b05fc
commit fd4c188c95
2 changed files with 85 additions and 28 deletions

View File

@ -72,16 +72,17 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
ipcMain.handle('send-http-request', async (event, item, collectionUid, collectionPath, environment, collectionVariables) => {
const cancelTokenUid = uuid();
mainWindow.webContents.send('main:http-request-queued', {
collectionUid,
itemUid: item.uid,
cancelTokenUid
});
const _request = item.draft ? item.draft.request : item.request;
const request = prepareRequest(_request);
const envVars = getEnvVars(environment);
try {
mainWindow.webContents.send('main:http-request-queued', {
collectionUid,
itemUid: item.uid,
cancelTokenUid
});
const _request = item.draft ? item.draft.request : item.request;
const request = prepareRequest(_request);
// make axios work in node using form data
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
if(request.headers && request.headers['content-type'] === 'multipart/form-data') {
@ -97,8 +98,6 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
request.cancelToken = cancelToken.token;
saveCancelToken(cancelTokenUid, cancelToken);
const envVars = getEnvVars(environment);
// run pre-request vars
const preRequestVars = get(request, 'vars.req', []);
if(preRequestVars && preRequestVars.length) {
@ -226,7 +225,29 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
return Promise.reject(error);
}
if(error.response) {
if(error && error.response) {
// run assertions
const assertions = get(request, 'assertions');
const assertRuntime = new AssertRuntime();
const results = assertRuntime.runAssertions(assertions, request, error.response, envVars, collectionVariables, collectionPath);
mainWindow.webContents.send('main:assertion-results', {
results: results,
itemUid: item.uid,
collectionUid
});
// run tests
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
const testRuntime = new TestRuntime();
const testResults = testRuntime.runTests(testFile, request, error.response, envVars, collectionVariables, collectionPath);
mainWindow.webContents.send('main:test-results', {
results: testResults.results,
itemUid: item.uid,
collectionUid
});
return {
status: error.response.status,
statusText: error.response.statusText,
@ -334,15 +355,15 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
let timeStart;
let timeEnd;
mainWindow.webContents.send('main:run-folder-event', {
type: 'request-queued',
...eventData
});
const _request = item.draft ? item.draft.request : item.request;
const request = prepareRequest(_request);
try {
mainWindow.webContents.send('main:run-folder-event', {
type: 'request-queued',
...eventData
});
const _request = item.draft ? item.draft.request : item.request;
const request = prepareRequest(_request);
// make axios work in node using form data
// reference: https://github.com/axios/axios/issues/1006#issuecomment-320165427
if(request.headers && request.headers['content-type'] === 'multipart/form-data') {
@ -485,7 +506,43 @@ const registerNetworkIpc = (mainWindow, watcher, lastOpenedCollections) => {
size: error.response.headers['content-length'] || getSize(error.response.data),
data: error.response.data,
}
// run assertions
const assertions = get(item, 'request.assertions');
if(assertions && assertions.length) {
const assertRuntime = new AssertRuntime();
const results = assertRuntime.runAssertions(assertions, request, error.response, envVars, collectionVariables, collectionPath);
mainWindow.webContents.send('main:run-folder-event', {
type: 'assertion-results',
assertionResults: results,
itemUid: item.uid,
collectionUid
});
}
// run tests
const testFile = item.draft ? get(item.draft, 'request.tests') : get(item, 'request.tests');
const testRuntime = new TestRuntime();
const testResults = testRuntime.runTests(testFile, request, error.response, envVars, collectionVariables, collectionPath);
mainWindow.webContents.send('main:run-folder-event', {
type: 'test-results',
testResults: testResults.results,
...eventData
});
// if we get a response from the server, we consider it as a success
mainWindow.webContents.send('main:run-folder-event', {
type: 'response-received',
error: error ? error.message : 'An error occurred while running the request',
responseReceived: responseReceived,
...eventData
});
return;
}
mainWindow.webContents.send('main:run-folder-event', {
type: 'error',
error: error ? error.message : 'An error occurred while running the request',

View File

@ -1,26 +1,26 @@
class BrunoResponse {
constructor(res) {
this.res = res;
this.status = res.status;
this.statusText = res.statusText;
this.headers = res.headers;
this.body = res.data;
this.status = res ? res.status : null;
this.statusText = res ? res.statusText : null;
this.headers = res ? res.headers : null;
this.body = res ? res.data : null;
}
getStatus() {
return this.res.status;
return this.res ? this.res.status : null;
}
getHeader(name) {
return this.res.header[name];
return (this.res && this.res.headers) ? this.res.headers[name] : null;
}
getHeaders() {
return this.res.headers;
return this.res ? this.res.headers : null;
}
getBody() {
return this.res.data;
return this.res ? this.res.data : null;
}
}