restinpeace

A REST Client with Service Testing Framework

npm install restinpeace
9 downloads in the last week
36 downloads in the last month

REST in peace

A REST Client with Service Testing Framework

Stability: 2 - Unstable

install

npm install restinpeace

api

rip.client(opt_options)

Creates a new Client object.

opt_options optionally contains the default request parameters:

{
  "json": false,
  "returnBuffer": false
  // + all options for rip.request()
}

The client diverts responses through the JsonReadStream if they contain the appropriate content-type header and returnBuffer is false.

example

var client = rip.client({host: 'www.google.com'});
client.get('/', function(err, statusCode, headers, body) {
  console.log(err ? err.message : err, statusCode, headers, body);
}).buffer();

client.request(options, opt_callback);

Issue a http request.

options is an url string or an object with request parameters.

client.get(options, opt_callback);

client.post(options, opt_callback);

client.put(options, opt_callback);

client.head(options, opt_callback);

client.options(options, opt_callback);

client.delete(options, opt_callback);

rip.request(options, opt_callback)

Creates a new Request object. Request extends Stream. opt_callback is an optional function(err, statusCode, headers, body).

options contains the request parameters, with the following defaults:

{
  proto: 'http',
  host: 'localhost',
  port: 80,
  method: 'GET',
  path: '/',
  query: null, // object or string
  body: null, // string or buffer
  encoding: 'utf8', // only determines the request content-encoding
  buffer: false // when set to true, response data is returned as Buffer
                //   with the complete listener.
}

examples

Using a callback & buffering response data
rip.request({host: 'www.google.com', buffer: true},
    function(err, statusCode, headers, body) {
      console.log(err ? err.message : err, statusCode, headers, body);
    });
Using event listeners
rip.request({host: 'www.google.com'})
    .on('complete', function(statusCode, headers, body) {
      console.log('complete:', statusCode, headers, body);
    })
    .on('error', function(err) {
      console.log('error:', err.message);
    })
    .pipe(fs.createWriteStream('./response.txt'));

Event: 'error'

function(err)

Emitted when a stream or connection error occurs. This event is directly followed by the end event.

Event: 'headers'

function(statusCode, headers)

Event: 'data'

function(chunk)

Event: 'end'

function()

Emitted when the response ended.

Event: 'close'

function()

Emitted right after the end event.

Event: 'complete'

function(statusCode, headers, body)

Emitted after the close event, but only when the request completed without errors. body will be null if the buffer is not enabled.

request.divert(stream)

Diverts the response through another stream.

request.buffer()

Enables the response buffer. This provides an alternative to setting the buffer option.

request.abort()

request.write(chunk, opt_encoding)

request.end(opt_chunk, opt_encoding)

request.pipe(stream, opt_options)

rip.createJsonReadStream(opt_options)

Creates a new JsonReadStream object. JsonReadStream extends Stream.

opt_options:

{
  encoding: 'utf8',
  delimiter: '\r\n'
}

rip.createJsonWriteStream(opt_options)

Creates a new JsonWriteStream object. JsonWriteStream extends Stream.

opt_options:

{
  encoding: 'utf8',
  delimiter: '\r\n'
}

license

(The MIT License)

Copyright (c) 2012 Malte-Thorben Bruns <skenqbx@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
npm loves you