2.3.4 • Published 5 years ago

node-vcr v2.3.4

Weekly downloads
185
License
MIT
Repository
github
Last release
5 years ago

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:

  1. Make your client's target host configurable
  2. Set up a node-vcr server locally to proxy the target host
  3. 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

NameTypeDescriptionDefault
hostStringThe proxy target to tape
dirnameStringThe tapes directory./tapes/
noRecordBooleanIf true, requests will return a 404 error if the tape doesn't existfalse
maxRedirectsNumberNumber of max http redirects. 0 means no redirects5
tapeRequestBodyBooleanIf enabled the request body will be written to tapefalse
ignoreHeadersArrayHeaders which must not be written down to tape (req and res)[ ]
hashFunctionProvide your own IncomingMessage hash function of the signature function (req, body)see source
reloadBooleanIf true, node-vcr will reload (delete and record) required tapefalse
refreshBooleanIf true, node-vcr will refresh required tapefalse

Examples

Some examples and its results can be found in the folder under ./examples.

Tech-Stack

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

2.3.4

5 years ago

2.3.3

5 years ago

2.3.2

5 years ago

2.3.1

5 years ago

2.2.0

6 years ago

2.1.0

6 years ago

2.0.2

6 years ago

2.0.1

6 years ago

2.0.0

6 years ago

1.4.5

6 years ago

1.4.4

6 years ago

1.4.3

6 years ago

1.4.2

6 years ago

1.4.1

6 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago