Comparing version 3.7.5 to 3.7.6
{ | ||
"name": "pactum", | ||
"version": "3.7.5", | ||
"version": "3.7.6", | ||
"description": "REST API Testing Tool for all levels in a Test Pyramid", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -6,9 +6,5 @@ import { EventEmitter } from 'node:events'; | ||
AFTER_RESPONSE: 'AFTER_RESPONSE'; | ||
AFTER_RESPONSE_ERROR: 'AFTER_RESPONSE_ERROR'; | ||
}; | ||
export const pactumEvents: EventEmitter; | ||
/** | ||
* @deprecated | ||
*/ | ||
export const events: EventEmitter; |
const { EventEmitter } = require('events'); | ||
/** | ||
* @deprecated | ||
*/ | ||
const events = new EventEmitter(); | ||
const pactumEvents = new EventEmitter(); | ||
@@ -13,8 +8,8 @@ | ||
AFTER_RESPONSE: "AFTER_RESPONSE", | ||
AFTER_RESPONSE_ERROR: "AFTER_RESPONSE_ERROR", | ||
} | ||
module.exports = { | ||
events, | ||
pactumEvents, | ||
EVENT_TYPES | ||
} |
@@ -65,5 +65,10 @@ import { IncomingMessage } from 'http'; | ||
interface ResponseHandlerContext extends RequestResponseContext { | ||
spec: Spec | ||
} | ||
export type SpecHandlerFunction = (ctx: SpecHandlerContext) => void; | ||
export type ExpectHandlerFunction = (ctx: ExpectHandlerContext) => void; | ||
export type RetryHandlerFunction = (ctx: RequestResponseContext) => boolean; | ||
export type ResponseHandlerFunction = (ctx: ResponseHandlerContext) => void; | ||
export type CaptureHandlerFunction = (ctx: CaptureContext) => any; | ||
@@ -134,2 +139,2 @@ export type StateHandlerFunction = (ctx: StateHandlerContext) => any; | ||
*/ | ||
export function addResponseHandler(name: string, func: RetryHandlerFunction): void; | ||
export function addResponseHandler(name: string, func: ResponseHandlerFunction): void; |
@@ -1,5 +0,2 @@ | ||
const fs = require('fs'); | ||
const pt = require('path'); | ||
const log = require('../plugins/logger'); | ||
const { PactumConfigurationError } = require('../helpers/errors'); | ||
const { loadDataManagement } = require('../helpers/file.utils'); | ||
const config = require('../config'); | ||
@@ -15,29 +12,9 @@ const jq = require('json-query'); | ||
loadData(path = './data') { | ||
if (!fs.existsSync(path)) { | ||
log.error(`path not found - ${path}`); | ||
log.warn(`Current Working Dir: ${process.cwd()}`); | ||
throw new PactumConfigurationError('`path` not found'); | ||
const { templates, maps } = loadDataManagement(path); | ||
for (const template of templates) { | ||
this.addDataTemplate(template); | ||
} | ||
const stats = fs.lstatSync(path); | ||
if (!stats.isDirectory()) { | ||
log.error(`path should be a directory - ${path}`); | ||
throw new PactumConfigurationError('`path` should be a directory'); | ||
for (const map of maps) { | ||
this.addDataMap(map); | ||
} | ||
const dir = fs.readdirSync(path); | ||
dir.forEach(file => { | ||
if (file === 'maps') { | ||
const maps = fs.readdirSync(pt.resolve(path, file)); | ||
maps.forEach(map => this.addDataMap(JSON.parse(fs.readFileSync(pt.resolve(path, file, map))))); | ||
} | ||
if (file === 'templates') { | ||
const templates = fs.readdirSync(pt.resolve(path, file)); | ||
templates.forEach(template => this.addDataTemplate(JSON.parse(fs.readFileSync(pt.resolve(path, file, template))))); | ||
} | ||
if (file.endsWith('.template.json')) { | ||
this.addDataTemplate(JSON.parse(fs.readFileSync(pt.resolve(path, file)))); | ||
} | ||
if (file.endsWith('.map.json')) { | ||
this.addDataMap(JSON.parse(fs.readFileSync(pt.resolve(path, file)))); | ||
} | ||
}); | ||
}, | ||
@@ -44,0 +21,0 @@ |
const fs = require('fs'); | ||
const path = require('path'); | ||
const pt = require('path'); | ||
const config = require('../config'); | ||
const log = require('../plugins/logger'); | ||
const { PactumConfigurationError } = require('./errors'); | ||
@@ -31,3 +33,2 @@ function getSnapshotDirAndName(name) { | ||
function findFileRecursively(name, dir = config.data.dir) { | ||
@@ -45,3 +46,3 @@ if (fs.existsSync(name)) { | ||
for (const file of files) { | ||
const dirPath = path.resolve(dir, file); | ||
const dirPath = pt.resolve(dir, file); | ||
const stats = fs.statSync(dirPath); | ||
@@ -58,6 +59,50 @@ if (stats.isDirectory()) { | ||
function loadDataManagement(path = './data') { | ||
const templates = []; | ||
const maps = []; | ||
if (!fs.existsSync(path)) { | ||
log.error(`path not found - ${path}`); | ||
log.warn(`Current Working Dir: ${process.cwd()}`); | ||
throw new PactumConfigurationError(`path not found to load data - '${path}'`); | ||
} | ||
const stats = fs.lstatSync(path); | ||
if (!stats.isDirectory()) { | ||
log.error(`path should be a directory - ${path}`); | ||
throw new PactumConfigurationError(`path should be a directory to load data - '${path}'`); | ||
} | ||
const dir = fs.readdirSync(path); | ||
for (const file of dir) { | ||
if (file.endsWith('.template.json')) { | ||
templates.push(JSON.parse(fs.readFileSync(pt.resolve(path, file)))); | ||
} | ||
if (file.endsWith('.map.json')) { | ||
maps.push(JSON.parse(fs.readFileSync(pt.resolve(path, file)))); | ||
} | ||
if (file === 'maps') { | ||
loadAllFilesRecursively(pt.resolve(path, file), maps); | ||
} | ||
if (file === 'templates') { | ||
loadAllFilesRecursively(pt.resolve(path, file), templates); | ||
} | ||
} | ||
return { templates, maps }; | ||
} | ||
function loadAllFilesRecursively(dir, data = []) { | ||
const files = fs.readdirSync(dir); | ||
for (const file of files) { | ||
if (fs.lstatSync(pt.resolve(dir, file)).isDirectory()) { | ||
loadAllFilesRecursively(pt.resolve(dir, file), data); | ||
} else { | ||
const json = JSON.parse(fs.readFileSync(pt.resolve(dir, file))); | ||
data.push(json); | ||
} | ||
} | ||
} | ||
module.exports = { | ||
getSnapshotFile, | ||
saveSnapshot, | ||
findFileRecursively | ||
findFileRecursively, | ||
loadDataManagement | ||
}; |
@@ -76,3 +76,3 @@ const lc = require('lightcookie'); | ||
} | ||
if(store.options && store.options.append) { | ||
if (store.options && store.options.append) { | ||
ctx.store[store.name] = ctx.store[store.name] || []; | ||
@@ -82,5 +82,10 @@ ctx.store[store.name].push(data_to_store); | ||
} | ||
const specData = {}; | ||
specData[store.name] = data_to_store; | ||
stash.addDataStore(specData); | ||
if (store.options && store.options.merge) { | ||
ctx.store[store.name] = ctx.store[store.name] || {}; | ||
Object.assign(ctx.store[store.name], data_to_store); | ||
} else { | ||
const specData = {}; | ||
specData[store.name] = data_to_store; | ||
stash.addDataStore(specData); | ||
} | ||
} | ||
@@ -152,3 +157,3 @@ } | ||
try { | ||
const ctx = { req: spec._request, res: spec._response }; | ||
const ctx = { req: spec._request, res: spec._response, spec }; | ||
const handlers = spec._response_handlers; | ||
@@ -155,0 +160,0 @@ for (let i = 0; i < handlers.length; i++) { |
@@ -11,2 +11,3 @@ export type ISpecStore = { | ||
append?: boolean | ||
merge?: boolean | ||
} |
@@ -13,3 +13,3 @@ const phin = require('phin'); | ||
const hr = require('../helpers/handler.runner'); | ||
const { events, pactumEvents, EVENT_TYPES } = require('../exports/events'); | ||
const { pactumEvents, EVENT_TYPES } = require('../exports/events'); | ||
@@ -90,25 +90,30 @@ class Tosser { | ||
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); | ||
@@ -226,2 +231,3 @@ this.response = await getResponse(this); | ||
this.printRequestAndResponse(); | ||
pactumEvents.emit(EVENT_TYPES.AFTER_RESPONSE_ERROR, { request: this.request, response: this.response, error }); | ||
throw error; | ||
@@ -304,3 +310,2 @@ } | ||
try { | ||
events.emit(EVENT_TYPES.BEFORE_REQUEST, request); | ||
pactumEvents.emit(EVENT_TYPES.BEFORE_REQUEST, { request }); | ||
@@ -323,3 +328,2 @@ log.debug(`${request.method} ${request.url}`); | ||
res.responseTime = Date.now() - requestStartTime; | ||
events.emit(EVENT_TYPES.AFTER_RESPONSE, res); | ||
pactumEvents.emit(EVENT_TYPES.AFTER_RESPONSE, { request, response: res }); | ||
@@ -326,0 +330,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
184012
5524
8