Skip to content

Commit

Permalink
chore: after response error (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
ASaiAnudeep authored Jan 1, 2025
1 parent 6a92340 commit 9cd4e83
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
6 changes: 1 addition & 5 deletions src/exports/events.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import { EventEmitter } from 'node:events';
export const EVENT_TYPES: {
BEFORE_REQUEST: 'BEFORE_REQUEST';
AFTER_RESPONSE: 'AFTER_RESPONSE';
AFTER_RESPONSE_ERROR: 'AFTER_RESPONSE_ERROR';
};

export const pactumEvents: EventEmitter;

/**
* @deprecated
*/
export const events: EventEmitter;
7 changes: 1 addition & 6 deletions src/exports/events.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
const { EventEmitter } = require('events');

/**
* @deprecated
*/
const events = new EventEmitter();

const pactumEvents = new EventEmitter();

const EVENT_TYPES = {
BEFORE_REQUEST: "BEFORE_REQUEST",
AFTER_RESPONSE: "AFTER_RESPONSE",
AFTER_RESPONSE_ERROR: "AFTER_RESPONSE_ERROR",
}

module.exports = {
events,
pactumEvents,
EVENT_TYPES
}
30 changes: 17 additions & 13 deletions src/models/Tosser.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const mock = require('../exports/mock');
const request = require('../exports/request');
const config = require('../config');
const hr = require('../helpers/handler.runner');
const { events, pactumEvents, EVENT_TYPES } = require('../exports/events');
const { pactumEvents, EVENT_TYPES } = require('../exports/events');

class Tosser {

Expand Down Expand Up @@ -86,29 +86,34 @@ class Tosser {
const strategy = options.strategy;
const status = options.status;
for (let i = 0; i < count; i++) {
let noRetry = true;
let err = null;
let shouldRetry = false;
const ctx = { req: this.request, res: this.response };
if (typeof strategy === 'function') {
noRetry = strategy(ctx);
shouldRetry = !strategy(ctx);
} else if (typeof strategy === 'string') {
noRetry = hr.retry(strategy, ctx);
shouldRetry = !hr.retry(strategy, ctx);
} else if (status) {
if (Array.isArray(status)) {
noRetry = !(status.includes(this.response.statusCode));
shouldRetry = status.includes(this.response.statusCode);
} else {
noRetry = !(status === ctx.res.statusCode);
shouldRetry = status === ctx.res.statusCode;
}
}
else {
} else {
try {
await this.validateResponse();
} catch (error) {
noRetry = false;
err = error;
shouldRetry = true;
}
}
if (!noRetry) {
if (shouldRetry) {
const scale = delay === 1000 ? 'second' : 'seconds';
log.info(`Request retry initiated, waiting ${delay / 1000} ${scale} for attempt ${i + 1} of ${count}`);
if (err) {
log.info(`Request retry initiated, waiting ${delay / 1000} ${scale} for attempt ${i + 1} of ${count} due to error: ${err.message}`);
} else {
log.info(`Request retry initiated, waiting ${delay / 1000} ${scale} for attempt ${i + 1} of ${count}`);
}
await helper.sleep(delay);
this.response = await getResponse(this);
this.spec._response = this.response;
Expand Down Expand Up @@ -221,6 +226,7 @@ class Tosser {
this.spec.failure = error.toString();
this.runReport();
this.printRequestAndResponse();
pactumEvents.emit(EVENT_TYPES.AFTER_RESPONSE_ERROR, { request: this.request, response: this.response, error });
throw error;
}
}
Expand Down Expand Up @@ -298,7 +304,6 @@ async function getResponse(tosser) {
let res = {};
const requestStartTime = Date.now();
try {
events.emit(EVENT_TYPES.BEFORE_REQUEST, request);
pactumEvents.emit(EVENT_TYPES.BEFORE_REQUEST, { request });
log.debug(`${request.method} ${request.url}`);
res = await phin(request);
Expand All @@ -316,7 +321,6 @@ async function getResponse(tosser) {
res = error;
} finally {
res.responseTime = Date.now() - requestStartTime;
events.emit(EVENT_TYPES.AFTER_RESPONSE, res);
pactumEvents.emit(EVENT_TYPES.AFTER_RESPONSE, { request, response: res });
}
return res;
Expand Down
22 changes: 21 additions & 1 deletion test/component/events.spec.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
const { spec } = require('../../src');
const { pactumEvents, EVENT_TYPES } = require('../../src').events;

describe('events', () => {
describe.only('events', () => {

before(() => {
pactumEvents.on(EVENT_TYPES.BEFORE_REQUEST, (r) => {
console.log('BEFORE_REQUEST');
console.log(r);
});
pactumEvents.on(EVENT_TYPES.AFTER_RESPONSE, (r) => {
console.log('AFTER_RESPONSE');
console.log(r.response.body);
});
pactumEvents.on(EVENT_TYPES.AFTER_RESPONSE_ERROR, (r) => {
console.log('AFTER_RESPONSE_ERROR');
console.log(r.request);
console.log(r.response.body);
console.log(r.error);
});
});

after(() => {
Expand All @@ -22,4 +30,16 @@ describe('events', () => {
.get('http://localhost:9393/default/get')
.expectStatus(200);
});

it('get error', async () => {
try {
await spec()
.useInteraction('default get')
.get('http://localhost:9393/default/get')
.expectStatus(400);
} catch (error) {

}

});
});

0 comments on commit 9cd4e83

Please sign in to comment.