1.4.0 • Published 4 years ago

injectar v1.4.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

injectar

npm version Build Status Greenkeeper badge

Inject a fake http request to test, debug your server logic with ease. Using light-my-request internally. Provide friendly and chainable APIs based on light-my-request.

Install

npm install injectar

or

yarn add injectar

Usage

const injectar = require('injectar')

function dispatch (req, res) {
  res.writeHead(200, { 'content-type': 'text/plain' })
  res.end('hello injectar')
}

injectar(dispatch)
  .get('http://example.site/test')
  .end((err, res) => {
    // do something
  })
})

API

Check the document for light-my-request for more.

constructor (dispatchFunction, options)

The constructor recieves two arguments: the dispatch function and the options to be passed to the light-my-request instance.

get

post

put

delete

head

options

patch

trace

The above all determine the request method.

body

Add body to request. Can be a string, Buffer, Stream or object (will be treated as JSON format).

injectar(dispatch)
  .post('http://example.site/test')
  .body({ foo: 'bar' })
  .end((err, res) => {
    // do something
  })
})

headers

Add request headers.

injectar(dispatch)
  .get('http://example.site/test')
  .headers({ foo: 'bar' })
  .end((err, res) => {
    // do something
  })
})

header

Add a single request header.

injectar(dispatch)
  .get('http://example.site/test')
  .header('foo', 'bar')
  .end((err, res) => {
    // do something
  })
})

NOTE: If followed by a headers method, the value provided by headers will override the previous set headers, instead of combining them.

payload

An alias for body.

query

Add querystring to url.

injectar(dispatch)
  .get('http://example.site/test')
  .query({ foo: 'bar' })
  .end((err, res) => {
    // do something
  })
})
1.4.0

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.0.1

5 years ago