Comparing version 3.0.11 to 3.0.12
{ | ||
"name": "pactum", | ||
"version": "3.0.11", | ||
"version": "3.0.12", | ||
"description": "REST API Testing Tool for all levels in a Test Pyramid", | ||
@@ -5,0 +5,0 @@ "main": "./src/index.js", |
@@ -18,2 +18,3 @@ export interface Have { | ||
responseTimeLessThan(ms: number): void; | ||
error(err?: string | object): void; | ||
_(handler: string, data: any): Promise<void>; | ||
@@ -20,0 +21,0 @@ } |
@@ -92,2 +92,7 @@ const ExpectModel = require('../models/expect'); | ||
error(err) { | ||
this.expect.errors.push(err); | ||
this._validate(); | ||
} | ||
_(handler, data) { | ||
@@ -94,0 +99,0 @@ this.expect.customExpectHandlers.push({ handler, data }); |
@@ -48,5 +48,12 @@ const Interaction = require('../models/Interaction.model'); | ||
} | ||
const interaction = new Interaction(raw, true); | ||
this._server.addInteraction(interaction.id, interaction); | ||
ids.push(interaction.id); | ||
if (!Array.isArray(raw)) { | ||
raw = [raw]; | ||
} else { | ||
alone = false; | ||
} | ||
for (let j = 0; j < raw.length; j++) { | ||
const interaction = new Interaction(raw[j], true); | ||
this._server.addInteraction(interaction.id, interaction); | ||
ids.push(interaction.id); | ||
} | ||
} | ||
@@ -53,0 +60,0 @@ return alone ? ids[0] : ids; |
@@ -8,4 +8,16 @@ const handler = require('../exports/handler'); | ||
const mi = handler.getInteractionHandler(name)({ data }); | ||
if (mi && mi.name) return this.interaction(mi.name, mi.data); | ||
return mi; | ||
if (Array.isArray(mi)) { | ||
const interactions = []; | ||
for (let i = 0; i < mi.length; i++) { | ||
if (mi[i] && mi[i].name) { | ||
interactions.push(this.interaction(mi[i].name, mi[i].data)); | ||
} else { | ||
interactions.push(mi[i]); | ||
} | ||
} | ||
return interactions; | ||
} else { | ||
if (mi && mi.name) return this.interaction(mi.name, mi.data); | ||
return mi; | ||
} | ||
}, | ||
@@ -12,0 +24,0 @@ |
@@ -37,2 +37,3 @@ const assert = require('assert'); | ||
this.customExpectHandlers = []; | ||
this.errors = []; | ||
} | ||
@@ -58,2 +59,3 @@ | ||
this._validateResponseTime(response); | ||
this._validateErrors(response); | ||
// for asynchronous expectations | ||
@@ -352,2 +354,27 @@ return this._validateCustomExpectHandlers(request, response); | ||
_validateErrors(response) { | ||
if (this.errors.length > 0) { | ||
if (!(response instanceof Error)) { | ||
this.fail(`No Error while performing a request`); | ||
} | ||
for (let i = 0; i < this.errors.length; i++) { | ||
const expected = this.errors[i]; | ||
if (typeof expected === 'string') { | ||
const actual = response.toString(); | ||
if (!actual.includes(expected)) { | ||
this.fail(`Error - "${actual}" doesn't include - ${expected}`); | ||
} | ||
} | ||
if (typeof expected === 'object') { | ||
const rules = jmv.getMatchingRules(expected, '$.error'); | ||
const value = jmv.getRawValue(expected); | ||
const errors = jmv.validate(response, value, rules, '$.error', false); | ||
if (errors) { | ||
this.fail(errors.replace('$.error', '$')); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
async _validateCustomExpectHandlers(request, response) { | ||
@@ -354,0 +381,0 @@ for (let i = 0; i < this.customExpectHandlers.length; i++) { |
@@ -534,2 +534,6 @@ import { RequestOptions } from 'http'; | ||
expectError(): Spec; | ||
expectError(error: string): Spec; | ||
expectError(error: object): Spec; | ||
/** | ||
@@ -536,0 +540,0 @@ * expects the json to match with stored snapshots |
@@ -396,2 +396,7 @@ const FormData = require('form-data'); | ||
expectError(error) { | ||
this._expect.errors.push(error); | ||
return this; | ||
} | ||
updateSnapshot() { | ||
@@ -398,0 +403,0 @@ this._expect.updateSnapshot = true; |
@@ -48,6 +48,11 @@ const phin = require('phin'); | ||
async addInteractionsToServer() { | ||
const mockIdPromises = []; | ||
let mockIdPromises = []; | ||
for (let i = 0; i < this.interactions.length; i++) { | ||
const raw = this.interactions[i]; | ||
mockIdPromises.push(mock.addInteraction(raw.interaction, raw.data)); | ||
const { interaction, data } = this.interactions[i]; | ||
const ids = mock.addInteraction(interaction, data); | ||
if (Array.isArray(ids)) { | ||
mockIdPromises = mockIdPromises.concat(ids); | ||
} else { | ||
mockIdPromises.push(ids); | ||
} | ||
} | ||
@@ -58,3 +63,3 @@ this.mockIds = this.mockIds.concat(await Promise.all(mockIdPromises)); | ||
async setResponse() { | ||
this.response = await getResponse(this.request); | ||
this.response = await getResponse(this); | ||
const retryOptions = this.request.retryOptions; | ||
@@ -75,3 +80,3 @@ if (retryOptions) { | ||
await helper.sleep(delay); | ||
this.response = await getResponse(this.request); | ||
this.response = await getResponse(this); | ||
} else { | ||
@@ -145,3 +150,3 @@ break; | ||
validateError() { | ||
if (this.response instanceof Error) { | ||
if (this.response instanceof Error && this.expect.errors.length === 0) { | ||
this.spec.status = 'ERROR'; | ||
@@ -164,3 +169,3 @@ this.spec.failure = this.response.toString(); | ||
runReport() { | ||
if (config.reporter.autoRun) { | ||
if (config.reporter.autoRun && this.expect.errors.length === 0) { | ||
rlc.afterSpecReport(this.spec); | ||
@@ -172,11 +177,14 @@ } | ||
async function getResponse(req) { | ||
async function getResponse(tosser) { | ||
const { request, expect } = tosser; | ||
let res = {}; | ||
const requestStartTime = Date.now(); | ||
try { | ||
log.debug(`${req.method} ${req.url}`); | ||
res = await phin(req); | ||
log.debug(`${request.method} ${request.url}`); | ||
res = await phin(request); | ||
res.json = helper.getJson(res.body); | ||
} catch (error) { | ||
log.error('Error performing request', error); | ||
if (expect.errors.length === 0) { | ||
log.error('Error performing request', error); | ||
} | ||
res = error; | ||
@@ -183,0 +191,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
150377
4578