Coder Social home page Coder Social logo

restpal's Introduction

RESTpal

A tiny library for testing REST APIs, built with Node.js, powered by request, tv4.

NPM Version NPM Downloads Travis Status

Usage

var restpal = require('./index.js');

restpal
  .get('http://your.api/to-test')
  .status(200)
  .header('content-type', /json/)
  .schema({
    required: ['status', 'msg'],
    properties: {
      status: {
        type: 'string',
        pattern: /^[a-z]*$/
      }
    }
  })
.run();

// OR

restpal
  .get('http://your.api/to-test')
  .pattern({
    status: 200,
    timer: 2000,
    header: {
      'content-type': /json/
    },
    schema: {
      type: 'string',
      pattern: /^[a-z]*$/
    }
  })
.run();

Feature:

  • test your HTTP Status Code
  • test your HTTP Headers
  • set a request timer
  • validate response body with JSON Schema
  • work with any testing framework

Installation

Install from NPM.

$ npm install restpal

API

restpal(url, options)

Provide common used REST methods: .get(), .post(), .put(), .delete(), It's just a wrapper of lib/restpal.js

  • url {String}
  • options {Object}
restpal.get(url, options)
restpal.post(url, options)
restpal.put(url, options)
restpal.delete(url, options)

new Restpal(url, options)

Will make a request with given arguments

  • url {String}
  • options {Object}
new Restpal(url, options).status(200).run();

.header(key, value), .header(options)

  • key {String}
  • value {String} or {RegExp}
  • options {Object}
restpal
  .get(url)
  .header('content-length', /\d/)
  .header('content-type', /json/)
.run()

// OR

restpal
  .get(url)
  .header({
    'content-length': '0',
    'content-type': 'text/html'
  })
.run()

.status(code)

  • code {Number} HTTP Status Code
restpal
  .get(url)
  .status(200)
.run()

.schema(options)

JSON Schema Validation, link: vocabulary examples

  • options {Object}
restpal
  .get(url)
  .schema({
    type: 'object',
    required: [
      'status', 'msg'
    ],
    properties: {
      status: {
        type: 'number',
        pattern: /\d/
      }
    }
  })
.run()

.timer(time)

Request should finish in @time miliseconds.

  • time {Number}
restpal
  .get(url)
  .timer(1000)
.run()

.pattern(options)

Test from a JSON Object which contains the expected fields like status, header, timer, schema

  • options {Object}
restpal
  .get(url)
  .pattern({
    status: 200,
    timer: 2000,
    header: {
      'content-length', /\d/
    },
    schema: {
      type: 'string',
      pattern: /^[a-z]*$/
    }
  })
.run()

.check(function (res) { })

Check response body, throw an Error if it's wrong.

  • {Function}
restpal
  .get(url)
  .check(function (res) {
    if (!res.headers) {
      throw new Error(':lol');
    }
  })
.run()

.run(function (err) { })

Happy testing.

  • {Function} optional
restpal
  .get(url)
  .status(200)
.run(function (err) {
  if (err) {
    console.log(err)
  } else {
    throw new Error(':lol');
  }
})

License:

The MIT License (MIT)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.