artillery-plugin-expect
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -9,3 +9,3 @@ /* This Source Code Form is subject to the terms of the Mozilla Public | ||
const chalk = require('chalk'); | ||
const renderVariables = require('artillery/util').renderVariables; | ||
const template = global.artillery ? global.artillery.util.template : require('artillery/util').template; | ||
const _ = require('lodash'); | ||
@@ -20,5 +20,2 @@ | ||
// FIXME: Current implementation only works with primitive values, | ||
// and forces everything to a string. Objects, lists, and type checks | ||
// can be implemented with template() exported from artillery/util. | ||
function expectEquals(expectation, body, req, res, userContext) { | ||
@@ -36,3 +33,3 @@ debug('check equals'); | ||
const values = _.map(expectation.equals, (str) => { | ||
return String(renderVariables(String(str), userContext.vars)); | ||
return String(template(String(str), userContext.vars)); | ||
}); | ||
@@ -52,9 +49,10 @@ | ||
const expectedContentType = template(expectation.contentType, userContext); | ||
let result = { | ||
ok: false, | ||
expected: expectation.contentType, | ||
expected: expectedContentType, | ||
type: 'contentType' | ||
}; | ||
if (expectation.contentType === 'json') { | ||
if (expectedContentType === 'json') { | ||
if ( | ||
@@ -76,3 +74,3 @@ typeof body === 'object' && | ||
} else { | ||
result.ok = res.headers['content-type'] && res.headers['content-type'].toLowerCase() === expectation.contentType.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'; | ||
@@ -86,9 +84,11 @@ return result; | ||
const expectedStatusCode = template(expectation.statusCode, userContext); | ||
let result = { | ||
ok: false, | ||
expected: expectation.statusCode, | ||
expected: expectedStatusCode, | ||
type: 'statusCode' | ||
}; | ||
result.ok = res.statusCode === expectation.statusCode; | ||
result.ok = Number(res.statusCode) === Number(expectedStatusCode); | ||
result.got = res.statusCode; | ||
@@ -101,5 +101,6 @@ return result; | ||
const expectedProperty = template(expectation.hasProperty, userContext); | ||
let result = { | ||
ok: false, | ||
expected: expectation.hasProperty, | ||
expected: expectedProperty, | ||
type: 'hasProperty' | ||
@@ -109,8 +110,8 @@ }; | ||
if (typeof body === 'object') { | ||
if (_.has(body, expectation.hasProperty)) { | ||
if (_.has(body, expectedProperty)) { | ||
result.ok = true; | ||
result.got = `${body[expectation.hasProperty]}`; | ||
result.got = expectedProperty; | ||
return result; | ||
} else { | ||
result.got = `response body has no ${expectation.hasProperty} property`; | ||
result.got = `response body has no ${expectedProperty} property`; | ||
return result; | ||
@@ -117,0 +118,0 @@ } |
@@ -44,3 +44,3 @@ /* This Source Code Form is subject to the terms of the Mozilla Public | ||
console.log(chalk.yellow(' User variables:')); | ||
Object.keys(userContext.vars).forEach(function(varName) { | ||
Object.keys(userContext.vars).filter(varName => varName !== '$processEnvironment').forEach(function(varName) { | ||
console.log(' ', varName, ':', userContext.vars[varName]); | ||
@@ -47,0 +47,0 @@ }); |
{ | ||
"name": "artillery-plugin-expect", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Expectations and assertions for HTTP", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "node test/index" | ||
"test": "bash test/run.sh" | ||
}, | ||
@@ -20,3 +20,6 @@ "author": "Hassy Veldstra <h@artillery.io>", | ||
"@commitlint/config-conventional": "^7.0.1", | ||
"husky": "^1.0.0-rc.13" | ||
"artillery": "^1.6.0-26", | ||
"ava": "^0.25.0", | ||
"husky": "^1.0.0-rc.13", | ||
"shelljs": "^0.8.3" | ||
}, | ||
@@ -23,0 +26,0 @@ "husky": { |
# artillery-plugin-expect | ||
CI status: [![CircleCI](https://circleci.com/gh/shoreditch-ops/artillery-plugin-expect/tree/master.svg?style=svg)](https://circleci.com/gh/shoreditch-ops/artillery-plugin-expect/tree/master) | ||
## Functional API testing with Artillery | ||
Add expectations to your HTTP scenarios for functional API testing with Artillery. | ||
- Add expectations and assertions to your HTTP scenarios | ||
- Use the same `artillery` command to run functional / acceptance tests on your APIs | ||
- See details of any failed assertions (request headers & body, response etc) | ||
- Use your Artillery scenarios for **both** functional testing and load testing | ||
@@ -17,2 +22,4 @@ 🐞 Please report issues over at [https://github.com/shoreditch-ops/artillery/issues](https://github.com/shoreditch-ops/artillery/issues) | ||
**Important**: this plugin requires Artillery `v1.6.0-26` or higher. | ||
### Enable the plugin in the config section | ||
@@ -27,3 +34,3 @@ | ||
### Use expectations on your requests | ||
### Add expectations to your requests | ||
@@ -48,2 +55,49 @@ ```yaml | ||
### Run your test & see results | ||
Run your script that uses expectations with: | ||
``` | ||
artillery run --quiet my-script.yaml | ||
``` | ||
The `--quiet` option is to stop Artillery from printing its default reports to the console. | ||
Failed expectations provide request and response details: | ||
![artillery expectations plugin screenshot](./docs/expect-output.png) | ||
### Re-using scenarios as load tests or functional tests | ||
This plugin allows for the same scenario to be re-used for either load testing or functional testing of an API. (The only real difference between the two, of course, is how many virtual users you run -- only one for functional tests, and `$BIG_NUMBER` for a load test.) | ||
In practical terms, what you probably want to do is use the [`environments` functionality](https://artillery.io/docs/script-reference/#environments) in Artillery to create a separate "environment" with configuration for functional testing. Something like: | ||
```yaml | ||
config: | ||
target: "https://my.api.internal" | ||
environments: | ||
# | ||
# This is our load testing profile, where we create a lot of virtual users. | ||
# Note that we don't load the plugin here, so that we don't see the output | ||
# from the plugin. | ||
# | ||
load: | ||
phases: | ||
- duration: 600 | ||
arrivalRate: 10 | ||
# | ||
# This is our functional testing profile, with a single virtual user, and | ||
# the plugin enabled. | ||
# | ||
functional: | ||
phases: | ||
- duration: 1 | ||
arrivalCount: 1 | ||
plugins: | ||
expect: {} | ||
scenarios: | ||
# Your scenario definitions go here. | ||
``` | ||
## Expectations | ||
@@ -89,4 +143,8 @@ | ||
## Feedback | ||
This plugin is still in the early stages and we would love to hear your comments, ideas and suggestions for improvements. Please file an issue over at [https://github.com/shoreditch-ops/artillery/issues](https://github.com/shoreditch-ops/artillery/issues). | ||
## License | ||
MPL 2.0 |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
273546
15
387
147
0
6
2