node-vcr
v2.3.4
Published
Record your test suite's HTTP interactions and replay them during future test runs for fast, deterministic, accurate tests
Downloads
157
Readme
Node VCR
Record HTTP interactions The Node Way™. This repository is basically a clone of flickr/yakbak and is also based on ijpiantanida/talkback.
Installation
$ npm install node-vcr --save-dev
Code Documentation
Please refer to the github wiki page
Usage
The main idea behind testing HTTP clients with node-vcr is:
- Make your client's target host configurable
- Set up a node-vcr server locally to proxy the target host
- Point your client at the node-vcr server.
Then develop or run your tests. If a recorded HTTP request is found on disk, it will be played back instead of hitting the target host. If no recorded request is found, the request will be forwarded to the target host and recorded to disk (or return 404).
const crypto = require('crypto')
const http = require('http')
const nodeVcr = require('node-vcr')
const path = require('path')
const _ = require('lodash')
const proxyTarget = 'https://api.github.com/users/mbaertschi/orgs'
const dirname = path.join(__dirname, 'playback')
const port = 8888
const hash = (req, body) => {
const action = `${req.method.toLowerCase()}_${_.last(req.url.split('/'))}`
const content = body.toString()
const md5sum = crypto.createHash('md5')
return `${action}_${md5sum.update(content).digest('hex')}`
}
const handler = nodeVcr(proxyTarget, {
dirname,
hash
})
const server = http.createServer(handler)
server.listen(port)
Options
| Name | Type | Description | Default |
| --- | --- | --- | --- |
| host | String | The proxy target to tape | |
| dirname | String | The tapes directory | ./tapes/
| noRecord | Boolean | If true, requests will return a 404 error if the tape doesn't exist | false
|
| maxRedirects | Number | Number of max http redirects. 0 means no redirects | 5
|
| tapeRequestBody | Boolean | If enabled the request body will be written to tape | false
|
| ignoreHeaders | Array | Headers which must not be written down to tape (req and res) | [ ]
|
| hash | Function | Provide your own IncomingMessage hash function of the signature function (req, body)
| see source
|
| reload | Boolean | If true, node-vcr will reload (delete and record) required tape| false
|
| refresh | Boolean | If true, node-vcr will refresh required tape| false
|
Examples
Some examples and its results can be found in the folder under ./examples.
Tech-Stack
- nodemon development mode
- jest test environment
- jsdoc documentation
- npm-check for dependencies check
- pre-commit for pre git commit hooks
- babel to compile to es2015
Scripts
# start development mode with nodemon
yarn dev
# run tests with jest
yarn test
# start continous integration testing with jest
yarn ci
# generate the jsdoc documentation
yarn jsdoc
# run eslint
yarn lint
# check for dependendies updates
yarn deps
# build with babel
yarn build
License
MIT