artillery-plugin-expect
Advanced tools
Comparing version 2.3.0 to 2.3.1-884a216
61
index.js
@@ -20,3 +20,3 @@ /* This Source Code Form is subject to the terms of the Mozilla Public | ||
function ExpectationsPlugin(script, events) { | ||
if(!global.artillery || !global.artillery.log) { | ||
if (!global.artillery || !global.artillery.log) { | ||
console.error('artillery-plugin-expect requires Artillery v2'); | ||
@@ -38,3 +38,3 @@ return; | ||
script.scenarios.forEach(function(scenario) { | ||
script.scenarios.forEach(function (scenario) { | ||
scenario.onError = [].concat(scenario.onError || []); | ||
@@ -52,6 +52,7 @@ scenario.onError.push('expectationsPluginOnError'); | ||
script.config.processor.expectationsPluginCheckExpectations = expectationsPluginCheckExpectations; | ||
script.config.processor.expectationsPluginCheckExpectations = | ||
expectationsPluginCheckExpectations; | ||
script.config.processor.expectationsPluginOnError = expectationsPluginOnError; | ||
script.config.processor.expectationsPluginSetExpectOptions = function( | ||
script.config.processor.expectationsPluginSetExpectOptions = function ( | ||
userContext, | ||
@@ -62,7 +63,11 @@ events, | ||
userContext.expectationsPlugin = {}; | ||
userContext.expectationsPlugin.formatter = script.config.plugins.expect.formatter || | ||
userContext.expectationsPlugin.formatter = | ||
script.config.plugins.expect.formatter || | ||
script.config.plugins.expect.outputFormat || | ||
'pretty'; | ||
userContext.expectationsPlugin.expectDefault200 = (script.config.plugins.expect.expectDefault200 === true || script.config.plugins.expect.expectDefault200 === 'true'); | ||
userContext.expectationsPlugin.reportFailuresAsErrors = script.config.plugins.expect.reportFailuresAsErrors; | ||
userContext.expectationsPlugin.expectDefault200 = | ||
script.config.plugins.expect.expectDefault200 === true || | ||
script.config.plugins.expect.expectDefault200 === 'true'; | ||
userContext.expectationsPlugin.reportFailuresAsErrors = | ||
script.config.plugins.expect.reportFailuresAsErrors; | ||
@@ -75,3 +80,9 @@ return done(); | ||
function expectationsPluginOnError(scenarioErr, requestParams, userContext, events, done) { | ||
function expectationsPluginOnError( | ||
scenarioErr, | ||
requestParams, | ||
userContext, | ||
events, | ||
done | ||
) { | ||
if (userContext.expectationsPlugin.outputFormat === 'json') { | ||
@@ -94,5 +105,9 @@ artillery.log(JSON.stringify({ ok: false, error: scenarioErr.message })); | ||
const expectations = _.isArray(req.expect) ? | ||
req.expect : | ||
_.map(req.expect, (v, k) => { const o = {}; o[k] = v; return o; }); | ||
const expectations = _.isArray(req.expect) | ||
? req.expect | ||
: _.map(req.expect, (v, k) => { | ||
const o = {}; | ||
o[k] = v; | ||
return o; | ||
}); | ||
@@ -108,3 +123,3 @@ if (expectations.length === 0) { | ||
let body = maybeParseBody(res); | ||
_.each(expectations, ex => { | ||
_.each(expectations, (ex) => { | ||
const checker = Object.keys(ex)[0]; | ||
@@ -133,6 +148,6 @@ debug(`checker: ${checker}`); | ||
if (e.ok) { | ||
events.emit('counter', `plugins.expect.ok`, 1); | ||
events.emit('counter', 'plugins.expect.ok', 1); | ||
events.emit('counter', `plugins.expect.ok.${e.type}`, 1); | ||
} else { | ||
events.emit('counter', `plugins.expect.failed`, 1); | ||
events.emit('counter', 'plugins.expect.failed', 1); | ||
events.emit('counter', `plugins.expect.failed.${e.type}`, 1); | ||
@@ -142,3 +157,9 @@ } | ||
events.emit('plugin:expect:expectations', requestExpectations, req, res, userContext); | ||
events.emit( | ||
'plugin:expect:expectations', | ||
requestExpectations, | ||
req, | ||
res, | ||
userContext | ||
); | ||
@@ -155,3 +176,3 @@ const formatterName = userContext.expectationsPlugin.formatter; | ||
const failedExpectations = results.filter(res => !res.ok).length > 0; | ||
const failedExpectations = results.filter((res) => !res.ok).length > 0; | ||
@@ -177,7 +198,5 @@ if (failedExpectations) { | ||
res.headers['content-type'] && | ||
( | ||
res.headers['content-type'].indexOf('application/json') !== -1 | ||
|| res.headers['content-type'].indexOf('application/problem+json') !== -1 | ||
|| res.headers['content-type'].indexOf('application/ld+json') !== -1 | ||
) | ||
(res.headers['content-type'].indexOf('application/json') !== -1 || | ||
res.headers['content-type'].indexOf('application/problem+json') !== -1 || | ||
res.headers['content-type'].indexOf('application/ld+json') !== -1) | ||
) { | ||
@@ -184,0 +203,0 @@ try { |
@@ -8,3 +8,5 @@ /* This Source Code Form is subject to the terms of the Mozilla Public | ||
const debug = require('debug')('plugin:expect'); | ||
const template = global.artillery ? global.artillery.util.template : require('artillery/util').template; | ||
const template = global.artillery | ||
? global.artillery.util.template | ||
: require('artillery/util').template; | ||
const _ = require('lodash'); | ||
@@ -25,3 +27,3 @@ const jmespath = require('jmespath'); | ||
jmespath: expectJmesPath, | ||
jpath: expectJmesPath, | ||
jpath: expectJmesPath | ||
}; | ||
@@ -32,9 +34,20 @@ | ||
debug('expectation', expectation); | ||
const result = jmespath.search(JSON.parse(body), expectation.jmespath || expectation.jpath); | ||
return { | ||
ok: result, | ||
const result = { | ||
expected: expectation.description || expectation.jmespath, | ||
got: expectation.jmespath, | ||
type: expectation.jmespath ? 'jmespath' : 'jpath' | ||
}; | ||
if (body === null || typeof body !== 'object') { | ||
result.ok = false; | ||
result.got = `response body is not an object`; | ||
} else { | ||
result.ok = jmespath.search( | ||
body, | ||
expectation.jmespath || expectation.jpath | ||
); | ||
result.got = expectation.jmespath; | ||
} | ||
return result; | ||
} | ||
@@ -52,3 +65,3 @@ | ||
if(expectation.cdnHit) { | ||
if (expectation.cdnHit) { | ||
result.expected = 'a cache header indicating a cache hit'; | ||
@@ -61,4 +74,4 @@ } else { | ||
'cf-cache-status', // CloudFlare | ||
'x-cache', // CloudFront, Fastly | ||
'x-vercel-cache' // Vercel | ||
'x-cache', // CloudFront, Fastly | ||
'x-vercel-cache' // Vercel | ||
]; | ||
@@ -68,5 +81,5 @@ | ||
for(const h of cacheHeaderNames) { | ||
for (const h of cacheHeaderNames) { | ||
if (res.headers[h]) { | ||
for(const headerValue of expectedHeaderValues) { | ||
for (const headerValue of expectedHeaderValues) { | ||
if (res.headers[h].toLowerCase().startsWith(headerValue)) { | ||
@@ -100,3 +113,3 @@ result.ok = true; | ||
result.ok = unique.length === 1; | ||
result.got = `${ values.join(', ' )}`; | ||
result.got = `${values.join(', ')}`; | ||
@@ -111,3 +124,3 @@ return result; | ||
debug(expectedHeader); | ||
debug(expectedHeader); | ||
@@ -175,7 +188,6 @@ let result = { | ||
typeof body === 'object' && | ||
( | ||
res.headers['content-type'].indexOf('application/json') !== -1 | ||
|| res.headers['content-type'].indexOf('application/problem+json') !== -1 | ||
|| res.headers['content-type'].indexOf('application/ld+json') !== -1 | ||
) | ||
(res.headers['content-type'].indexOf('application/json') !== -1 || | ||
res.headers['content-type'].indexOf('application/problem+json') !== | ||
-1 || | ||
res.headers['content-type'].indexOf('application/ld+json') !== -1) | ||
) { | ||
@@ -194,3 +206,6 @@ result.ok = true; | ||
} else { | ||
result.ok = res.headers['content-type'] && res.headers['content-type'].toLowerCase() === expectedContentType.toLowerCase(); | ||
result.ok = | ||
res.headers['content-type'] && | ||
res.headers['content-type'].toLowerCase() === | ||
expectedContentType.toLowerCase(); | ||
result.got = res.headers['content-type'] || 'content-type header not set'; | ||
@@ -213,3 +228,5 @@ return result; | ||
if (Array.isArray(expectedStatusCode)) { | ||
result.ok = expectedStatusCode.filter(x => Number(res.statusCode) === Number(x)).length > 0; | ||
result.ok = | ||
expectedStatusCode.filter((x) => Number(res.statusCode) === Number(x)) | ||
.length > 0; | ||
} else { | ||
@@ -226,3 +243,6 @@ result.ok = Number(res.statusCode) === Number(expectedStatusCode); | ||
const expectedNotStatusCode = template(expectation.notStatusCode, userContext); | ||
const expectedNotStatusCode = template( | ||
expectation.notStatusCode, | ||
userContext | ||
); | ||
@@ -232,7 +252,9 @@ let result = { | ||
expected: `Status code different than ${expectedNotStatusCode}`, | ||
type: 'notStatusCode', | ||
type: 'notStatusCode' | ||
}; | ||
if (Array.isArray(expectedNotStatusCode)) { | ||
result.ok = !expectedNotStatusCode.filter((x) => Number(res.statusCode) === Number(x)).length; | ||
result.ok = !expectedNotStatusCode.filter( | ||
(x) => Number(res.statusCode) === Number(x) | ||
).length; | ||
} else { | ||
@@ -246,3 +268,9 @@ result.ok = Number(res.statusCode) !== Number(expectedNotStatusCode); | ||
function checkProperty(expectationName, expectedProperty, expectedCondition, failureMessage, body) { | ||
function checkProperty( | ||
expectationName, | ||
expectedProperty, | ||
expectedCondition, | ||
failureMessage, | ||
body | ||
) { | ||
let result = { | ||
@@ -261,3 +289,3 @@ ok: false, | ||
result.ok = isOk; | ||
result.got = isOk ? expectedProperty: failureMessage; | ||
result.got = isOk ? expectedProperty : failureMessage; | ||
return result; | ||
@@ -273,3 +301,9 @@ } | ||
const failureMessage = `response body has no ${expectedProperty} property`; | ||
return checkProperty(expectationName, expectedProperty, expectedCondition, failureMessage, body); | ||
return checkProperty( | ||
expectationName, | ||
expectedProperty, | ||
expectedCondition, | ||
failureMessage, | ||
body | ||
); | ||
} | ||
@@ -281,6 +315,13 @@ | ||
const expectedCondition = (body, expectedProperty) => !_.has(body, expectedProperty); | ||
const expectedCondition = (body, expectedProperty) => | ||
!_.has(body, expectedProperty); | ||
const expectedProperty = template(expectation[expectationName], userContext); | ||
const failureMessage = `response body has ${expectedProperty} property`; | ||
return checkProperty(expectationName, expectedProperty, expectedCondition, failureMessage, body); | ||
return checkProperty( | ||
expectationName, | ||
expectedProperty, | ||
expectedCondition, | ||
failureMessage, | ||
body | ||
); | ||
} | ||
@@ -287,0 +328,0 @@ |
@@ -15,3 +15,3 @@ /* This Source Code Form is subject to the terms of the Mozilla Public | ||
prettyError: prettyError, | ||
silent: silent, | ||
silent: silent | ||
}; | ||
@@ -26,3 +26,7 @@ | ||
if (requestExpectations.results.length > 0) { | ||
console.log(`${chalk.blue('*', req.method, urlparse(req.url).path)} ${req.name ? '- ' + req.name : ''}\n`); | ||
console.log( | ||
`${chalk.blue('*', req.method, urlparse(req.url).path)} ${ | ||
req.name ? '- ' + req.name : '' | ||
}\n` | ||
); | ||
} | ||
@@ -32,3 +36,3 @@ | ||
requestExpectations.results.forEach(result => { | ||
requestExpectations.results.forEach((result) => { | ||
console.log( | ||
@@ -58,3 +62,3 @@ ` ${result.ok ? chalk.green('ok') : chalk.red('not ok')} ${ | ||
console.log(chalk.yellow(' Headers:')); | ||
Object.keys(res.headers).forEach(function(h) { | ||
Object.keys(res.headers).forEach(function (h) { | ||
console.log(` ${h}: ${res.headers[h]}`); | ||
@@ -66,5 +70,9 @@ }); | ||
console.log(chalk.yellow(' User variables:')); | ||
Object.keys(userContext.vars).filter(varName => varName !== '$processEnvironment').forEach(function(varName) { | ||
console.log(` ${varName}: ${userContext.vars[varName]}`); | ||
}); | ||
Object.keys(userContext.vars) | ||
.filter( | ||
(varName) => varName !== '$processEnvironment' && varName !== '$env' | ||
) | ||
.forEach(function (varName) { | ||
console.log(` ${varName}: ${userContext.vars[varName]}`); | ||
}); | ||
} | ||
@@ -77,3 +85,3 @@ | ||
function prettyError(requestExpectations, req, res, userContext) { | ||
if (requestExpectations.results.find(result => !result.ok) === undefined) { | ||
if (requestExpectations.results.find((result) => !result.ok) === undefined) { | ||
return; | ||
@@ -87,3 +95,3 @@ } | ||
.split('\n') | ||
.map(function(line) { | ||
.map(function (line) { | ||
return str + line; | ||
@@ -90,0 +98,0 @@ }) |
{ | ||
"name": "artillery-plugin-expect", | ||
"version": "2.3.0", | ||
"version": "2.3.1-884a216", | ||
"description": "Expectations and assertions for HTTP scenarios", | ||
@@ -21,5 +21,5 @@ "main": "index.js", | ||
"devDependencies": { | ||
"ava": "^3.15.0", | ||
"shelljs": "^0.8.4" | ||
"shelljs": "^0.8.4", | ||
"tap": "^16.3.7" | ||
} | ||
} | ||
} |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
271561
9
533
2