fralonra / injectar

Inject a fake http request to test, debug your server logic with ease.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

injectar

npm version Build Status Greenkeeper badge

Deprecated: light-my-request is now supporting chainable APIs! Check here.

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
  })
})

// or using async/await
try {
  await injectar(dispatch)
    .get('http://example.site/test')
    .end()
} catch (err) {
  // handle error
}

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
  })
})

About

Inject a fake http request to test, debug your server logic with ease.


Languages

Language:JavaScript 100.0%