Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: response handler #385

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pactum",
"version": "3.7.3",
"version": "3.7.4",
"description": "REST API Testing Tool for all levels in a Test Pyramid",
"main": "./src/index.js",
"types": "./src/index.d.ts",
Expand Down
10 changes: 8 additions & 2 deletions src/exports/handler.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IncomingMessage } from 'http';
import * as Spec from '../models/Spec';
import { Interaction } from './mock';
import { IncomingMessage } from 'http';

interface PactumRequest {
url: string;
Expand Down Expand Up @@ -124,4 +124,10 @@ export function addAssertHandler(name: string, func: AssertHandlerFunction): voi
* adds a custom wait handler
* @see https://pactumjs.github.io/api/handlers/addWaitHandler.html
*/
export function addWaitHandler(name: string, func: WaitHandlerFunction): void;
export function addWaitHandler(name: string, func: WaitHandlerFunction): void;

/**
* adds a response handler
* @see https://pactumjs.github.io/api/handlers/addResponseHandler.html
*/
export function addResponseHandler(name: string, func: RetryHandlerFunction): void;
11 changes: 11 additions & 0 deletions src/exports/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const dataHandlers = {};
const interactionHandlers = {};
const assertHandlers = {};
const waitHandlers = {};
const responseHandlers = {};

const handler = {

Expand Down Expand Up @@ -58,6 +59,11 @@ const handler = {
stateHandlers[name] = func;
},

addResponseHandler(name, func) {
isValidHandler(name, func);
responseHandlers[name] = func;
},

getStateHandler(name) {
if (stateHandlers[name]) return stateHandlers[name];
throw new PactumHandlerError(`State Handler Not Found - '${name}'`);
Expand Down Expand Up @@ -102,6 +108,11 @@ const handler = {
getWaitHandler(name) {
if (waitHandlers[name]) return waitHandlers[name];
throw new PactumHandlerError(`Wait Handler Not Found - '${name}'`);
},

getResponseHandler(name) {
if (responseHandlers[name]) return responseHandlers[name];
throw new PactumHandlerError(`Response Handler Not Found - '${name}'`);
}

};
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/handler.runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ const hr = {

retry(name, ctx) {
return handler.getRetryHandler(name)(ctx);
},

response(name, ctx) {
return handler.getResponseHandler(name)(ctx);
}

};
Expand Down
16 changes: 15 additions & 1 deletion src/helpers/toss.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const helper = require('./helper');
const hr = require('./handler.runner');

const stash = require('../exports/stash');
const logger = require('../plugins/logger');

function getCaptureHandlerName(name) {
if (helper.matchesStrategy(name, config.strategy.capture.handler)) {
Expand Down Expand Up @@ -144,10 +145,23 @@ function storeInteractionData(request, interaction) {
stash.addDataStore(interactionData);
}

async function runResponseHandler(spec) {
try {
const ctx = { req: spec._request, res: spec._response };
const handlers = spec._response_handlers;
for (let i = 0; i < handlers.length; i++) {
await hr.response(handlers[i], ctx);
}
} catch (error) {
logger.warn(`Error running response handler: ${error}`);
}
}

module.exports = {
storeSpecData,
recordSpecData,
getOutput,
storeInteractionData,
getPathValueFromRequestResponse
getPathValueFromRequestResponse,
runResponseHandler
};
2 changes: 2 additions & 0 deletions src/models/Spec.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ declare class Spec {
useDataMap(maps: object[]): Spec;
useDataMap(key: string, value: any): Spec;

useResponseHandler(key: string): Spec;

/**
* The GET method requests a representation of the specified resource.
* @see https://pactumjs.github.io/guides/api-testing.html
Expand Down
6 changes: 6 additions & 0 deletions src/models/Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class Spec {
this._wait = null;
this._save = null;
this._data_maps = [];
this._response_handlers = [];
this._specHandlerData = data;
this._sleep = '';
hr.spec(name, data, this);
Expand Down Expand Up @@ -81,6 +82,11 @@ class Spec {
return this;
}

useResponseHandler(key) {
this._response_handlers.push(key);
return this;
}

get(url) {
validateRequestUrl(this._request, url);
this._request.url = url;
Expand Down
2 changes: 2 additions & 0 deletions src/models/Tosser.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class Tosser {

async setResponse() {
this.response = await getResponse(this);
this.spec._response = this.response;
await th.runResponseHandler(this.spec);
const options = this.request.retryOptions;
if (options) {
const count = typeof options.count === 'number' ? options.count : config.request.retry.count;
Expand Down
17 changes: 17 additions & 0 deletions test/component/response-handler.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { expect } = require('chai');
const { spec, handler } = require('../../src/index');

describe('Response Handler', () => {

it('response handler', async () => {
let ran = false;
handler.addResponseHandler('test', async (ctx) => {
ran = true;
});
await spec()
.get('http://localhost:9393/api/users/1')
.useResponseHandler('test');
expect(ran).equals(true);
});

})
Loading