0.0.3 • Published 6 years ago

nightsleep v0.0.3

Weekly downloads
1
License
ISC
Repository
github
Last release
6 years ago

NightSleep

The library helps you to implement live tests.

We should test a lot of cases in live: timeouts, server errors and so on.

Current implementation is working only with Express.

Initialization

npm install nightsleep --save

Usage

Example of usage with Express

const NigthSleep =  require('../index');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();

const config = require('./example.json');

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded


NigthSleep.initRoutes(app, console, config);

app.listen(3000, () => console.info('App listening on port 3000!'));

Structure of route-configuration.

Example of config-file is here

First route that can process a request will do it and send response without propagation it into next routes.

propertymeaning
urlrequest url
methodHTTP-method(capitalized)
statusreturns as a result of successful request
bodyresponse body
paramsobject with properties that match parameter names in the url and as value is array with values
bodyParamsthe same as params but for body parameters
queryParamsthe same as params but for query parameters

Also instead of each response we can get server-error or timeout, for this need to use additional headers:

headervalue
x-night-sleep'timeout'(timeout based on value-header) OR 'server-error'(returns 501)
x-night-sleep-valuevalue for timeout

Request reaches success if all parameters are valid and no special headers were sent. It returns 'status' and 'body'.

An example - the enpoint for two customers, customerNumber should be sent in url, that returns 200 and body with benefits:

{
  "url": "/subscription-service/members/:customerNumber",
  "method": "GET",
  "status": 200,
  "body": {
    "benefits": {
      "VOUCHER_OPTION": "MOCK-BENEFIT"
    }
  },
  "params": {
    "customerNumber": ["304045544", "304063444"]
  }
}