Comparing version 3.0.19 to 3.0.20
{ | ||
"name": "pactum", | ||
"version": "3.0.19", | ||
"version": "3.0.20", | ||
"description": "REST API Testing Tool for all levels in a Test Pyramid", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -20,3 +20,7 @@ const config = { | ||
headers: {}, | ||
expectHandlers: [] | ||
expectHandlers: [], | ||
wait: { | ||
duration: 1000, | ||
polling: 100 | ||
} | ||
}, | ||
@@ -23,0 +27,0 @@ data: { |
@@ -60,2 +60,7 @@ import * as Spec from '../models/Spec'; | ||
interface WaitHandlerContext extends RequestResponseContext { | ||
data?: any; | ||
rootData?: any; | ||
} | ||
export type SpecHandlerFunction = (ctx: SpecHandlerContext) => void; | ||
@@ -69,2 +74,3 @@ export type ExpectHandlerFunction = (ctx: ExpectHandlerContext) => void; | ||
export type AssertHandlerFunction = (ctx: AssertionContext) => boolean; | ||
export type WaitHandlerFunction = (ctx: WaitHandlerContext) => any | Promise<any>; | ||
@@ -116,2 +122,8 @@ /** | ||
*/ | ||
export function addAssertHandler(name: string, func: AssertHandlerFunction): void; | ||
export function addAssertHandler(name: string, func: AssertHandlerFunction): void; | ||
/** | ||
* adds a custom wait handler | ||
* @see https://pactumjs.github.io/#/api-handlers?id=addwaithandler | ||
*/ | ||
export function addWaitHandler(name: string, func: WaitHandlerFunction): void; |
@@ -12,2 +12,3 @@ const { PactumHandlerError } = require('../helpers/errors'); | ||
const assertHandlers = {}; | ||
const waitHandlers = {}; | ||
@@ -95,2 +96,12 @@ const handler = { | ||
throw new PactumHandlerError(`Assert Handler Not Found - '${name}'`); | ||
}, | ||
addWaitHandler(name, func) { | ||
isValidHandler(name, func); | ||
waitHandlers[name] = func; | ||
}, | ||
getWaitHandler(name) { | ||
if (waitHandlers[name]) return waitHandlers[name]; | ||
throw new PactumHandlerError(`Wait Handler Not Found - '${name}'`); | ||
} | ||
@@ -97,0 +108,0 @@ |
@@ -51,3 +51,4 @@ export type RequestMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'TRACE'; | ||
flow?: string; | ||
strict: boolean; | ||
strict?: boolean; | ||
background?: boolean; | ||
request: InteractionRequest; | ||
@@ -54,0 +55,0 @@ response: InteractionResponse; |
@@ -35,2 +35,10 @@ const handler = require('../exports/handler'); | ||
return handler.getStateHandler(name)({ data }); | ||
}, | ||
wait(name, ctx) { | ||
return handler.getWaitHandler(name)(ctx); | ||
}, | ||
retry(name, ctx) { | ||
return handler.getRetryHandler(name)(ctx); | ||
} | ||
@@ -37,0 +45,0 @@ |
@@ -202,2 +202,3 @@ const { setMatchingRules, getValue } = require('pactum-matchers').utils; | ||
strict, | ||
background, | ||
request, | ||
@@ -211,2 +212,3 @@ response, | ||
if (provider) this.provider = provider; | ||
if (background) this.background = background; | ||
this.strict = strict; | ||
@@ -213,0 +215,0 @@ this.request = new InteractionRequest(request); |
@@ -200,4 +200,4 @@ const polka = require('polka'); | ||
}; | ||
if (req.query && typeof req.query === 'object' && Object.keys(req.query) > 0) { | ||
msg.queryParams = req.query; | ||
if (req._parsedUrl && req._parsedUrl.query) { | ||
msg.queryParams = req.query || req._parsedUrl.query; | ||
} | ||
@@ -204,0 +204,0 @@ if (req.body) { |
@@ -14,2 +14,3 @@ import { RequestOptions } from 'http'; | ||
strategy?: string | RetryHandlerFunction; | ||
status?: number | number[]; | ||
} | ||
@@ -378,2 +379,5 @@ | ||
wait(spec: Spec): Spec; | ||
wait(): Spec; | ||
wait(duration: number, pollingInterval: number): Spec; | ||
wait(handlerName: string, data?: any): Spec; | ||
@@ -380,0 +384,0 @@ /** |
@@ -36,2 +36,3 @@ const FormData = require('form-data'); | ||
this._wait = null; | ||
this._specHandlerData = data; | ||
hr.spec(name, data, this); | ||
@@ -53,2 +54,3 @@ this._expect.setDefaultResponseExpectations(); | ||
use(name, data) { | ||
this._specHandlerData = data; | ||
hr.spec(name, data, this); | ||
@@ -456,4 +458,4 @@ return this; | ||
wait(ms) { | ||
this._wait = ms; | ||
wait(arg1, arg2) { | ||
this._wait = { arg1, arg2 }; | ||
return this; | ||
@@ -460,0 +462,0 @@ } |
@@ -9,5 +9,5 @@ const phin = require('phin'); | ||
const mock = require('../exports/mock'); | ||
const handler = require('../exports/handler'); | ||
const request = require('../exports/request'); | ||
const config = require('../config'); | ||
const hr = require('../helpers/handler.runner'); | ||
@@ -33,5 +33,9 @@ class Tosser { | ||
await this.addInteractionsToServer(); | ||
// get interactions to check for background property | ||
await this.getInteractionsFromServer(); | ||
await this.setResponse(); | ||
this.inspect(); | ||
await this.wait(); | ||
if (!hasBackgroundInteractions(this.interactions)) { | ||
await this.staticWait(); | ||
} | ||
await this.getInteractionsFromServer(); | ||
@@ -41,2 +45,6 @@ this.recordData(); | ||
await this.validate(); | ||
if (hasBackgroundInteractions(this.interactions) || (this.spec._wait && typeof this.spec._wait.arg1 === 'string')) { | ||
await this.dynamicWait(); | ||
this.validateBackgroundInteractions(); | ||
} | ||
return th.getOutput(this.spec, this.spec._returns); | ||
@@ -74,2 +82,3 @@ } finally { | ||
const strategy = options.strategy; | ||
const status = options.status; | ||
for (let i = 0; i < count; i++) { | ||
@@ -81,5 +90,11 @@ let noRetry = true; | ||
} else if (typeof strategy === 'string') { | ||
const handlerFun = handler.getRetryHandler(strategy); | ||
noRetry = handlerFun(ctx); | ||
} else { | ||
noRetry = hr.retry(strategy, ctx); | ||
} else if (status) { | ||
if (Array.isArray(status)) { | ||
noRetry = !(status.includes(this.response.statusCode)); | ||
} else { | ||
noRetry = !(status === ctx.res.statusCode); | ||
} | ||
} | ||
else { | ||
try { | ||
@@ -112,11 +127,41 @@ await this.validateResponse(); | ||
async wait() { | ||
async staticWait() { | ||
const _wait = this.spec._wait; | ||
if (typeof _wait === 'number') { | ||
await helper.sleep(_wait); | ||
} else if (_wait && typeof _wait === 'object') { | ||
await _wait; | ||
if (_wait && _wait.arg1) { | ||
if (typeof _wait.arg1 === 'number') { | ||
await helper.sleep(_wait.arg1); | ||
} else { | ||
await _wait.arg1; | ||
} | ||
} | ||
} | ||
async dynamicWait() { | ||
const _wait = this.spec._wait; | ||
if (_wait) { | ||
if (typeof _wait.arg1 === 'undefined' || typeof _wait.arg1 === 'number') { | ||
let duration = config.response.wait.duration; | ||
let polling = config.response.wait.polling; | ||
let waited = 0; | ||
if (typeof _wait.arg1 === 'number') { | ||
duration = _wait.arg1; | ||
polling = _wait.arg2 && _wait.arg2 > 0 ? _wait.arg2 : 100; | ||
} | ||
while (waited < duration) { | ||
waited = waited + polling; | ||
await this.getInteractionsFromServer(); | ||
try { | ||
this.validateBackgroundInteractions(); | ||
break; | ||
} catch (error) { | ||
if (waited > duration) throw error; | ||
} | ||
await helper.sleep(polling); | ||
} | ||
} else { | ||
await hr.wait(_wait.arg1, { req: this.request, res: this.response, data: _wait.arg2, rootData: this.spec._specHandlerData }); | ||
} | ||
} | ||
} | ||
setPreviousLogLevel() { | ||
@@ -151,3 +196,3 @@ if (this.previousLogLevel) { | ||
try { | ||
this.validateInteractions(); | ||
this.validateNonBackgroundInteractions(); | ||
await this.validateResponse(); | ||
@@ -175,6 +220,12 @@ this.spec.status = 'PASSED'; | ||
validateInteractions() { | ||
this.expect.validateInteractions(this.interactions); | ||
validateNonBackgroundInteractions() { | ||
const nonBgInteractions = this.interactions.filter(interaction => !interaction.background); | ||
this.expect.validateInteractions(nonBgInteractions); | ||
} | ||
validateBackgroundInteractions() { | ||
const bgInteractions = this.interactions.filter(interaction => interaction.background); | ||
this.expect.validateInteractions(bgInteractions); | ||
} | ||
async validateResponse() { | ||
@@ -216,2 +267,6 @@ await this.expect.validate(this.request, this.response); | ||
function hasBackgroundInteractions(interactions) { | ||
return interactions.some(interaction => interaction.background); | ||
} | ||
module.exports = Tosser; |
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
155527
4660