1.0.1 • Published 8 years ago

node-api-mocker v1.0.1

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

Node-Api-Mocker

npm downloads Build Status Inline docs Code Climate Test Coverage

Description

Node-Api-Mocker is a node module which allow you to simulate a rest service / rest api. You can define the api as you like using a conf.json, sample can be found in /spec/test-conf/. You can configure a port on which the server will listen and also an fail rate for request. This feature enables you to test server error which your normally should not receive. Like internal errors or similar things. Node-Api-Mocker is so designed that you can either use it via command line or require it. Which makes it an ideal tool for testing. And usages during development.

Schema for the conf.json:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "global_conf": {
            "type": "object",
            "properties": {
                "port": {
                    "type": "integer"
                },
                "failing_rate": {
                    "type": "integer"
                }
            }
        },
        "routes": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "method": {
                        "type": "string"
                    },
                    "path": {
                        "type": "string"
                    },
                    "success": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "status": {
                                    "type": "integer"
                                },
                                "body": {
                                    "type": "object"
                                }
                            },
                            "required": [
                                "status",
                                "body"
                            ]
                        }
                    },
                    "error": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "status": {
                                    "type": "integer"
                                },
                                "body": {
                                    "type": "object"
                                }
                            },
                            "required": [
                                "status",
                                "body"
                            ]
                        }
                    }
                },
                "required": [
                    "method",
                    "path",
                    "success",
                    "error"
                ]
            }
        }
    },
    "required": [
        "global_conf",
        "routes"
    ]
}

Constributor

Templum

Tech

Node-Api-Mocker is powered by the following node modules:

  • Express - Fast, unopinionated, minimalist web framework for Node.js
  • jsonschema - JSON schema validator, which is designed to be fast and simple to use. The latest IETF published draft is v4, this library is mostly v4 compatible.

Installation

Installation from git:

$ git clone https://github.com/Templum/node-api-mocker.git
$ cd node-api-mocker
$ npm install --production

Installation from npm:

$ npm install node-api-mocker
$ npm install -g node-api-mocker //Recommended when using as command line tool

Usage

Usage via require():

var apiMocker = require('node-api-mocker');
apiMocker('path/to/config/file.json', (server) => {
    // Do what ever you want with server
});

Usage via command line:

$ node-api-mocker path/to/config/file.json

Changelog

1.0.0

  • Added Schema for config.json
  • Restructured Project
  • Removed on validation solution for the conf.json
  • Errors regarding server start now don't get caught
  • Bugfixes

Development

Want to contribute? Great! Feel free to clone and set up pull request. Or send issue reports.

Todos

  • Enhance Documentation with code samples
  • Rewrite some Util Modules as class
  • Testing